【发布时间】:2011-02-04 11:10:33
【问题描述】:
谈javax.crypto.Cipher
我尝试使用 Cipher.getInstance("RSA/None/NoPadding", "BC") 加密数据,但出现异常:
ArrayIndexOutOfBoundsException: RSA 块的数据过多
看起来与“NoPadding”有关,因此,阅读有关填充的内容,看起来 CBC 是在这里使用的最佳方法。
我在谷歌上找到了一些关于“RSA/CBC/PKCS#7”的东西,这个“PKCS#7”是什么?为什么它没有在sun's standard algorithm names 上列出?
更新:
我想知道,如果是填充问题,为什么这个例子运行得很好?
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
/**
* Basic RSA example.
*/
public class BaseRSAExample
{
public static void main(
String[] args)
throws Exception
{
byte[] input = new byte[] { (byte)0xbe, (byte)0xef };
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
// create the keys
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
new BigInteger("d46f473a2d746537de2056ae3092c451", 16),
new BigInteger("11", 16));
RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(
new BigInteger("d46f473a2d746537de2056ae3092c451", 16),
new BigInteger("57791d5430d593164082036ad8b29fb1", 16));
RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);
RSAPrivateKey privKey = (RSAPrivateKey)keyFactory.generatePrivate(privKeySpec);
// encryption step
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = cipher.doFinal(input);
// decryption step
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(cipherText);
}
}
更新 2:
我意识到,即使我只使用Cipher.getInstance("RSA", "BC"),它也会引发相同的异常。
【问题讨论】:
-
brito 我没有仔细阅读这个问题。我会添加作为答案。现在看下面。
-
我更新了我的答案,对您的更新做出了合理的解释。
标签: java security cryptography