node.js - 尝试在Cipher.update中以不受支持的状态添加数据

标签 node.js cryptojs

下面的代码正在工作

var crypto = require('crypto');
var cipher = crypto.createCipher('aes-128-cbc','abcdefghijklmnop')
var http = require('http')

var userStr = 'a134aad';
var crypted = cipher.update(userStr, 'utf8', 'hex');
crypted += cipher.final('hex');
console.log(crypted);

但是,当将其放入服务器回调中时,它将无法正常工作,并且在请求到达且 Node 被压碎时会在err以下抛出:
http.createServer(function(req, res){
    var userStr = 'a134aad';
    var crypted = cipher.update(userStr, 'utf8', 'hex');
    crypted += cipher.final('hex');
    console.log(crypted);

    res.end('hello');
}).listen(9888)

---------------------------------

7364aee753f0568f7e5171add6868b75
crypto.js:170
  var ret = this._handle.update(data, inputEncoding);
                         ^
Error: Trying to add data in unsupported state
    at Cipher.update (crypto.js:170:26)
    at Server.<anonymous> (C:\Users\58\Desktop\sha256.js:12:26)
    at emitTwo (events.js:126:13)
    at Server.emit (events.js:214:7)
    at parserOnIncoming (_http_server.js:602:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:117:23)

最佳答案

原来
var cipher = crypto.createCipher('aes-128-cbc','abcdefghijklmnop')
不应重复使用。我也将其放入服务器回调中,问题已解决。

关于node.js - 尝试在Cipher.update中以不受支持的状态添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51280576/

相关文章:

javascript - 使用 Jest 测试 Typescript,如何测试内部函数?我的导入/导出失败了

node.js - 我如何通过 NodeJs 在谷歌云中设置数据存储索引配置?

javascript - 两人 - 回合制游戏

javascript - 文件与文件内容的不同 sha256 哈希值作为 WordArray

javascript - Javascript JSON.stringify 和 PHP json_encode 一样吗?

node.js - 如何调试 Node.js 服务器?调试器跳过我的断点! (使用VSCode)

node.js - 通过 mongoose 流导出到 node-csv

javascript - 使用 CryptoJS 更改 key

java - 在 Node 中加密文件并使用 "AES/GCM/NoPadding"在 java 中解密。基本上 AES/GCM/NoPadding 等价于 Node.js

javascript - 使用 AES 时,Crypto-js 每次运行时都会返回不同的值