當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js decipher.final()用法及代碼示例


decipher.final()方法是加密模塊中的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 initializng algorith 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 initializg the static iv 
const iv = Buffer.alloc(16, 0); 
  
// Creating and initializng the decipher object 
const decipher = crypto.createDecipheriv(algorithm, key, iv); 
  
// Encrypted using same algorithm, key and iv. 
const encrypted = 
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; 
  
// Updaing 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);

輸出:

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 initializng algorith 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 intializg the static iv 
const iv = Buffer.alloc(16, 0); 
  
// creating and intializng the decipher object 
const decipher = crypto.createDecipheriv(algorithm, key, iv); 
  
// Encrypted using same algorithm, key and iv. 
const encrypted =  
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; 
  
// Creating and initializng empty buffer 
var buf = []; 
  
// Updaing the data to tbe decipher 
let decrypted = decipher.update(encrypted, 'hex', 'utf8'); 
  
// Uushing the updated data into buffer 
buf.push(decrypted); 
  
// Pushing dcripted 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

相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js decipher.final() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。