我的客户端代码:
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
函数使用相同的 EVP_BytesToKey
函数 Node.js' createCipher
,重要的区别在于 CryptoJS 使用随机盐来导出,而 node 则不(强调我的):
注意:createCipher 使用 OpenSSL 函数 EVP_BytesToKey 导出密钥,摘要算法设置为 MD5,一次迭代,并且无盐。
您可以直接在节点中使用 CryptoJS,这是可能的,因为 CryptoJS 没有任何依赖项,或者您自己在两端进行密钥派生并使用
crypto.createCipheriv
。如果您选择前者,则必须另外将用户名和密码加密的盐传递给节点。
请注意,
data.username
是包含盐和IV的CryptoJS cipherParams对象,但是当您使用data.username.toString()
将其转换为字符串时,不再包含盐,但包含IV。这不是您要放入 node.js 函数中的 data
。改为发送 data.username.ciphertext
。