1.出现上述问题的原因: 非对称加密的密文有 "+" , 再网络传输的时候(通过url),传递到后端时 "+" 被替换为 " "。后端解析错误。
需要将空格进行替换。
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(base64.getDecoder().decode(Constant.PRIVATE_KEY));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
String newEncodeStr = encodeStr.replaceAll(" ", "+");
byte[] decodeStr = base64.getDecoder().decode(newEncodeStr);
byte[] bytes = cipher.doFinal(decodeStr);
// log.info("公钥加密,私钥解密--解密: "+ new String(bytes));
// 判断解密出的字符串是否和指定的相同
if (Constant.ENCODE_STR.equals(new String(bytes))) {
return true;
} else {
throw new RuntimeException("解密失败, 请重新登录");
}
替换之后在进行测试,问题解决!!!



