decipher.final() ใช้เพื่อส่งคืนบัฟเฟอร์หรือสตริงที่มีค่าของวัตถุถอดรหัส มันเป็นหนึ่งในวิธีการ inbuilt ที่จัดเตรียมโดย class Cipher ภายในโมดูล crypto วิธีถอดรหัสไม่สามารถใช้เพื่อถอดรหัสข้อมูลได้เมื่อมีการเรียกวิธีการ decipher.final การเรียกเมธอด cipher.final มากกว่าหนึ่งครั้งจะทำให้เกิดข้อผิดพลาด
ไวยากรณ์
decipher.final([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\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