0

我正在尝试将数据从 Java (Android) 发送到 Node.js 应用程序,但加密不起作用并且 Node.js 没有正确解密,而且我真的不知道我在做什么。

爪哇:

                    // Encrypt
                    byte[] input = jo.toString().getBytes("UTF-8");

                    MessageDigest md = MessageDigest.getInstance("MD5");
                    byte[] thedigest = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
                    SecretKeySpec skc = new SecretKeySpec(thedigest, "AES/ECB/PKCS5Padding");
                    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                    cipher.init(Cipher.ENCRYPT_MODE, skc);

                    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
                    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
                    ctLength += cipher.doFinal(cipherText, ctLength);
                    String query = Base64.encodeToString(cipherText, Base64.DEFAULT);

query然后发送到我们的服务器并且joJSONObject

在 Node 中,我正在做:

        var decipher = crypto.createDecipher('aes-128-ecb', encryption_key);
        console.log("System: " + new Buffer(fullBuffer, "base64").toString("binary") );

        chunks = []
        chunks.push( decipher.update( new Buffer(fullBuffer, "base64").toString("binary") , 'hex', 'utf-8') );
        chunks.push( decipher.final('utf-8') );
        var txt = chunks.join("");

        console.log("System: " + txt);
        js = JSON.parse(txt);
        console.log("System: " + js);   

并且fullBuffer接收到的 POST 数据是否正确传输

4

1 回答 1

2

加密和身份验证很难调试,因为您犯的任何错误都会导致整个输出随机化。建议:

  1. 切换到像 Base64 或 gzip 这样的非加密转换,这样您就可以看到输出的样子。
  2. 试着找出哪一半坏了。捕获您的服务器输出并使用 openssl 或 python 对其进行解码。使用其中一个生成一些好的输入并将其填充到您的客户端中。
  3. CBC 比 ECB 安全得多。
  4. 如果这些都没有帮助,请进入低级密码和解密,并确保密钥和密文在内容和长度上完全匹配,并长时间地盯着算法选择。
于 2012-07-13T20:42:05.793 回答