您好,欢迎访问一九零五行业门户网

Node.js 中的 decipher.update() 方法

decipher.update()用于根据给定的编码格式用接收到的数据更新解密。它是 crypto 模块中的 decipher 类提供的内置方法之一。如果指定了输入编码,则数据参数是字符串,否则数据参数是缓冲区
语法decipher.update(data, [inputencoding], [outputencoding])
参数以上参数描述如下 -
data  – 它需要数据作为传递以更新解密内容的输入。
inputencoding  - 它将输入编码作为参数。可能的输入值为十六进制、base64 等。
outputencoding – 它将输出编码作为参数。该参数的输入类型是字符串。可能的输入值为十六进制、base64 等。
示例创建一个名为 decipherupdate.js 的文件并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 -
node decipherupdate.js
decipherupdate.js
// example to demonstrate the use of decipher.final() method// importing the crypto moduleconst crypto = require('crypto');// initialising the aes algorithmconst algorithm = 'aes-192-cbc';// initialising the password used for generating keyconst password = '12345678123456789';// retrieving key for the decipher objectconst key = crypto.scryptsync(password, 'old data', 24);// initializing the static ivconst iv = buffer.alloc(16, 0);const decipher = crypto.createdecipheriv(algorithm, key, iv);// initializing the decipher object to get decipherconst encrypted = '083bfe1b2f91677e5d00add115be2f1b2e362e190406f5c6b60e86969bf03bff';// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';let decryptedvalue = decipher.update(encrypted, 'hex', 'utf8');// let decryptedvalue1 = decipher.update(encrypted1, 'hex', 'utf8');decryptedvalue += decipher.final('utf8');// printing the result...console.log("decrypted value -- " + decryptedvalue);// console.log("base64 string:- " + base64value)
输出c:\homeode>> node decipherupdate.jsdecrypted value -- some new text data
示例让我们再看一个示例。
// example to demonstrate the use of decipher.final() method// importing the crypto moduleconst crypto = require('crypto');// initialising the aes algorithmconst algorithm = 'aes-192-cbc';// initialising the password used for generating keyconst password = '12345678123456789';// retrieving key for the decipher objectcrypto.scrypt(password, 'salt', 24, { n: 512 }, (err, key) => { if (err) throw err; // initializing the static iv const iv = buffer.alloc(16, 0); // initializing the decipher with algo, key and iv const decipher = crypto.createdecipheriv(algorithm, key, iv); const encrypted = '91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074'; //getting the decrypted string value const decrypted = decipher.update(encrypted, 'hex', 'utf8'); // printing the result... console.log("decrypted value:- " + decrypted);});
输出c:\homeode>> node decipherupdate.jsdecrypted value:- some new text data
以上就是node.js 中的 decipher.update() 方法的详细内容。
其它类似信息

推荐信息