เมธอด crypto.createDecipheriv() เป็นอินเทอร์เฟซการเขียนโปรแกรมจากโมดูล 'crypto' จะสร้างและคืนค่าออบเจ็กต์ Decipher ตามอัลกอริทึม กุญแจ IV และตัวเลือกที่ส่งผ่านฟังก์ชัน
ไวยากรณ์
crypto.createDecipheriv(algorithm, key, iv, [options])
พารามิเตอร์
พารามิเตอร์ข้างต้นอธิบายดังนี้:
algorithm – ระบุอัลกอริทึมที่จะใช้สร้าง cipher ค่าที่เป็นไปได้ เช่น
aes192,aes256ฯลฯkey – รับกุญแจดิบ (raw key) ที่อัลกอริทึมและ IV ใช้งานได้ ค่าที่เป็นไปได้: string, buffer, TypedArray, DataView หรืออาจเป็นออบเจ็กต์ประเภท secret
iv – เวกเตอร์การเริ่มต้น (initialization vector) พารามิเตอร์นี้รับ IV ที่ทำให้ cipher ไม่แน่นอนและไม่ซ้ำกัน ไม่จำเป็นต้องเป็นความลับ ค่าที่เป็นไปได้: string, buffer, TypedArray, DataView สามารถเป็น
nullได้หาก cipher ไม่ต้องการoptions – พารามิเตอร์ตัวเลือกสำหรับควบคุมพฤติกรรมสตรีม ไม่ใช่ตัวเลือกเมื่อ cipher ใช้ในโหมด CCM หรือ OCB (เช่น
'aes-256-ccm')
ตัวอย่าง
สร้างไฟล์ชื่อ createDecipheriv.js และคัดลอกโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ใช้คำสั่งต่อไปนี้เพื่อรันโค้ด:
node createDecipheriv.js
createDecipheriv.js
// โปรแกรม Node.js สาธิตการสร้าง ECDH (ตัวอย่างใช้ createDecipheriv)
// นำเข้าโมดูล crypto
const crypto = require('crypto');
// กำหนดอัลกอริทึม
const algorithm = 'aes-192-cbc';
// กำหนดและเริ่มต้นรหัสผ่าน
const password = '123456789';
// สร้างกุญแจ
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);
// กำหนด IV vector
const iv = Buffer.alloc(16, 0);
// สร้าง Decipher ด้วยพารามิเตอร์ที่กำหนด
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = '';
// อ่านและถอดรหัสข้อมูล
decipher.on('readable', () => {
let chunk;
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
// จัดการเหตุการณ์ปิด/สิ้นสุด
decipher.on('end', () => {
console.log(decrypted);
});
// ข้อมูลที่เข้ารหัสซึ่งจะถูกถอดรหัส
const encrypted = 'uqeQEkXy5dpJjQv+JDvMHw==';
// แสดงข้อความที่ถอดรหัสแล้ว
decipher.write(encrypted, 'base64');
decipher.end();
console.log("Completed... !");
ผลลัพธ์
C:\home\node>> node createDecipheriv.js Completed... ! TutorialsPoint
ตัวอย่างเพิ่มเติม
ลองดูตัวอย่างอีกหนึ่งกรณี:
// โปรแกรม Node.js สาธิตการสร้าง ECDH (ตัวอย่างใช้ createDecipheriv)
// นำเข้าโมดูล crypto
const crypto = require('crypto');
// กำหนดอัลกอริทึม
const algorithm = 'aes-256-cbc';
// กำหนดและเริ่มต้นรหัสผ่าน
const password = '123456789';
// สร้างกุญแจ
const key = crypto.randomBytes(32);
// กำหนด IV vector
const iv = crypto.randomBytes(16);
// ฟังก์ชันเข้ารหัสข้อมูล
function encrypt(text) {
// สร้าง cipher ด้วยพารามิเตอร์ที่กำหนด
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
// อัปเดตข้อความที่เข้ารหัส
let encrypted = cipher.update(text);
// ใช้การต่อบัฟเฟอร์
encrypted = Buffer.concat([encrypted, cipher.final()]);
// คืนค่า IV vector พร้อมข้อมูลที่เข้ารหัส
return {
iv: iv.toString('hex'),
encryptedData: encrypted.toString('hex')
};
}
// ฟังก์ชันถอดรหัสข้อมูล
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText = Buffer.from(text.encryptedData, 'hex');
// สร้าง decipher จากอัลกอริทึม กุญแจ และ IV
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
// อัปเดตข้อความที่ถอดรหัส
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
// คืนค่าข้อมูลตอบกลับหลังถอดรหัส
return decrypted.toString();
}
// เข้ารหัสข้อมูลด้านล่างและแสดงผลลัพธ์
var output = encrypt("Welcome to TutorialsPoint !");
console.log("Encrypted data -- ", output);
// แสดงข้อมูลที่ถอดรหัส
console.log("Decrypted data -- ", decrypt(output));
ผลลัพธ์
C:\home\node>> node createDecipheriv.js
Encrypted data -- { iv: '3fb2c84290e04d9bfb099bc65a7ac941', encryptedData: '4490777e90c5a78037cb92a99d561ae250562e2636af459b911cfa01c0191e3f' }
Decrypted data -- Welcome to TutorialsPoint !