Decipher.final() 方法在 Node.js 中
decipher.final() 用于返回包含 decipher 对象值的缓冲区或字符串。它是 crypto 模块中 Cipher 类提供的内置方法之一。一旦调用了 decipher.final 方法,就不能再使用 decipher 方法来解密数据。多次调用 cipher.final 方法将抛出错误。
语法
decipher.final([outputEncoding])
参数
以上参数描述如下 -
outputEncoding – 将输出编码作为参数。此参数的输入类型为字符串。可能的输入值为 hex、base64 等。
范例
创建一个名为 decipherFinal.js 的文件,并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 -
node decipherFinal.js
decipherFinal.js
// Example to demonstrate the use of cipher.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 cipher object to get cipher const encrypted1 = 'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a'; // const encrypted2 = '8d11772fce59f08e7558db5bf17b3112'; let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8'); // let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8'); decryptedValue1 += decipher.final('utf8'); // Printing the result... console.log("Decrypted value -- " + decryptedValue1); // console.log("Base64 String:- " + base64Value)
输出
C:\home
ode>> node decipherFinal.js Decrypted value -- Welcome to tutorials point
范例
我们来看一个再看一个例子。
// Example to demonstrate the use of cipher.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 cipher object to get cipher const encrypted = 'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a'; // const encrypted2 = '8d11772fce59f08e7558db5bf17b3112'; var buf = []; // Updating the decopher data let decrypted = decipher.update(encrypted, 'hex', 'utf8'); // Pushinf the data into buffer after decryption buf.push(decrypted); buf.push(decipher.final('utf8')); // Printing the result console.log(buf.join(' '));
输出
C:\home
ode>> node decipherFinal.js Welcome to tutor ials point
广告