javascript - 如何破译 Node js中在javascript中的加密js中加密的字符串

标签 javascript node.js express cryptojs

我的客户端代码:

data.username = CryptoJS.AES.encrypt(user.username, "password");
data.password = CryptoJS.AES.encrypt(user.password, "password");

然后我将“数据”发送到服务器,即express.js

var user = req.body;
var decipher = crypto.createDecipher('aes256', "password");
var decrypted = decipher.update(user.username, 'hex', 'utf-8');
decrypted += decipher.final('utf-8'); 

我收到此错误:

Error: DecipherInit error
at new Decipher (crypto.js:368:17)
at Object.Decipher (crypto.js:365:12)

最佳答案

CryptoJS 的带有密码的 encrypt 函数使用与 Node.js 的 createCipher 相同的 EVP_BytesToKey 函数,其重要区别在于 CryptoJS 使用随机盐来导出,而 node does not (强调我的):

Note: createCipher derives keys with the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt.

要么直接在 Node 中使用 CryptoJS(这是可能的),因为 CryptoJS 没有任何依赖项,要么您自己在两端进行 key 派生并使用 crypto.createCipheriv。如果您选择前者,则必须另外将用户名和密码加密的盐传递给 Node 。

请注意,data.username 是包含盐和 IV 的 CryptoJS cipherParams 对象,但是当您使用 data.username.toString() 将其转换为字符串时,不再包含盐,但包含 IV。这不是您要放入 Node.js 函数中的数据。改为发送 data.username.ciphertext

关于javascript - 如何破译 Node js中在javascript中的加密js中加密的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359128/

相关文章:

javascript - 将参数从 Node.JS 传递到 HTML 文件

node.js - 如何为主 Handlebars 布局设置变量而不将其传递给每条路线?

javascript - 单击更改 DIV 的样式并在取消单击时再次更改

javascript - 使用 cheerio 检索 href

node.js - Sequelize 配置以检索带有详细信息的总数

Node.js + Express.js + Passport.js - 我需要一些如何构建它们的建议

javascript - firebase.auth().createUserWithEmailAndPassword Undefined 不是函数

javascript - 带有 express 的 socket.io 更新 DOM 中的变量

javascript - 简单的 Nivo slider 图像无法在 IE 上加载

javascript - 调整窗口大小时BJQS slider 的多个实例