Node.js 中的 decipher.update() 方法
decipher.update() 用于根据给定的编码格式用接收到的数据更新 decipher。这是由 crypto 模块内的 Decipher 类提供的内置方法之一。如果指定了输入编码,则 data 参数是一个字符串,否则 data 参数是一个缓冲区。
语法
decipher.update(data, [inputEncoding], [outputEncoding])
参数
上述参数如下所述 -
data – 它获取作为输入的数据,该数据用于更新 decipher 内容。
inputEncoding – 它将输入编码作为参数。可能的输入值为十六进制、base64 等。
outputEncoding – 它将输出编码作为参数。此参数的输入类型为字符串。可能的输入值为十六进制、base64 等。
示例
创建一个名为 decipherUpdate.js 的文件,并复制下面的代码段。创建文件后,使用以下命令运行此代码,如以下示例所示 -
node decipherUpdate.js
decipherUpdate.js
// Example to demonstrate the use of decipher.final() method // Importing the crypto module const crypto = require('crypto'); // Initialising the AES algorithm const algorithm = 'aes-192-cbc'; // Initialising the password used for generating key const password = '12345678123456789'; // Retrieving key for the decipher object const key = crypto.scryptSync(password, 'old data', 24); // Initializing the static iv const iv = Buffer.alloc(16, 0); const decipher = crypto.createDecipheriv(algorithm, key, iv); // Initializing the decipher object to get decipher const 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:\home
ode>> node decipherUpdate.js Decrypted value -- Some new text data
示例
让我们看另一个示例。
// Example to demonstrate the use of decipher.final() method // Importing the crypto module const crypto = require('crypto'); // Initialising the AES algorithm const algorithm = 'aes-192-cbc'; // Initialising the password used for generating key const password = '12345678123456789'; // Retrieving key for the decipher object crypto.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:\home
ode>> node decipherUpdate.js Decrypted value:- Some new text data
广告