代码如下:
var kk = JSON.stringify(object);
console.log(kk);
var kk1 = encrypt(kk);
console.log(kk1)
var kk2 = decrypt(kk1);
console.log(kk2)
this.write(encrypt(kk))
函数:
var encrypt = function (data) {
var cipher = crypto.createCipher('aes-256-ecb', password)
cipher.update(data, 'utf8')
return cipher.final('hex')
}
var decrypt = function (data) {
var cipher = crypto.createDecipher('aes-256-ecb', password)
cipher.update(data, 'hex')
return cipher.final('utf8')
}
控制台消息:
{"action":"ping","ping":30989}
4613a3a8719c921eed61e19b7480de9c
,"ping":30989}
为什么解密没有得到初始字符串?
最佳答案
.update()
返回部分加密/解密的内容,您将立即丢弃该数据。您还缺少 .update()
的输出编码,它与您在 .final()
中使用的编码相匹配。试试这个:
function encrypt(data) {
var cipher = crypto.createCipher('aes-256-ecb', password);
return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}
function decrypt(data) {
var cipher = crypto.createDecipher('aes-256-ecb', password);
return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}
关于javascript - Node.JS Crypto Cipher/Decipher 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25710552/