Java/JCE:解密“长”字符串 使用 RSA 加密的消息
我收到一条包含在 byte[] 中的消息,用“RSA/ECB/PKCS1Padding”加密。 为了解密它,我创建了一个 Cipher c 并用 启动它
c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
直到现在我只使用 doFinal() 方法解密了小消息,返回带有解密字节的 byte[]。
c.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptetBytes = c.doFinal(encryptedBytes);
但在本例中,数据较大(大约 500 字节),并且 doFinal() 方法会引发异常(javax.crypto.IllegalBlockSizeException:数据不得长于 128 字节)。 我想我需要使用 update() - 方法,但我不知道如何让它正常工作。 这是怎么做到的?
I've got a message contained in an byte[], encrypted with "RSA/ECB/PKCS1Padding". To decrypt it I create a Cipher c and initiate it with
c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
Untill now I have only decrypted small messages, using the doFinal() method, returning an byte[] with the decrypted bytes.
c.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptetBytes = c.doFinal(encryptedBytes);
But in this case the data is bigger (approx 500 Bytes), and the doFinal()-method throws an exception (javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes). I guess I need to use the update()- method, but I can't figure out how to get it to work properly. How is this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为将 RSA 加密用于除密钥传输之外的任何内容都是滥用行为。
为对称密码生成新密钥并用它加密您的批量数据。 然后用RSA加密密钥。 将对称加密的密文与非对称加密的内容加密密钥一起发送给收件人。
I think using RSA encryption for anything but key transport is abuse.
Generate a new key for a symmetric cipher and encrypt your bulk data with that. Then encrypt the key with RSA. Send the symmetrically-encrypted cipher-text along with the asymmetrically-encrypted content encryption key to your recipient.
使用 RSA,您只能加密/解密大小不超过密钥长度减去填充长度的块。 如果你的数据比你的密钥长,也许它只是合并在一个数组中,所以你应该将它分成与你的密钥大小相同的块(128 字节建议 1024 个密钥,没有填充,我不确定是否可能)。 使用 update() 的情况并非如此。
简单来说,你必须知道这个数组是如何创建的。
一般来说,RSA 不应该用于加密大量数据,因为它非常耗时。 应用于对称密码的密钥加密,例如 AES。
看看这里:
https://www.owasp.org/index.php/Digital_Signature_Implementation_in_Java
With RSA you can only encrypt/decrypt block with size up to your key length minus padding length. If you have data longer than your key maybe it is just merged in one array so you should split it into chunks with size of your key (128 bytes suggests 1024 key with no padding, I'm not sure if it's possible). Using update() is not the case here.
Simply, you have to know how this array was created.
Generally speaking, RSA shouldn't be used to encrypt large amount of data as it's quite time consuming. Should be used to encrypt key to symmetric cipher, like AES.
Take a look here:
https://www.owasp.org/index.php/Digital_Signature_Implementation_in_Java
就像 Erickson 所说,
您应该采取的加密步骤是:
解密:
Like Erickson said,
The steps you should take encrypt are:
To decrypt: