我正在尝试从服务器端解密RSA字符串。从本地服务器运行代码时,它的工作效果很好,但是将应用程序部署到GAE时,该方法返回一个空字符串(不为null)。
(字符串输入是要解密的二进制数据的BASE64表示形式)
这是我的代码:
private static String decrypt(String pwd) {
byte[] input = Base64.decode(pwd);
try {
rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");
} catch (NoSuchAlgorithmException e) {
return null;
} catch (NoSuchPaddingException e) {
return null;
}
KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
return null;
}
String modString = "*********";
String privateExponentString = "*********";
RSAPrivateKeySpec prvKeySpec = new RSAPrivateKeySpec(new BigInteger(modString), new BigInteger(privateExponentString));
RSAPrivateKey prvKey;
try {
prvKey = (RSAPrivateKey) keyFactory.generatePrivate(prvKeySpec);
} catch (InvalidKeySpecException e) {
return null;
}
try {
rsaCipher.init(Cipher.DECRYPT_MODE, prvKey);
} catch (InvalidKeyException e) {
return null;
}
byte[] cipherText;
try {
cipherText = rsaCipher.doFinal(input);
} catch (IllegalBlockSizeException e) {
return null;
} catch (BadPaddingException e) {
return null;
}
return new String(cipherText);
}
在远程运行时,我在服务器端做了一些调试。我发现该功能:
rsaCipher.doFinal(input);
当每个项目都包含“ 0”时,返回一个字节数组。
请指教,
谢谢您,最好的问候,诺姆·科恩(Noam Cohen)
我发现了错误,输出字符串始终为128位长,而所需的未加密字符串在末尾,而所有其他字节均为(空,ASCII码0)。Google日志不会显示所有字符串,并且看起来像是空的。我只是循环输出字符串中的每个字符,然后将每个字符的ASCII码都不同于0的字符复制到我的字符串中。
我希望这能对某人有所帮助,诺姆
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句