crypto.createCipheriv() পদ্ধতিটি প্রথমে প্রদত্ত কী এবং অনুমোদন ফ্যাক্টর (iv) এর জন্য পাস করা অ্যালগরিদম অনুযায়ী সাইফার অবজেক্ট তৈরি করবে এবং ফেরত দেবে।
সিনট্যাক্স
crypto.createCipheriv(algorithm, key, iv, options)
পরামিতি
উপরের পরামিতিগুলি নীচে −
হিসাবে বর্ণনা করা হয়েছে-
অ্যালগরিদম - এটি অ্যালগরিদমের জন্য ইনপুট নেয় যা সাইফার তৈরি করতে ব্যবহার করা হবে। কিছু সম্ভাব্য মান হল:aes192, aes256, ইত্যাদি।
-
কী - এটি কাঁচা কীটির জন্য ইনপুট নেয় যা অ্যালগরিদম এবং iv দ্বারা ব্যবহৃত হয়। সম্ভাব্য মান টাইপের হতে পারে:স্ট্রিং, বাফার, টাইপডঅ্যারে বা ডেটাভিউ। এটি ঐচ্ছিকভাবে গোপন টাইপের একটি টাইপ অবজেক্ট হতে পারে।
-
iv - প্রারম্ভিক ভেক্টর হিসাবেও পরিচিত। এই প্যারামিটারটি iv এর জন্য ইনপুট নেয় যা সাইফারটিকে অনিশ্চিত এবং অনন্য করে তুলবে। এটি একটি গোপন হতে হবে না. এর সম্ভাব্য মান প্রকারগুলি হল:স্ট্রিং, বাফার, টাইপডঅ্যারে, ডেটাভিউ। সাইফারের প্রয়োজন না হলে এটি শূন্য হতে পারে।
-
বিকল্পগুলি ৷ - এটি স্ট্রিম আচরণ নিয়ন্ত্রণ করার জন্য একটি ঐচ্ছিক পরামিতি। এটি ঐচ্ছিক নয় যখন সাইফার 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==