【发布时间】:2016-12-18 01:15:31
【问题描述】:
我用 Node.js 开发服务器,用 Ionic 框架开发客户端
我为来自客户端的登录请求创建了 API
当客户端请求登录时,发送加密的 id 和密码字符串
服务器解密收到的id和密码字符串并检查验证
我使用 crypto-js(https://code.google.com/archive/p/crypto-js/) 库进行客户端加密
客户端加密代码如下
var secret = 'abcdefghijklmnopqrstuvwxyz123456';
var id = "someId";
var encrypted = CryptoJS.AES.encrypt(id, password);
console.log(encrypted.toString()); // U2FsdGVkX19EfjjBwydSZL509wKl5TEX+4f3vakEejU=
对于服务器端解密,我使用了节点内置的加密模块
const crypto = require('crypto');
var method = 'aes256';
var secret = 'abcdefghijklmnopqrstuvwxyz123456';
var id = "U2FsdGVkX19EfjjBwydSZL509wKl5TEX+4f3vakEejU=" // suppose we received with no loss
var decipher = crypto.createDecipher(method, secret);
decipher.update(id,'base64','utf8');
var deciphered = decipher.final('utf8');
console.log(deciphered);
服务器端解密代码崩溃,错误消息如下
crypto.js:153
var ret = this._handle.final();
^
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Error (native)
at Decipher.Cipher.final (crypto.js:153:26)
at Object.<anonymous> (...\routes\index.js:33:27)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (...\app.js:18:14)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
由于错误消息是“错误解密”,所以我尝试使用每个库加密相同的文本
[crypto-js]
var secret = 'abcdefghijklmnopqrstuvwxyz123456';
var id = "someId";
var encrypted = CryptoJS.AES.encrypt(id, password);
console.log(encrypted.toString()); // U2FsdGVkX19EfjjBwydSZL509wKl5TEX+4f3vakEejU=
[加密模块]
const crypto = require('crypto');
var method = 'aes256';
var secret = 'abcdefghijklmnopqrstuvwxyz123456';
var id = "someId"
var cipher= crypto.createCipher(method, secret);
cipher.update(id,'base64','utf8');
var ciphered = decipher.final('utf8');
console.log(ciphered.toString()); // WAsd61C2bfG7UbO5STo13A==
我发现库的结果不同
plain text : 'someId'
crpyto-js : 'U2FsdGVkX19EfjjBwydSZL509wKl5TEX+4f3vakEejU='
crpyto module : 'WAsd61C2bfG7UbO5STo13A=='
我试图了解每个库的源代码
但是太复杂了,看不懂
我想知道每个库的加密是如何工作的以及导致不同结果的原因
【问题讨论】:
-
我的这两个答案处理两个方向:CryptoJS to node.js 和node.js to CryptoJS
-
您没有使用有效的 AES 密钥,因此两个系统都试图使其长度正确,显然采用不同的策略。
-
@dandavis 这正是不同之处。两者都没有使用任何密钥,但他们假设传递的“密钥”是密码,然后从密码中派生出实际的密钥。唯一的区别是 CryptoJS 使用随机盐,而 node.js 没有。这就是我在第一个链接背后的答案所说的。遗憾的是,我不能提出重复的建议。
标签: node.js encryption cryptojs