我下面有这段代码用于加密对象,可以完成工作。 但是我有一个包含另一个对象列表的对象。加密对象时,出现错误,即数据必须大于256个字节。当我删除列表时,加密管理无误。
public class A {
private List<B> b = new ArrayList<>();
//getter setter
}
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(message.getBytes());
} catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException
| IllegalBlockSizeException | InvalidKeySpecException | InvalidKeyException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
RSA不适合加密数量有限的数据。常规解决方案是hybrid encryption,其中使用带有随机数密钥的对称密码对数据进行加密,并且使用RSA(仅)对密钥进行加密。有关示例,请参见PKCS7 / CMS / SMIME,PGP,XMLenc,JWE,SSL / TLS和SSH。
Dupes在这里和跨栈:
Encrypting large files using a public key
How to encrypt a large file in openssl using public key
https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length
https://crypto.stackexchange.com/questions/3608/why-is-padding-used-for-rsa-encryption-given-that-it-is-not-a-block-cipher
https://crypto.stackexchange.com/questions/5782/why-is-asymmetric-cryptography-bad-for-huge-data
https://crypto.stackexchange.com/questions/10685/hybrid-encryption-with-rsa-and-aes-versus-spliting-into-multiple-rsa-messages
https://crypto.stackexchange.com/questions/25899/using-ecb-as-rsa-encryption-mode-when-encrypted-messages-are-unique
https://security.stackexchange.com/questions/37581/why-does-pgp-use-symmetric-encryption-and-rsa