Java RSA加密 [英] Java RSA Encryption

查看:203
本文介绍了Java RSA加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

byte [] byteData = data.getBytes(); //将字符串转换为字节数组

加密密码= Cipher.getInstance(ALGORITHM); //创建转换处理对象
cipher.init(Cipher.ENCRYPT_MODE,publicKey); //初始化对象的模式和键

byte [] encryptedByteData = cipher.doFinal(byteData); //使用对象进行加密

return new String(encryptedByteData); //将加密的字节数组转换为字符串并返回

}

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

byte [] byteData = data.getBytes(); //将字符串转换为字节数组

加密密码= Cipher.getInstance(ALGORITHM); //创建转换处理对象
cipher.init(Cipher.DECRYPT_MODE,privateKey); //初始化对象的模式和键

System.out.println(byteData.length);

byte [] decryptedByteData = cipher.doFinal(byteData); //使用对象进行解密

返回新的String(decryptptedByteData); //将解密的字节数组转换为字符串并返回

}

虽然加密工作正常(ALGORITHM是RSA),当尝试解密我刚刚从加密测试获得的字符串时,我得到以下异常:


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


我应该将加密的256位的字节,以便能够解密?

解决方案

您无法将随机字节可靠地转换为字符串。结果将取决于您运行此操作的机器上的默认字符编码。有许多编码,密文将被破坏,信息将丢失。



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



如果需要转换 byte [] 到一个字符串,使用类似Base-64的编码。


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?

解决方案

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.

这篇关于Java RSA加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆