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