rsa 解密 乱码

yykl2008_ 2017-11-29 06:43:55
以下方法,在java main 函数里面运行,解密的字符串是正常的; 在tomcat里面运行调用时,解密出的字符串含有乱码,请问各位,这是什么原因啊,纠结了好几天。

public static String asynDecrypt() {
//需要解密字符串
String str = "Gl/qCjdyVb6OFc3OVRyKvNnvXMtpy6p98wIr。。。。";//太长了,省略部分字符串
//私钥
String pivateMod = rs.getString("dsf.epay.async.private.mod");
//密码
String pivateD = rs.getString("dsf.epay.async.private.d");

int mode = Cipher.DECRYPT_MODE;

byte[] byteSrc = str.getBytes();

byte[] modBytes = Base64.decode(pivateMod.getBytes());
byte[] dBytes = Base64.decode(pivateD.getBytes());

BigInteger modules = new BigInteger(1, modBytes);
BigInteger d = new BigInteger(1, dBytes);

KeyFactory factory = null;
try {
factory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}

Cipher cipher = null;
try {
cipher = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (NoSuchPaddingException e1) {
e1.printStackTrace();
}

RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);

PrivateKey privKey = null;
try {
privKey = factory.generatePrivate(privSpec);
} catch (InvalidKeySpecException e2) {
e2.printStackTrace();
}

try {
cipher.init(Cipher.DECRYPT_MODE, privKey);
} catch (InvalidKeyException e1) {
e1.printStackTrace();
}

byteSrc = Base64.decode(byteSrc);

int inputLen = byteSrc.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;

// 对数据分段解密
try {
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(byteSrc, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(byteSrc, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}

byte[] decryptedData = out.toByteArray();

try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}

String retSt = null;
try {
retSt = new String(decryptedData, "UTF-16LE");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return retSt;
}

/*********************************************************/
一、 main函数运行效果(正常解密):
<?xml version="1.0" encoding="utf-8"?>
<ParmXMl xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="szedi">
<Status>Error</Status>
<Message>找不到用户</Message>
//其他信息,此处省略
</ParmXMl>

二、在tomcat中运行(出现乱码)
愂彯???<?xml version="1.0" encoding="utf-8"?>
<ParmXMl xmlns:xsd??宏??

如图:




...全文
1032 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2020-01-07
  • 打赏
  • 举报
回复
操作系统的缺省字符集不同,getbytes()中应强制指定字符集。还有输出字符集为什么是 UTF-16LE? 项目字符集(windows项目缺省使用了GBK) 也会影响中文
jin_recruit 2020-01-07
  • 打赏
  • 举报
回复
楼主请问您解决这个问题了吗,我现在也遇到同样的问题,在main方法中正常,tomcat运行解密会乱码
JiaoJunfeng 2018-05-28
  • 打赏
  • 举报
回复
楼主解决了吗问题,我现在也是。 验签都没问题,但是解析返回的数据乱码。 移动端 服务器都是 utf-8(android)
yykl2008_ 2017-12-01
  • 打赏
  • 举报
回复
试过了,不是这个原因。奇怪得很啊, java main函数里面直接运行解密方法没有问题,在tomcat里面却有乱码。
自由自在_Yu 2017-11-30
  • 打赏
  • 举报
回复
retSt = new String(decryptedData, "UTF-16LE"); <?xml version="1.0" encoding="utf-8"?> 两个编码不一致
  • 打赏
  • 举报
回复
retSt = new String(decryptedData, "UTF-16LE"); ?<?xml version="1.0" encoding="utf-8"?> 改为 utf-8 编码

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧