繁体   English   中英

Google App Engine中的RSA

[英]RSA in Google App Engine

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

new String(byteArray)使用默认的JVM编码将字节数组解码为字符串。 在AppEngine上,它奇怪地是US-ASCII。

如果您的原始内容为UTF-8,请尝试使用new String(cipherText), "UTF-8"

我发现了错误,输出字符串始终为128位长,而所需的未加密字符串在末尾,而所有其他字节均为(空,ASCII码0)。 Google日志未显示所有字符串,并且看起来为空。 我只是循环输出字符串中的每个字符,并将每个字符的ASCII码都不同于0的字符复制到我的字符串中。

我希望这能对某人有所帮助,诺姆

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM