Phương thức crypto.createDecipheriv() là giao diện lập trình từ module 'crypto'. Nó tạo và trả về đối tượng Decipher dựa trên thuật toán, khóa, vector khởi tạo (iv) và options được truyền vào.
Cú pháp
crypto.createDecipheriv(algorithm, key, iv, [options])
Tham số
algorithm – Thuật toán dùng để tạo cipher. Ví dụ:
aes192,aes256, v.v.key – Khóa gốc dùng bởi thuật toán và iv. Kiểu: string, buffer, TypedArray, DataView hoặc KeyObject.
iv – Vector khởi tạo (initialization vector). Làm cho cipher độc nhất, không cần giữ bí mật. Kiểu: string, buffer, TypedArray, DataView. Có thể là
nullnếu cipher không yêu cầu.options – Tham số tùy chọn kiểm soát stream. Bắt buộc khi dùng chế độ CCM hoặc OCB (ví dụ
'aes-256-ccm').
Ví dụ
Tạo file createDecipheriv.js và sao chép mã. Chạy lệnh:
node createDecipheriv.js
createDecipheriv.js
// Chương trình demo tạo Decipher
// Import module crypto
const crypto = require('crypto');
// Khởi tạo thuật toán
const algorithm = 'aes-192-cbc';
// Đặt mật khẩu
const password = '123456789';
// Tạo khóa từ mật khẩu
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);
// Khởi tạo vector iv
const iv = Buffer.alloc(16, 0);
// Tạo Decipher với các tham số trên
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = '';
// Đọc và giải mã dữ liệu
decipher.on('readable', () => {
let chunk;
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
// Xử lý sự kiện kết thúc
decipher.on('end', () => {
console.log(decrypted);
});
// Dữ liệu đã mã hóa sẽ được giải mã
const encrypted = 'uqeQEkXy5dpJjQv+JDvMHw==';
// Ghi dữ liệu mã hóa vào decipher
decipher.write(encrypted, 'base64');
decipher.end();
console.log("Hoàn tất... !");
Kết quả
C:\home\node>> node createDecipheriv.js Hoàn tất... ! TutorialsPoint
Ví dụ 2
Ví dụ hoàn chỉnh mã hóa và giải mã AES-256-CBC.
// Chương trình demo tạo Decipher
// Import module crypto
const crypto = require('crypto');
// Khởi tạo thuật toán
const algorithm = 'aes-256-cbc';
// Đặt mật khẩu
const password = '123456789'
// Tạo khóa ngẫu nhiên 32 byte
const key = crypto.randomBytes(32);
// Tạo iv ngẫu nhiên 16 byte
const iv = crypto.randomBytes(16);
// Hàm mã hóa dữ liệu
function encrypt(text) {
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}
// Hàm giải mã dữ liệu
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText = Buffer.from(text.encryptedData, 'hex');
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();
}
// Mã hóa và in kết quả
var output = encrypt("Welcome to TutorialsPoint !");
console.log("Dữ liệu mã hóa -- ", output);
// Giải mã và in kết quả
console.log("Dữ liệu giải mã -- ", decrypt(output));
Kết quả
C:\home\node>> node createDecipheriv.js
Dữ liệu mã hóa -- { iv: '3fb2c84290e04d9bfb099bc65a7ac941', encryptedData: '4490777e90c5a78037cb92a99d561ae250562e2636af459b911cfa01c0191e3f' }
Dữ liệu giải mã -- Welcome to TutorialsPoint !