【问题标题】:How do I replace deprecated crypto.createCipher in Node.js?如何替换 Node.js 中已弃用的 crypto.createCipher?
【发布时间】:2020-06-07 16:24:23
【问题描述】:

我在 Node.js 中使用以下函数来加密/解密字符串:

var crypto = require('crypto');
var algorithm = 'aes-256-ctr';
function encrypt(text) {
    var cipher = crypto.createCipher(algorithm, password);
    try {
        var crypted = cipher.update(text, 'utf8', 'hex');
        crypted += cipher.final('hex');
    } catch (e) {
        return;
    }
    return crypted;
}

function decrypt(text) {
    var decipher = crypto.createDecipher(algorithm, password);
    try {
        var dec = decipher.update(text, 'hex', 'utf8');
        dec += decipher.final('utf8');
    } catch (e) {
        return;
    }
    return dec;
}

(密码与编码文本分开存储)。新版nodejs/crypt包报错:

(node:5212) [DEP0106] DeprecationWarning: crypto.createDecipher is deprecated.

如何重写它来升级我的源代码?

【问题讨论】:

    标签: node.js encryption


    【解决方案1】:

    我们可以这样说:

    crypto.createDecipheriv替换不推荐使用的crypto.createDecipher

    为什么?因为:

    根据deprecation 文档,这是出于安全考虑。

    使用crypto.createCipher()crypto.createDecipher() 应避免使用,因为它们使用弱密钥派生函数(没有盐的MD5)和静态初始化向量。建议使用crypto.pbkdf2()crypto.scrypt()派生密钥,并使用crypto.createCipheriv()crypto.createDecipheriv()分别获取Cipher和Decipher对象。

    以上参考链接:Click Here

    也有人说:

    根据crypto_crypto_createdecipher_algorithm_password_options,现在需要切换到crypto.createDecipheriv

    示例代码:

    const crypto = require('crypto');
    const algorithm = 'aes-256-ctr';
    const ENCRYPTION_KEY = 'Put_Your_Password_Here'; // or generate sample key Buffer.from('FoCKvdLslUuB4y3EZlKate7XGottHski1LmyqJHvUhs=', 'base64');
    const IV_LENGTH = 16;
    
    function encrypt(text) {
        let iv = crypto.randomBytes(IV_LENGTH);
        let cipher = crypto.createCipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
        let encrypted = cipher.update(text);
        encrypted = Buffer.concat([encrypted, cipher.final()]);
        return iv.toString('hex') + ':' + encrypted.toString('hex');
    }
    
    function decrypt(text) {
        let textParts = text.split(':');
        let iv = Buffer.from(textParts.shift(), 'hex');
        let encryptedText = Buffer.from(textParts.join(':'), 'hex');
        let decipher = crypto.createDecipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
        let decrypted = decipher.update(encryptedText);
        decrypted = Buffer.concat([decrypted, decipher.final()]);
        return decrypted.toString();
    }
    

    如需完整运行示例,请克隆 node-cheat 并运行 node crypto-create-cipheriv.js

    【讨论】:

    • 好的,现在查看更新的答案,它既有代码又有节点作弊链接。 @StepanYakovenko
    • 它将触发错误 (node:28641) UnhandledPromiseRejectionWarning: E​​rror: Invalid key length
    • @Codebrekers 这意味着您没有提供正确的 ENCRYPTION_KEY。请仔细阅读此行以使其正常工作:github.com/zishon89us/node-cheat/blob/master/…
    • @Codebrekers 问题是密钥需要 32 字节长。为了实现这一点(无论 ENCRYPTION_KEY 的大小如何,填充或缩小到 32 个字节),您可以使用Buffer.concat([Buffer.from(ENCRYPTION_KEY), Buffer.alloc(32)], 32) 作为createCipheriv()createDecipheriv() 的第二个参数
    • 您可以使用scrypt(secret, salt, 24, (err, key) => { // cipher/decipher here }) 来确保您的密钥长度适合您的算法。例如。 aes192 = 24 字节(192 位)。如果可能,您应该使用独特的盐。
    猜你喜欢
    • 2023-04-07
    • 2021-05-08
    • 2020-02-27
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多