问题:
RSA加密 byte类型转String类型,格式转换之后内容变了,解密解不出来
第一种方式:使用"ISO-8859-1"编码,使用此方式编码返回结果会乱码
例如:
public static void main(String[] args) throws UnsupportedEncodingException {
String text = "你好哇aaaaaaaaa";
byte[] byteResult = text.getBytes("ISO-8859-1");
System.out.println(byteResult);
String textResult = new String(byteResult,"ISO-8859-1");
System.out.println(textResult);
}
打印结果:
[B@4783da3f
???aaaaaaaaa
Process finished with exit code 0
第二种方式:使用Base64.encodeBase64String转码
public static void main(String[] args) throws UnsupportedEncodingException {
String text = "你好哇aaaaaaaaa";
byte[] byteResult = text.getBytes("UTF-8");
System.out.println(byteResult);
String textResult = Base64.encodeBase64String(byteResult);
System.out.println(textResult);
byte[] baseResult = Base64.decodeBase64(textResult);
System.out.println(baseResult);
String result = new String(baseResult,"UTF-8");
System.out.println(result);
}
打印结果:
[B@4783da3f
5L2g5aW95ZOHYWFhYWFhYWFh
[B@439f5b3d
你好哇aaaaaaaaa
Process finished with exit code 0
我采用的第二种方式,避免字符乱码