我正在使用以下函数来加密/解密 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.
如何重写它来升级我的源代码?
所以我们可以这么说:
将已弃用的
crypto.createDecipher
用法替换为 crypto.createDecipheriv
为什么?因为:
根据弃用文档,这是出于安全考虑。
应避免使用
crypto.createCipher()
和 crypto.createDecipher()
,因为它们使用弱密钥导出函数(无盐 MD5)和静态初始化向量。建议使用 crypto.pbkdf2()
或 crypto.scrypt()
导出密钥,并使用 crypto.createCipheriv()
和 crypto.createDecipheriv()
分别获取 Cipher 和 Decipher 对象。
上述参考链接:单击此处
也有人说:
根据 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
。
createDecipheriv 现已弃用。稳定性:0 - 已弃用:使用 crypto.createCipheriv() 代替。人们可以轻松地用 createCipheriv 替换它
const crypto = require('crypto');
const ENC= 'bf3c199c2470cb477d907b1e0917c17b';
const IV = "5183666c72eec9e4";
const ALGO = "aes-256-cbc"
const encrypt = ((text) =>
{
let cipher = crypto.createCipheriv(ALGO, ENC, IV);
let encrypted = cipher.update(text, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
});
const decrypt = ((text) =>
{
let decipher = crypto.createDecipheriv(ALGO, ENC, IV);
let decrypted = decipher.update(text, 'base64', 'utf8');
return (decrypted + decipher.final('utf8'));
});
const encrypted_key = encrypt("HelloWorld");
const decrypted_key = decrypt(encrypted_key);
您也可以使用createCipheriv
[![
`const crypto = require('crypto');
var encryptionDecryption = {};
encryptionDecryption.Encryption = async (data, key) =>{
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(data, 'utf8', 'base64');
encrypted += cipher.final('base64');
return {
iv: iv.toString('base64'),
encryptedData: encrypted
};
}
encryptionDecryption.Decryption = async (encryptedData, key, iv) =>{
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), Buffer.from(iv, 'base64'));
let decrypted = decipher.update(encryptedData, 'base64', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
module.exports=encryptionDecryption;
// driver code
// calling this function MENU use this function in API if you call here then //it says cyclic dependencies
const encrypted = encrypt(data, key);
console.log('Encrypted:', encrypted);
const decrypted = decrypt(encrypted.encryptedData, key, encrypted.iv);
console.log('Decrypted:', decrypted);`
为了执行此操作,请将其作为任何节点/javascript编译器中的函数