注意:
RSA加密明文最大长度117字节,解密要求密文最大长度为128字节,所以在加密和解密的过程中需要分块进行。
RSA加密对明文的长度是有限制的,如果加密数据过大会抛出如下异常:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 byte
当要加密的byte数组超过117的时候抛出异常
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = encodeString.getBytes();
cipher.doFinal(ArrayUtils.subarray(bytes, 0, 118));
解决方法:分段加密
2 javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
当要解密的byte数组超过128的时候抛出异常
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(encodedByteArray, 0, 129));
解决方法 : 分段解密