当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Decipher.final()用法及代码示例


decipher.final() 用于返回包含解密对象值的缓冲区或字符串。它是加密模块中的类 Cipher 提供的内置方法之一。一旦 decipher.final 方法被调用, decipher 方法就不能用于解密数据。多次调用 cipher.final 方法将引发错误。

用法

decipher.final([outputEncoding])

参数

上述参数描述如下 -

  • outputEncoding - 它将输出编码作为参数。此参数的输入类型是字符串。可能的输入值为十六进制、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\node>> 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\node>> node decipherFinal.js
Welcome to tutor ials point

相关用法


注:本文由纯净天空筛选整理自Mayank Agarwal大神的英文原创作品 Decipher.final() Method in Node.js。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。