Java/JCE:解密“长”字符串 使用 RSA 加密的消息

发布于 2024-07-08 08:01:52 字数 489 浏览 5 评论 0原文

我收到一条包含在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽月 2024-07-15 08:01:52

我认为将 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.

相对绾红妆 2024-07-15 08:01:52

使用 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

所有深爱都是秘密 2024-07-15 08:01:52

就像 Erickson 所说,

您应该采取的加密步骤是:

  1. 生成 RSA 密钥对(或从密钥存储中检索公钥)
  2. 生成对称密钥 (AES)
  3. 使用 AES 密钥加密数据
  4. 使用公共 RSA 密钥存储加密 AES 密钥
  5. (或发送给个人)使用私钥)加密的 AES 密钥和 AES 加密数据

解密:

  1. 获取与用于加密的密钥对关联的私钥 使用
  2. 私钥解密 AES 密钥 使用
  3. AES 密钥解密数据
  4. 使用数据

Like Erickson said,

The steps you should take encrypt are:

  1. Generate RSA key pair (or retrieve public key from a key store)
  2. Generate Symmetric key (AES)
  3. Encrypt data with AES key
  4. Encrypt AES key with public RSA key
  5. Store (or send to person with private key) the encrypted AES key, and the AES Encrypted Data

To decrypt:

  1. Get private key associated with that key pair used to encrypt
  2. Decrypt AES key with private key
  3. Decrypt data with AES key
  4. Use data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文