decipher.update() ใช้เพื่ออัปเดตตัวถอดรหัสด้วยข้อมูล receivd ตามรูปแบบการเข้ารหัสที่กำหนด มันเป็นหนึ่งในวิธีการ inbuilt ที่จัดเตรียมโดยคลาส Decipher ภายในโมดูล crypto หากมีการระบุการเข้ารหัสอินพุต อาร์กิวเมนต์ data จะเป็นสตริง มิฉะนั้น อาร์กิวเมนต์ data จะเป็นบัฟเฟอร์
ไวยากรณ์
decipher.update(data, [inputEncoding], [outputEncoding])
พารามิเตอร์
พารามิเตอร์ข้างต้นอธิบายไว้ด้านล่าง −
-
ข้อมูล – ใช้ข้อมูลเป็นอินพุตที่ส่งผ่านเพื่ออัปเดตเนื้อหาถอดรหัส
-
การเข้ารหัสอินพุต – ใช้การเข้ารหัสอินพุตเป็นพารามิเตอร์ ค่าอินพุตที่เป็นไปได้ ได้แก่ hex, base64 เป็นต้น
-
การเข้ารหัสเอาต์พุต – ใช้การเข้ารหัสเอาต์พุตเป็นพารามิเตอร์ ประเภทอินพุตสำหรับพารามิเตอร์นี้คือสตริง ค่าอินพุตที่เป็นไปได้ ได้แก่ hex, base64 เป็นต้น
ตัวอย่าง
สร้างไฟล์ที่มีชื่อ – decipherUpdate.js และคัดลอกข้อมูลโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ให้ใช้คำสั่งต่อไปนี้เพื่อเรียกใช้โค้ดนี้ดังแสดงในตัวอย่างด้านล่าง −
node decipherUpdate.js
decipherUpdate.js
// Example to demonstrate the use of decipher.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 decipher object to get decipher
const encrypted = '083bfe1b2f91677e5d00add115be2f1b2e362e190406f5c6b60e86969bf03bff';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';
let decryptedValue = decipher.update(encrypted, 'hex', 'utf8');
// let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');
decryptedValue += decipher.final('utf8');
// Printing the result...
console.log("Decrypted value -- " + decryptedValue);
// console.log("Base64 String:- " + base64Value) ผลลัพธ์
C:\home\node>> node decipherUpdate.js Decrypted value -- Some new text data
ตัวอย่าง
ลองดูอีกตัวอย่างหนึ่ง
// Example to demonstrate the use of decipher.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
crypto.scrypt(password, 'salt', 24,
{ N: 512 }, (err, key) => {
if (err) throw err;
// Initializing the static iv
const iv = Buffer.alloc(16, 0);
// Initializing the decipher with algo, key and iv
const decipher = crypto.createDecipheriv(algorithm, key, iv);
const encrypted = '91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074';
//Getting the decrypted string value
const decrypted = decipher.update(encrypted, 'hex', 'utf8');
// Printing the result...
console.log("Decrypted value:- " + decrypted);
}); ผลลัพธ์
C:\home\node>> node decipherUpdate.js Decrypted value:- Some new text data