📜  Node.js decipher.final() 方法

📅  最后修改于: 2022-05-13 01:56:27.961000             🧑  作者: Mango

Node.js decipher.final() 方法

decipher.final()方法是加密模块中 Decipher 类的内置应用程序编程接口,用于返回包含 decipher 对象值的缓冲区。

句法:

const decipher.final([outputEncoding])

参数:此方法将输出编码作为参数。

返回值:此方法返回包含解密值的缓冲区对象。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// decipher.final() method
 
// Importing crypto module
const crypto = require('crypto');
 
// Creating and initializing algorithm and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
 
// Getting key for the decipher object
const key = crypto.scryptSync(password, 'salt', 24);
 
// Creating and initializing the static iv
const iv = Buffer.alloc(16, 0);
 
// Creating and initializing the decipher object
const decipher = crypto.createDecipheriv(algorithm, key, iv);
 
// Encrypted using same algorithm, key and iv.
const encrypted =
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
 
// Updating the data to tbe decipher 
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
// Deciphering data by using
// final() method
decrypted += decipher.final('utf8');
 
// Display the result
console.log(decrypted);


JavaScript
// Node.js program to demonstrate the
// decipher.final() method
 
// Importing crypto module
const crypto = require('crypto');
 
// Creating and initializing algorithm and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
 
// Getting key for the decipher object
const key = crypto.scryptSync(password, 'salt', 24);
 
// creating and initializing the static iv
const iv = Buffer.alloc(16, 0);
 
// creating and initializing the decipher object
const decipher = crypto.createDecipheriv(algorithm, key, iv);
 
// Encrypted using same algorithm, key and iv.
const encrypted =
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
 
// Creating and initializing empty buffer
var buf = [];
 
// Updating the data to tbe decipher
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
// Pushing the updated data into buffer
buf.push(decrypted);
 
// Pushing decrypted data into buffer
buf.push(decipher.final('utf8'));
 
// Display the result
console.log(buf.join(' '));


输出:

some clear text data

示例 2:文件名:index.js

JavaScript

// Node.js program to demonstrate the
// decipher.final() method
 
// Importing crypto module
const crypto = require('crypto');
 
// Creating and initializing algorithm and password
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
 
// Getting key for the decipher object
const key = crypto.scryptSync(password, 'salt', 24);
 
// creating and initializing the static iv
const iv = Buffer.alloc(16, 0);
 
// creating and initializing the decipher object
const decipher = crypto.createDecipheriv(algorithm, key, iv);
 
// Encrypted using same algorithm, key and iv.
const encrypted =
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
 
// Creating and initializing empty buffer
var buf = [];
 
// Updating the data to tbe decipher
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
 
// Pushing the updated data into buffer
buf.push(decrypted);
 
// Pushing decrypted data into buffer
buf.push(decipher.final('utf8'));
 
// Display the result
console.log(buf.join(' '));

输出:

some clear text  data

使用以下命令运行 index.js 文件:

node index.js

参考: https://nodejs.org/dist/latest-v12.x/docs/api/crypto.html#crypto_decipher_final_outputencoding