java - Google App Engine 中的 RSA

标签 java google-app-engine rsa

我正在尝试从我的服务器端解密一个 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/

相关文章:

java - Spring 无法将 AOP 与嵌入式数据库脚本一起使用

python - Appengine 不会显示 Python 函数

google-app-engine - 如何在 2021 年增加 gcloud 应用程序部署超时

html - 为什么 App Engine 不加载我的样式表?

c - PEM_write_PrivateKey() 函数未将 RSA 私钥存储在 private.pem 文件中

java - 如何在android中使用RSA进行加密和解密

java - List<Object[]> 的类型安全问题

java - 关于正则表达式中字符的恰好 n 次出现

java - JNDI的目的是什么

c - 打开SSL : What does RSA n e d p q parameters represent?