Node.js 中的 Decipher.final() 方法
所述用于返回包含解码对象的值的缓冲或字符串。它是加密模块中的类Cipher提供的内置方法之一。一旦方法被调用,解密方法就不能用于解密数据。多次调用该方法将引发错误。decipher.final()decipher.finalcipher.final
语法
decipher.final([outputEncoding])
参数
上述参数描述如下-
outputEncoding –它将输出编码作为参数。此参数的输入类型是字符串。可能的输入值为十六进制、base64等。
示例
创建一个具有名称的文件-decipherFinal.js并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示-
node decipherFinal.js
decipherFinal.js
//演示cipher.final()方法使用的示例
//导入加密模块
const crypto = require('crypto');
//初始化AES算法
const algorithm = 'aes-192-cbc';
//初始化用于生成密钥的密码
const password = '12345678123456789';
//检索解密对象的密钥
const key = crypto.scryptSync(password, 'old data', 24);
//初始化静态iv
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
//初始化密码对象以获取密码
const encrypted1 =
'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a';
//constencrypted2='8d11772fce59f08e7558db5bf17b3112';
let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');
//让decryptedValue1=decipher.update(encrypted1,'hex','utf8');
decryptedValue1 += decipher.final('utf8');
//打印结果...
console.log("Decrypted value -- " + decryptedValue1);
// console.log("Base64 String:- " + base64Value)输出结果C:\home\node>> node decipherFinal.js Decrypted value -- Welcome to tutorials point
示例
让我们再看一个例子。
//演示cipher.final()方法使用的示例
//导入加密模块
const crypto = require('crypto');
//初始化AES算法
const algorithm = 'aes-192-cbc';
//初始化用于生成密钥的密码
const password = '12345678123456789';
//检索解密对象的密钥
const key = crypto.scryptSync(password, 'old data', 24);
//初始化静态iv
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
//初始化密码对象以获取密码
const encrypted =
'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a';
//constencrypted2='8d11772fce59f08e7558db5bf17b3112';
var buf = [];
//Updatingthedecopherdata
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
//Pushinfthedataintobufferafterdecryption
buf.push(decrypted);
buf.push(decipher.final('utf8'));
//Printingtheresult
console.log(buf.join(' '));输出结果C:\home\node>> node decipherFinal.js Welcome to tutor ials point