node.js - NodeJS:createCipheriv 应该提供与之前的 createCipher 相同的输出
问题描述
目前,我正在使用旧版本的 crypto.js 来加密和解密字符串。
以下是我的加密代码 -
const encrypt = (password, algorithm, encMethod) => (value) => {
const cipher = crypto.createCipher(algorithm, password);
return cipher.update(JSON.stringify(value), 'utf8', encMethod)
+ cipher.final(encMethod);
};
使用上面的代码我的字符串(E-mailID)
p1@yopmail.com
被转换为
29c68f3bad0068c44122e734367f64557112e058c8222e3fd3908e68402ce6d5
现在createCipher
不推荐使用我应该怎么做createCipheriv
才能提供与上面相同的输出。
我试图通过null
函数IV
,createCipheriv
但我得到了错误Missing IV for cipher aes-256-cbc-hmac-sha1
解决方案
是的,最后我使用以下一些技巧解决了同样的问题 -
const bytesToKey = require('evp_bytestokey');
const encryptionToken = bytesToKey(SomePasswordString, null, 256, 16);
//Ciphering
const cipher = crypto.createCipheriv('aes-256-cbc-hmac-sha1', encryptionToken.key, encryptionToken.iv);
return cipher.update(JSON.stringify(##VALUEtoENCRYPT##), 'utf8', 'hex') + cipher.final('hex');
//De-Ciphering
const decipher = crypto.createDecipheriv('aes-256-cbc-hmac-sha1', encryptionToken.key, encryptionToken.iv);
return JSON.parse(decipher.update(##VALUEtoDECRYPT##, 'hex', 'utf8') + decipher.final('utf8'));
感谢您@topaco
建议我使用 NPM 包evp_bytestokey
推荐阅读
- amazon-web-services - 如何获得从 AWS Lambda 到 Redis 的安全访问?是否需要 VPC?
- javascript - 如何从功能组件调用调度程序?
- android - 在启动、安装和重新启动时初始化应用程序?
- arrays - 扩展 Tableview 部分。检查部分是否扩展的问题
- c - 命令访问不存在的文件时弹出错误状态
- pyspark - 带有条件的 PySpark DataFrame 窗口分区
- google-cloud-platform - 实体的属性可以是 Google Cloud Datastore 中的另一个实体吗?
- google-sheets - 将一系列多列和多行列出到单个列中
- python - 如何修复“TypeError:无法将字典更新序列元素 #0 转换为序列”
- performance - 为什么这个简单的 Haskell 程序这么慢?