有人还说: 根据 crypto_crypto_createdecipher_algorithm_password_options ,现在需要切换到 crypto.createDecipheriv. 示例代码: const crypto = require('crypto');const algorithm ='aes-256-ctr';const ENCRYPTION_KEY ='输入密码_此处';//或生成示例密钥Buffer.from('FoCKvdLslUuB4y3EZlKate7XGottHski1LmyqJHvUhs =','base64');const IV_LENGTH = 16;函数crypto(text){令iv = crypto.randomBytes(IV_LENGTH);let cipher = crypto.createCipheriv(算法,Buffer.from(ENCRYPTION_KEY,'hex'),iv);让加密= cipher.update(text);已加密= Buffer.concat([encrypted,cipher.final()]);返回iv.toString('hex')+':'+ crypto.toString('hex');}函数解密(文本){让textParts = text.split(':');让iv = Buffer.from(textParts.shift(),'hex');让cryptoText = Buffer.from(textParts.join(':'),'hex');让解密= crypto.createDecipheriv(算法,Buffer.from(ENCRYPTION_KEY,'hex'),iv);让解密= decipher.update(encryptedText);解密= Buffer.concat([decrypted,decipher.final()]);返回decrypted.toString();} 对于完整的运行示例,克隆节点-cheat 并运行 node crypto-create-cipheriv.js .I am using following functions to encrypt/decrypt strings in 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;}(password is stored separately from encoded text). New version of nodejs/crypt package complains:(node:5212) [DEP0106] DeprecationWarning: crypto.createDecipher is deprecated.How do I rewrite this to upgrade my source code? 解决方案 So lets say it like:Replace deprecated crypto.createDecipher usage with crypto.createDecipherivwhy? because: according to the deprecation docs it was due to security concerns.Using crypto.createCipher() and crypto.createDecipher() should be avoided as they use a weak key derivation function (MD5 with no salt) and static initialization vectors. It is recommended to derive a key using crypto.pbkdf2() or crypto.scrypt() and to use crypto.createCipheriv() and crypto.createDecipheriv() to obtain the Cipher and Decipher objects respectively.Link to the above reference: Click HereSomeone also said:As per crypto_crypto_createdecipher_algorithm_password_options, one now need to switch to crypto.createDecipheriv.Sample Code: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();}For complete running example clone node-cheat and run node crypto-create-cipheriv.js. 这篇关于如何在Node.js中替换已弃用的crypto.createCipher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 15:38