我如何在nodejs中像这样从java解密加密值?

我如何在nodejs中像这样从java解密加密值?,java,node.js,encryption,aes,Java,Node.js,Encryption,Aes,我试图从nodejs上的java代码中解密加密值 const crypto = require('crypto'); const key = 'somekeysomekey+)'; const algorithm = 'aes-128-ecb'; function decryptFunc(encryptedStr) { const base64Decoded = new Buffer(encryptedStr, 'base64').toString('binary'); con

我试图从nodejs上的java代码中解密加密值

const crypto = require('crypto');
const key = 'somekeysomekey+)';
const algorithm = 'aes-128-ecb';

function decryptFunc(encryptedStr) {
    const base64Decoded = new Buffer(encryptedStr, 'base64').toString('binary');
    const decipher = crypto.createDecipher(algorithm, key);
    decipher.setAutoPadding(false);

    let result = decipher.update(base64Decoded, 'binary', 'binary');
    result += decipher.final('binary');

    return new Buffer(result).toString('utf8');
};
下面是来自java的加密方法

public class EncryptUtil {
    public static String getKey()
    {
        return "somekeysomekey+)"; //key length 16!Use this on nodejs
    }

    public static String encryptAES(String ID) throws Exception {

        Key secretKeySpec = new SecretKeySpec(getKey().getBytes(), "AES"); 


        String transform = "AES/ECB/ISO10126Padding";
        String output = "";

        try {
            javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(transform);
            cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, secretKeySpec);
            String originStr = ID;

            byte[] input = originStr.getBytes("UTF8");
            byte[] output = cipher.doFinal(input);
            sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
            output = encoder.encode(output);
        } catch (Exception e) {
            System.out.println("ERROR: " + e.getMessage());
        }
        return output;
    }
}

//This is how to encrypt from above
//String encryptText = (String) EncryptUtil.encryptAES("something");
//System.out.println(encryptText) ---> "47gPeqm+0lvKb0VNXF29yQ==";
这是我在nodejs上解密上述结果的代码

const crypto = require('crypto');
const key = 'somekeysomekey+)';
const algorithm = 'aes-128-ecb';

function decryptFunc(encryptedStr) {
    const base64Decoded = new Buffer(encryptedStr, 'base64').toString('binary');
    const decipher = crypto.createDecipher(algorithm, key);
    decipher.setAutoPadding(false);

    let result = decipher.update(base64Decoded, 'binary', 'binary');
    result += decipher.final('binary');

    return new Buffer(result).toString('utf8');
};
但是我的代码不起作用。。
如何在nodejs上解密?。

多亏了“”模块,我解决了这个问题


“不工作”是什么意思?抛出异常、返回错误的结果或其他内容?您的加密模式是否相同?是否尝试了
新缓冲区(结果为“二进制”)
<代码>'utf8'是默认的字符串编码。此外,您应该避免使用
“二进制”
字符串编码,而直接使用缓冲区。最后,使用
Buffer.from()
而不是
new Buffer()
。还有一件事:ECB模式很糟糕,要不惜一切代价避免它。@talex我知道我的函数完全错误。。这只是我一直在努力破解的问题之一。我的问题的重点是如何在node js(使用crypto module)上通过上述java代码对加密结果进行解密