ভূমিকা
crypto.createCipheriv() মেথড প্রথমে সাইফার অবজেক্ট তৈরি করে এবং প্রদত্ত অ্যালগরিদম, কি এবং অথোরাইজেশন ফ্যাক্টর (iv) অনুযায়ী সেটি রিটার্ন করে।
সিনট্যাক্স
crypto.createCipheriv(algorithm, key, iv, options)
প্যারামিটার
algorithm — সাইফার তৈরির জন্য অ্যালগরিদম। সম্ভব মান: aes192, aes256, ইত্যাদি।
key — অ্যালগরিদম এবং iv দ্বারা ব্যবহৃত র কি। সম্ভব টাইপ: স্ট্রিং, বাফার, TypedArray, DataView। অপশনালি সিক্রেট টাইপ অবজেক্টও হতে পারে।
iv — ইনিশিয়ালাইজেশন ভেক্টর। সাইফারকে অনিশ্চিত এবং ইউনিক করে। সিক্রেট হতে হয় না। টাইপ: স্ট্রিং, বাফার, TypedArray, DataView। সাইফার যদি প্রয়োজন না করে তবে null হতে পারে।
options — স্ট্রিম বিহевиয়ার কন্ট্রোলের জন্য অপশনাল প্যারামিটার। CCM বা OCB মোডে (যেমন 'aes-256-ccm') বাধ্যতামূলক।
উদাহরণ ১
createCipheriv.js ফাইল তৈরি করে node 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==