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

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

问题描述

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



以下是我在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.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;
}
}

以下是我在节点中进行加密的方式:

  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 = ,这显然是不同的,因此不会正确解密。



我试图寻找与我有同样问题的人,这个 github问题是我找到的最接近的。正如在这个问题中建议的那样,我尝试运行openssl,如下所示:

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

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

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

这带给我这个问题,我如何使节点输出与我的java代码相同的加密字符串?我只能在节点中更改代码,但不能在java中更改代码。

解决方案

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


// ECB模式不需要IV,所以保持这样,它会很好的。


解决方案如下所示:

  key = new Buffer 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('');


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.

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('');

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.

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=

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

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

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 mode won't need IV, so keep it like this and it will work well.

Here's how the solution looks like:

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天全站免登陆