我举了rsa加密和解密的例子。如果消息大于110字节,我无法解密。但错误说;数据不得超过117个字节。为什么不能使用7个字节?
我的班级:
public class RSAEx {
static Cipher cipher;
static KeyPairGenerator keyPairGenerator;
static KeyPair keyPair;
static String message = "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
public static void main(String[] ars) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, IOException, BadPaddingException {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPair = keyPairGenerator.generateKeyPair();
cipher = Cipher.getInstance("RSA");
decryptIt(encryptIt());
}
static byte[] encryptIt() throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException {
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
SealedObject encryptedMessage = new SealedObject(message, cipher);
System.out.println("Encrypt Alg : "+encryptedMessage.getAlgorithm());
System.out.println("Encrypted Msg : ");
for (int i = 0; i < keyPair.getPrivate().getEncoded().length; i++){
System.out.print(keyPair.getPrivate().getEncoded()[i]);
}
System.out.print("\n");
return cipher.doFinal(message.getBytes());
}
static void decryptIt(byte[] encryptedMessage) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
System.out.println("Decrypted Msg : "+new String(cipher.doFinal(encryptedMessage)));
}
}
错误:
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:344)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at javax.crypto.SealedObject.<init>(SealedObject.java:170)
at com.mimcrea.metronic_ui_android.RSAEx.encryptIt(RSAEx.java:35)
at com.mimcrea.metronic_ui_android.RSAEx.main(RSAEx.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
答案 0 :(得分:2)
您可能正在使用RSA密钥大小为1024的旧版Java?在我的计算机上使用java 8,它的密钥大小为2048,最大值为245字节。
密钥大小/ 8 - 填充是11个字节。所以我在这里得到了例外:
线程“main”中的异常javax.crypto.IllegalBlockSizeException:数据不得超过245个字节
2048/8 - 11 = 245字节
对于你来说它是1024键大小 - &gt; 1024/8 - 11 = 117
问题在于您执行密封对象的代码。由于密码是7个字节,因此该密封对象可能带来一些开销。如果您尝试这样的代码:
static Cipher cipher;
static KeyPairGenerator keyPairGenerator;
static KeyPair keyPair;
static String message = "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
public static void main(String[] ars) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, IOException, BadPaddingException {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPair = keyPairGenerator.generateKeyPair();
cipher = Cipher.getInstance("RSA");
decryptIt(encryptIt());
}
static byte[] encryptIt() throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException {
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
System.out.println("Size:"+message.getBytes().length);
return cipher.doFinal(message.getBytes());
}
static void decryptIt(byte[] encryptedMessage) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
System.out.println("Decrypted Msg : "+new String(cipher.doFinal(encryptedMessage)));
}
如果不创建SealedObject(因为你还没有使用它),你将能够完成117个字节的加密和解密。