我正在尝试从我的服务器端解密一个 RSA 字符串。 从本地服务器运行代码时效果很好, 但是当将应用程序部署到 GAE 时,该方法返回一个空字符串(不为空)。
(字符串输入是要解密的二进制数据的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”。
请指教,
谢谢你和最好的问候, 诺姆·科恩
最佳答案
new String(byteArray)
使用默认的 JVM 编码将字节数组解码为字符串。在 AppEngine 上,奇怪的是 US-ASCII。
如果您的原始内容是 UTF-8 格式,请尝试使用 new String(cipherText), "UTF-8"
关于java - Google App Engine 中的 RSA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124686/