Node.js 关于nodejs AES-128-GCM加密后对方无法解析密文的问题

Node.js 关于nodejs AES-128-GCM加密后对方无法解析密文的问题,node.js,Node.js,根据项目要求,一组请求数据需要使用AES-128-GCM进行加密,但得到的加密数据总是不被对方解析,当我使用Golang编写加密算法时,加密数据可以被对方正确解析 function Encrypt(text, key) { if (!text) { return ''; } if (typeof text != 'string') { text = JSON.stringify(text); } const md

根据项目要求,一组请求数据需要使用AES-128-GCM进行加密,但得到的加密数据总是不被对方解析,当我使用Golang编写加密算法时,加密数据可以被对方正确解析

function Encrypt(text, key) {
    
    if (!text) {
        return '';
    }
    if (typeof text != 'string') {
        text = JSON.stringify(text);
    }

    const md5 = crypto.createHash('md5');
    
    const result = md5.update(key).digest();

    const iv = crypto.randomBytes(12);


    const cipher = crypto.createCipheriv('aes-128-gcm', result, iv);
    cipher.setAutoPadding(0);
    const encrypted = cipher.update(text, 'utf8');

    const finalstr = cipher.final();

    const tag = cipher.getAuthTag();

    const res = Buffer.concat([encrypted, finalstr, tag]);

    return  res.toString('base64');
    
}
上面是nodejs的加密代码,该密钥在“12f41deed45188c8061c840c643baede”中传递

现在我可以证明对方解析密文没有问题,那么问题只能是nodejs的加密码有问题,请帮我看看大家伙,帮我解决问题


以下是golang的加密代码除了auth标记外,我们还需要将IV与加密数据一起包括在内,否则另一方将无法解密它

我已经更新以匹配Golang代码,因此Node.js Encrypt应该使用相同的代码进行解密

我还添加了一个在Node.js中解密数据的示例,这可能有助于诊断任何问题

NB:由于IV是随机的,我们不会得到与Golang代码相同的精确输出(或在调用之间),但它应该正确解密

const crypto = require("crypto");

function Encrypt(text, key) {
    
    if (!text) {
        return '';
    }
    if (typeof text != 'string') {
        text = JSON.stringify(text);
    }

    const cipherKey = Buffer.from(key, "hex");
    const iv = crypto.randomBytes(12);
    const cipher = crypto.createCipheriv('aes-128-gcm', cipherKey, iv);

    const res = Buffer.concat([iv, cipher.update(text), cipher.final(), cipher.getAuthTag()]);
    return res.toString('base64');    
}

function Decrypt(input, key) {
    
    input = input instanceof Buffer ? input: Buffer.from(input, "base64");
    const tag = input.slice(input.length - 16, input.length);
    const iv = input.slice(0, 12);
    const toDecrypt = input.slice(12, input.length - tag.length);

    const cipherKey = Buffer.from(key, "hex");
    const decipher = crypto.createDecipheriv('aes-128-gcm', cipherKey, iv);
    decipher.setAuthTag(tag);

    const res = Buffer.concat([decipher.update(toDecrypt), decipher.final()]);
    return res.toString('utf8');
}

const key = "12f41deed45188c8061c840c643baede"
const encrypted = Encrypt("Why then 'tis none to you; for there is nothing either good or bad, but thinking makes it so.", key);

console.log("Encrypted data:", encrypted);
console.log("Decrypted:", Decrypt(encrypted, key));

您能否举例说明Golang加密(文本、密钥等)的输入和生成的加密数据?如果您能做到这一点,我们就可以理解Node.js代码的错误所在。或者,更好的办法是发布Golang代码。非常感谢。我已经在问题中添加了golang代码非常感谢您的回复,谢谢您的代码,但是使用您的代码后,问题仍然没有解决,对方仍然无法正确解密,nodejs的加密库和其他语言的加密库有什么区别吗?您是否尝试使用此代码成功地用其他语言进行加密和解密?感谢您发布golang代码。Node.js crypto库应产生与其他语言相同的结果,前提是加密过程的所有输入和选项都相同,例如填充、模式等。这可能很难实现!最好的方法是将加密输出与其他语言进行比较。我已经更新了Node.js代码,希望能够与另一方的代码一起工作。再次感谢您的回复,在使用您的代码后,问题已经得到完美解决,非常感谢,您学到了很多东西,祝您生活愉快!谢谢你测试我的答案,祝你项目顺利!!