วิธี crypto.createCipheriv() จะสร้างก่อนแล้วจึงส่งคืนวัตถุเข้ารหัสตามอัลกอริทึมที่ส่งผ่านสำหรับคีย์ที่กำหนดและปัจจัยการอนุญาต (iv)
ไวยากรณ์
crypto.createCipheriv(algorithm, key, iv, options)
พารามิเตอร์
พารามิเตอร์ข้างต้นอธิบายไว้ด้านล่าง −
-
อัลกอริทึม – ใช้อินพุตสำหรับอัลกอริธึมที่จะใช้ในการสร้างตัวเลข ค่าที่เป็นไปได้ ได้แก่ aes192, aes256 เป็นต้น
-
คีย์ – รับอินพุตสำหรับคีย์ดิบที่ใช้โดยอัลกอริทึมและ iv ค่าที่เป็นไปได้อาจเป็นประเภท:string, buffer, TypedArray หรือ DataView สามารถเลือกเป็นวัตถุประเภทที่เป็นความลับได้
-
iv – หรือที่เรียกว่าเวคเตอร์เริ่มต้น พารามิเตอร์นี้รับอินพุตสำหรับ iv ซึ่งจะทำให้รหัสไม่แน่นอนและไม่ซ้ำกัน ไม่จำเป็นต้องเป็นความลับ ประเภทของค่าที่เป็นไปได้คือ:string, buffer, TypedArray, DataView ค่านี้สามารถเป็นโมฆะได้หากรหัสลับไม่ต้องการ
-
ตัวเลือก – นี่เป็นพารามิเตอร์ทางเลือกสำหรับควบคุมพฤติกรรมการสตรีม ซึ่งไม่บังคับเมื่อใช้การเข้ารหัสในโหมด CCM หรือ OCB (เช่น 'aes-256-ccm')
ตัวอย่าง
สร้างไฟล์ที่มีชื่อ – createCipheriv.js และคัดลอกข้อมูลโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ให้ใช้คำสั่งต่อไปนี้เพื่อเรียกใช้โค้ดนี้ดังแสดงในตัวอย่างด้านล่าง −
node createCipheriv.js
createCipheriv.js
// A node demo program for creating the ECDH
// Importing the crypto module
const crypto = require('crypto');
// Initializing the algorithm
const algorithm = 'aes-256-cbc';
// Initializing the key
const key = crypto.randomBytes(32);
// Initializing the iv vector
const iv = crypto.randomBytes(16);
// Creating the function to encrypt data
function encrypt(text) {
// Creating the cipher with the above defined parameters
let cipher = crypto.createCipheriv(
'aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
// Returning iv and the encrypted data
return { iv: iv.toString('hex'),
encryptedData: encrypted.toString('hex') };
}
// Printing public & private curve keys...
var output = encrypt("TutorialsPoint");
console.log(output); ผลลัพธ์
C:\home\node>> node createCipheriv.js
{ iv: '3dd899aa441c00d4d8d2ff95abb2e684',
encryptedData: 'b4985053bc1507fc25a4d99823dc8b03' } ตัวอย่าง
ลองดูอีกตัวอย่างหนึ่ง
// A node demo program for creating the ECDH
// Importing the crypto module
const crypto = require('crypto');
// Initializing the algorithm
const algorithm = 'aes-192-cbc';
// Defining and initializing the password
const password = '123456789'
// Initializing the key
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);
// Initializing the iv vector
const iv = Buffer.alloc(16, 0);
// Creating the cipher with the above defined parameters
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = '';
// Reading and encrypting the data
cipher.on('readable', () => {
let chunk;
while (null !== (chunk = cipher.read())) {
encrypted += chunk.toString('base64');
}
});
//Handling the closing/end event
cipher.on('end', () => {
console.log(encrypted);
});
// Printing public & private curve keys...
cipher.write('TutorialsPoint');
cipher.end();
console.log("Completed... !"); ผลลัพธ์
C:\home\node>> node createCipheriv.js Completed... ! uqeQEkXy5dpJjQv+JDvMHw==