Mô tả
Phương thức crypto.createCipheriv() sẽ trước tiên tạo và sau đó trả về đối tượng cipher theo thuật toán được truyền cho khóa và yếu tố ủy quyền (iv) cho trước.
Cú pháp
crypto.createCipheriv(algorithm, key, iv, options)
Tham số
algorithm – Nhận đầu vào cho thuật toán sẽ được dùng để tạo cipher. Một số giá trị có thể: aes192, aes256, v.v.
key – Nhận đầu vào cho khóa gốc được thuật toán và iv sử dụng. Các giá trị có thể kiểu: string, buffer, TypedArray hoặc DataView. Nó cũng có thể tùy chọn là kiểu object secret.
iv – Còn gọi là vector khởi tạo. Tham số này nhận đầu vào cho iv sẽ làm cho cipher không xác định và duy nhất. Nó không cần phải bí mật. Các kiểu giá trị có thể: string, buffer, TypedArray, DataView. Có thể là null nếu cipher không cần.
options – Tham số tùy chọn để kiểm soát hành vi stream. Tham số này không tùy chọn khi cipher dùng ở chế độ CCM hoặc OCB (như 'aes-256-ccm').
Ví dụ
Tạo file tên createCipheriv.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:
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);Đầu ra
C:\home\node>> node createCipheriv.js { iv: '3dd899aa441c00d4d8d2ff95abb2e684', encryptedData: 'b4985053bc1507fc25a4d99823dc8b03' }Ví dụ khác
Xem thêm một ví dụ nữa.
// 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... !");Đầu ra
C:\home\node>> node createCipheriv.js Completed... ! uqeQEkXy5dpJjQv+JDvMHw==