Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在NodeJs中使用crpyto_Node.js_Cryptojs - Fatal编程技术网

Node.js 如何在NodeJs中使用crpyto

Node.js 如何在NodeJs中使用crpyto,node.js,cryptojs,Node.js,Cryptojs,我正在学习NodeJs,我正在尝试实现加密库来加密和减少文本, 我指的是下面的例子- 要加密的代码如下- var crypto = require('crypto'); var mykey = crypto.createCipher('aes-128-cbc', 'mypassword'); var mystr = mykey.update('abc', 'utf8', 'hex') mystr += mykey.update.final('hex'); console.log(mystr

我正在学习NodeJs,我正在尝试实现加密库来加密和减少文本,
我指的是下面的例子-

要加密的代码如下-

var crypto = require('crypto');

var mykey = crypto.createCipher('aes-128-cbc', 'mypassword');
var mystr = mykey.update('abc', 'utf8', 'hex')
mystr += mykey.update.final('hex');

console.log(mystr); 
var crypto = require('crypto');

var mykey = crypto.createDecipher('aes-128-cbc', 'mypassword');
var mystr = mykey.update('34feb914c099df25794bf9ccb85bea72', 'hex', 'utf8')
mystr += mykey.update.final('utf8');

console.log(mystr);
要解密的代码如下-

var crypto = require('crypto');

var mykey = crypto.createCipher('aes-128-cbc', 'mypassword');
var mystr = mykey.update('abc', 'utf8', 'hex')
mystr += mykey.update.final('hex');

console.log(mystr); 
var crypto = require('crypto');

var mykey = crypto.createDecipher('aes-128-cbc', 'mypassword');
var mystr = mykey.update('34feb914c099df25794bf9ccb85bea72', 'hex', 'utf8')
mystr += mykey.update.final('utf8');

console.log(mystr);
代码示例似乎在其环境中工作,
但是当我试图在我的机器上运行相同的代码时,我遇到了以下错误-

mystr += mykey.update.final('hex');
                      ^

TypeError: mykey.update.final is not a function
    at Object.<anonymous> (/home/user/office/pocs/node-apps/sample-apps/Apps/crypto.js:5:23)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:797:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)
mystr+=mykey.update.final('hex');
^
TypeError:mykey.update.final不是函数
反对。(/home/user/office/pocs/node apps/sample apps/apps/crypto.js:5:23)
at模块编译(内部/modules/cjs/loader.js:734:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:745:10)
在Module.load(内部/modules/cjs/loader.js:626:32)
在tryModuleLoad(内部/modules/cjs/loader.js:566:12)
at Function.Module._load(内部/modules/cjs/loader.js:558:3)
位于Function.Module.runMain(internal/modules/cjs/loader.js:797:12)
在executeUserCode(internal/bootstrap/node.js:526:15)
开始时线程执行(internal/bootstrap/node.js:439:3)
有人能帮我正确实现加密和解密TP文本吗?
我做错了什么?
更新行

mystr += mykey.update.final('hex');

尝试根据

请检查该选项。似乎w3schools.com是错误的。谢谢,它成功了,但代码是否已被弃用?我收到以下警告-“(节点:25470)[DEP0106]弃用警告:crypto.createCipher已弃用。”如何解密此->const crypto=require('crypto');const secret='abcdefg';const hash=crypto.createHmac('sha256',secret.).update('I love cupcakes').digest('hex');console.log(散列);createCipheriv已贬值,请尝试使用createCipheriv