在节点中加密并在java中解密 [英] Encrypt in node and decrypt in java

查看:21
本文介绍了在节点中加密并在java中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 加密代码.我正在尝试将加密部分移植到节点.基本上,node会使用crypto模块进行加密,然后Java会进行解密.

I have an encrypt-code in Java. I'm trying to port the encrypt part to node. Basically, node will do the encryption using the crypto module, and then Java will do the decryption.

以下是我在 Java 中进行加密的方法:

Here's how I do encryption in Java:

protected static String encrypt(String plaintext) {
    final byte[] KEY = {
            0x6d, 0x79, 0x56, 0x65, 0x72, 0x79, 0x54, 0x6f, 0x70,
            0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b
    };

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(KEY, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        final String encryptedString = Base64.encodeToString(
            cipher.doFinal(plaintext.getBytes()), Base64.DEFAULT);

        return encryptedString;
    } catch (Exception e) {
        return null;
    }
}

这是我在节点中进行加密的方法:

Here's how I do encryption in node:

var crypto = require('crypto'),
    key = new Buffer('6d7956657279546f705365637265744b', 'hex'),
    cipher = crypto.createCipher('aes-128-ecb', key),
    chunks = [];

cipher.setAutoPadding(true);
chunks.push(cipher.update(
    new Buffer(JSON.stringify({someKey: "someValue"}), 'utf8'),
    null, 'base64'));
chunks.push(cipher.final('base64'));

var encryptedString = chunks.join('');

在 Java 中,我得到字符串 T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA=.这被正确解密.但是,在节点中,我得到 al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM=,这显然不同,因此无法正确解密.

In Java, I get the string T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA=. This gets decrypted correctly. However, in node, I get al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM= which is obviously different and thus it won't get decrypted correctly.

我试图寻找和我有同样问题的人,这个github问题是我能找到的最接近的问题.正如该问题中所建议的,我尝试像这样运行 openssl:

I tried to look for people who has the same problem as me, and this github issue is the closest I can find. As suggested in that issue, I tried running openssl like so:

$ echo -e '{"someKey": "someValue"}' | openssl enc -a -e -aes-128-ecb -K "6d7956657279546f705365637265744b"
T4RlJo5ENV8h1uvmOHzz1MY2bhoFRHZ+ClxsV24l2BU=

我得到的结果与java产生的结果足够接近,但仍然不同:

The result I got was close enough to the one produced by java, but still different:

T4RlJo5ENV8h1uvmOHzz1MY2bhoFRHZ+ClxsV24l2BU=  // openssl
T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA=  // java
al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM=  // node

这让我想到了一个问题,如何让节点输出与我的 java 代码相同的加密字符串?我只能在 node 中更改我的代码,而不能在 java 中更改.

Which brings me to the question, how do I make node output the same encrypted string as my java code? I can only change my code in node, but not in java.

推荐答案

最后,我找到了解决我的问题的方法.感谢这位.解决方案的关键是初始化向量.引用要点:

Finally, I found the solution to my problem. Thanks to this guy. The key to the solution is the initialization vector. Quoting the gist:

//ECB 模式不需要 IV,所以保持这样,它会工作得很好.

// ECB mode won't need IV, so keep it like this and it will work well.

解决方案如下:

var crypto = require('crypto'),
    iv = new Buffer(''),
    key = new Buffer('6d7956657279546f705365637265744b', 'hex'),
    cipher = cypto.createCipheriv('aes-128-ecb', key, iv),
    chunks = [];

chunks.push(cipher.update(
    new Buffer(JSON.stringify({someKey: "someValue"}), 'utf8'),
    'buffer', 'base64'));
chunks.push(cipher.final('base64'));
var encryptedString = chunks.join('');

这篇关于在节点中加密并在java中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆