如何替换Node.js中不推荐使用的crypto.createCipher?

如何替换Node.js中不推荐使用的crypto.createCipher?,node.js,encryption,Node.js,Encryption,我正在使用以下函数加密/解密Node.js中的字符串: var crypto=require('crypto'); var算法='aes-256-ctr'; 函数加密(文本){ var cipher=crypto.createCipher(算法、密码); 试一试{ var crypted=cipher.update(文本“utf8”、“hex”); crypted+=cipher.final('hex'); }捕获(e){ 返回; } 返回加密; } 函数解密(文本){ var decipher

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

var crypto=require('crypto');
var算法='aes-256-ctr';
函数加密(文本){
var cipher=crypto.createCipher(算法、密码);
试一试{
var crypted=cipher.update(文本“utf8”、“hex”);
crypted+=cipher.final('hex');
}捕获(e){
返回;
}
返回加密;
}
函数解密(文本){
var decipher=crypto.createDecipher(算法、密码);
试一试{
var dec=解密.更新(文本'hex','utf8');
dec+=最终破译('utf8');
}捕获(e){
返回;
}
返回12月;
}
(密码与编码文本分开存储)。新版nodejs/crypt包投诉:

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

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

所以我们可以这样说:

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();
}
将不推荐的
crypto.createDecipher
用法替换为
crypto.createDecipheriv

为什么?因为:

根据文件,这是出于安全考虑

应避免使用
crypto.createCipher()
crypto.createDecipher()
,因为它们使用弱密钥派生函数(不含盐的MD5)和静态初始化向量。建议使用
crypto.pbkdf2()
crypto.scrypt()
导出密钥,并使用
crypto.createCipheriv()
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();
}
根据,现在需要切换到
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();
}

有关完整的运行示例克隆并运行
节点加密创建cipheriv.js

好的,请参阅更新的答案,现在它有代码和节点欺骗链接@StepanYakovenkoIt将触发错误(节点:28641)未处理PromiserEjectionWarning:错误:无效密钥length@Codebrekers这意味着您没有提供正确的加密密钥。请仔细阅读这一行以使其正常工作:@Codebrekers问题是密钥需要32字节长。要实现这一点(无论加密密钥大小,填充或收缩到32字节),您可以使用
Buffer.concat([Buffer.from(ENCRYPTION\u KEY),Buffer.alloc(32)],32)
作为
createCipheriv()
createDecipheriv()
的第二个参数,您可以使用
scrypt(secret,salt,24,(err,KEY)=>{//cipher/decipheriv here})
以确保您拥有适合算法长度的密钥。例如,aes192=24字节(192位)。如果可能的话,你应该使用一种独特的盐。