超过 110 字节的大字符串的 SealedObject 大小错误

SealedObject size error for big string more than 110 bytes

我做rsa加解密的例子。如果消息大于 110 字节,我无法解密。但是错误说;数据不得超过 117 个字节。为什么不能使用 7 个字节?

我的Class:

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)

您可能正在使用 RSA 密钥大小为 1024 的旧版本 java?在我使用 java 8 的计算机上,它的 2048 密钥大小最大为 245 字节。

这是密钥大小/8 - 填充,即 11 个字节。所以在这里我得到了例外:

线程中出现异常 "main" javax.crypto.IllegalBlockSizeException:数据不得超过 245 字节

2048 / 8 - 11 = 245 字节

对你来说,密钥大小是 1024 -> 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 个字节。