Java RSA 加密

发布于 2024-11-09 07:26:59 字数 1533 浏览 2 评论 0原文

我正在尝试来回编码一个简单的字符串“测试”。

public static String encode(Key publicKey, String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    byte[] byteData = data.getBytes(); // convert string to byte array

    Cipher cipher = Cipher.getInstance(ALGORITHM); // create conversion processing object
    cipher.init(Cipher.ENCRYPT_MODE, publicKey); // initialize object's mode and key

    byte[] encryptedByteData = cipher.doFinal(byteData); // use object for encryption

    return new String(encryptedByteData); // convert encrypted byte array to string and return it

}

public static String decode(Key privateKey, String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    byte[] byteData = data.getBytes(); // convert string to byte array

    Cipher cipher = Cipher.getInstance(ALGORITHM); // create conversion processing object
    cipher.init(Cipher.DECRYPT_MODE, privateKey); // initialize object's mode and key

    System.out.println(byteData.length);

    byte[] decryptedByteData = cipher.doFinal(byteData); // use object for decryption

    return new String(decryptedByteData); // convert decrypted byte array to string and return it

}

然而,虽然加密工作得很好(算法是“RSA”),但当尝试解密我刚刚从加密“test”获得的字符串时,我得到以下异常:

javax.crypto.IllegalBlockSizeException:数据不得超过 256 字节

我是否应该将加密字节拆分为 256 块,以便能够解密它?

I am trying to encode a simple String "test" back and forth.

public static String encode(Key publicKey, String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    byte[] byteData = data.getBytes(); // convert string to byte array

    Cipher cipher = Cipher.getInstance(ALGORITHM); // create conversion processing object
    cipher.init(Cipher.ENCRYPT_MODE, publicKey); // initialize object's mode and key

    byte[] encryptedByteData = cipher.doFinal(byteData); // use object for encryption

    return new String(encryptedByteData); // convert encrypted byte array to string and return it

}

public static String decode(Key privateKey, String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    byte[] byteData = data.getBytes(); // convert string to byte array

    Cipher cipher = Cipher.getInstance(ALGORITHM); // create conversion processing object
    cipher.init(Cipher.DECRYPT_MODE, privateKey); // initialize object's mode and key

    System.out.println(byteData.length);

    byte[] decryptedByteData = cipher.doFinal(byteData); // use object for decryption

    return new String(decryptedByteData); // convert decrypted byte array to string and return it

}

However, although the encryption works just fine (ALGORITHM is "RSA"), when trying to decrypt the string I have just gotten from encrypting "test", I get following exception:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes

Should I split the encrypted bytes in chunks of 256 in order to be able to decrypt it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

季末如歌 2024-11-16 07:26:59

您无法可靠地将随机字节转换为 String。结果将取决于运行该程序的计算机上的默认字符编码。对于许多编码,密文将被损坏,信息将丢失。

修改您的代码以使用 byte[] 代替('doFinal()` 方法的结果。

如果您需要将 byte[] 转换为字符串,使用 Base-64 等编码。

You can't reliably convert random bytes to a String. The results will depend on what your default character encoding is on the machine where you run this. With many encodings, the cipher text will be corrupted, and information will be lost.

Modify your code to use a byte[] instead (the result of the 'doFinal()` method.

If you need to convert the byte[] to a character string, use an encoding like Base-64.

黑凤梨 2024-11-16 07:26:59

来自此处

RSA算法只能加密具有最大字节长度的数据
RSA 密钥长度(以位为单位)除以 8 减 11 填充
字节,即最大字节数 = 密钥长度(以位为单位)/ 8 - 11。
如果你想加密更大的数据,那么使用更大的密钥,例如,
4096 位的密钥将允许您加密 501 字节的数据。

From here:

The RSA algorithm can only encrypt data that has a maximum byte length
of the RSA key length in bits divided with eight minus eleven padding
bytes, i.e. number of maximum bytes = key length in bits / 8 - 11.
If you want to encrypt larger data, then use a larger key, for example,
a key with 4096 bits will allow you to encrypt 501 bytes of data.

枕花眠 2024-11-16 07:26:59

如果您有很长的数据,您应该将其分割成适合的数据块并加密/解密每个数据块(不是一个好主意),或者使用对称算法(AES / DES / RC4 / 等)加密/解密它们。 ,用RSA公钥加密对称密钥并发送给对方。 (更好的主意)。

第二种方法是一种非常常见的方法,因为非对称加密算法比对称算法昂贵得多(对于加密和解密)。

If you have a long data, you should either split it to data chunks that fits and encrypt / decrypt each of them (not such a good idea) or encrypt / decrypt them using a symmetric algorithm (AES / DES / RC4 / etc.), encrypt the symmetric key with the RSA public key and send both to the other side. (much better idea).

The second approach is a very common approach, since asymmetric encryption algorithms are much more expensive than symmetric algorithms (for both encryption and decryption).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文