如何用Java对大数据进行RSA加解密
我正在尝试使用 RSA 和 OAEPPadding 对实际较大的数据(例如 10 KB)执行非对称加密和解密。
许多 Stackoverflow 文章提到使用混合方法 [不对称 + 对称],但我并不是在寻找这种方法。我故意要求非对称加密。
我初始化了加密和解密密码,如下所示:
mDecryptCipher = Cipher.getInstance("RSA/None/OAEPPadding", new BouncyCastleProvider());
mDecryptCipher.init(Cipher.DECRYPT_MODE, mPrivateKey);
Log.d(TAG, "Decrypt Cipher is successfully created");
mEncryptCipher = Cipher.getInstance("RSA/None/OAEPPadding", new BouncyCastleProvider());
mEncryptCipher.init(Cipher.ENCRYPT_MODE, mPublicKey);
Log.d(TAG, "Encrypt Cipher is successfully created");
以下是我尝试加密或解密大小为 10 KB 的大数据的代码。
// Encrypt
byte[] encryptedPayload = mEncryptCipher.doFinal(payload);
// Decrypt
byte[] decryptedPayload = mDecryptCipher.doFinal(payload);
以下是我收到的异常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205)
请让我知道如何对大数据执行 RSA 加密和解密。
I am trying to perform Asymmetric Encryption and Decryption using RSA and OAEPPadding on actually larger data, say, 10 KB.
Many Stackoverflow articles mentioned to use the Hybrid approach [Assymmetric + Symmetric] but I am not looking for that. I purposely require Asymmetric Encryption.
I initialized the Encrypt and Decrypt Ciphers as follows:
mDecryptCipher = Cipher.getInstance("RSA/None/OAEPPadding", new BouncyCastleProvider());
mDecryptCipher.init(Cipher.DECRYPT_MODE, mPrivateKey);
Log.d(TAG, "Decrypt Cipher is successfully created");
mEncryptCipher = Cipher.getInstance("RSA/None/OAEPPadding", new BouncyCastleProvider());
mEncryptCipher.init(Cipher.ENCRYPT_MODE, mPublicKey);
Log.d(TAG, "Encrypt Cipher is successfully created");
Following is the code by which I am trying to Encrypt or Decrypt the large data of size 10 KB.
// Encrypt
byte[] encryptedPayload = mEncryptCipher.doFinal(payload);
// Decrypt
byte[] decryptedPayload = mDecryptCipher.doFinal(payload);
Following is the exception I received:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205)
Please let me know how to perform RSA Encryption and Decryption on large data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java Cipher 对象允许我们检索块大小,并使用它来执行重复的加密和解密,这将使我们克服“RSA 块数据太多”例外。
以下是加密的逻辑:
以下是解密的逻辑:
请注意,在我的例子中,以下是 Cipher 对象的详细信息:
EncryptCipher:
DecryptCipher:
我是开放的以及任何更好的解决方案。
The Java Cipher objects allow us to retrieve the block sizes and using that we would be performing a repetitive kind of encryption as well as decryption which would let us over-come the "too much data for RSA block" exception.
Following is the logic for Encryption:
Following is the logic for Decryption:
Please note that in my case following are the details of the Cipher objects:
EncryptCipher:
DecryptCipher:
I am open to any better solution as well.