我想在前端(vue)登录页将密码加密,发送到后端(node)进行解密,但是报错;
我在node端用相同的key加密了密码,然后可以解密,而且和js加密过的不一样,这是什么原因?
// 加密
var key = _self.pwKey;
var word = _self.form.password;
var encrypt = CryptoJS.AES.encrypt(word, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
_self.encryptPW = encrypt.toString();
// 加密
function aesEncrypt(data, key) {
const cipher = crypto.createCipher('aes-256-ecb', key);
cipher.setAutoPadding(true);
var crypted = cipher.update(data, 'utf8', 'base64');
crypted += cipher.final('base64');
return crypted;
}
// 解密
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher('aes-256-ecb', key);
var decrypted = decipher.update(encrypted, 'base64', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
var data = '$admin123456';
var key = 'n3JydWaRTRFa7wp7pF4PFbHHYmTrynpH';
var encrypted = aesEncrypt(data, key);
var decrypted = aesDecrypt(encrypted, key);
console.log('Encrypted text: ' + encrypted);
console.log('Decrypted text: ' + decrypted);
是那写错了吗?
aes
还分ECB
,CBC
... 好多种的,可以看看aes加密解密这个站点上的,用的也是crypto-js
,它就分了5种。可以测试比对下结果。