কম্পিউটার

Node.js-এ crypto.publicDecrypt() পদ্ধতি


crypto.publicDecrypt() পাবলিক কী দিয়ে বাফারে প্রদত্ত ডেটা ডিক্রিপ্ট করার জন্য ব্যবহার করা হয়। এই বাফারটি সংশ্লিষ্ট প্রাইভেট কী অর্থাৎ crypto.privateEncrypt() পদ্ধতি ব্যবহার করে এনক্রিপ্ট করা হয়েছে।

সিনট্যাক্স

crypto.publicDecrypt(key, buffer)

পরামিতি

উপরের পরামিতিগুলি নীচে −

হিসাবে বর্ণনা করা হয়েছে
  • কী - এটিতে নিম্নলিখিত ধরণের 5 প্রকারের ডেটা থাকতে পারে - অবজেক্ট, স্ট্রিং, বাফার বা কীঅবজেক্ট৷

    • পাসফ্রেজ - এটি ব্যক্তিগত কী-এর জন্য একটি ঐচ্ছিক পাসফ্রেজ৷

    • প্যাডিং – এটি একটি ঐচ্ছিক মান যা crypto.constants এ সংজ্ঞায়িত করা হয়েছে।

    • এনকোডিং – এটি এমন একটি এনকোডিং যা ব্যবহার করা প্রয়োজন যখন বাফার, কী, oaepLabel বা পাসফ্রেজ মান স্ট্রিং হয়৷

  • বাফার - এই ক্ষেত্রটিতে এনক্রিপ্ট করা ডেটা সামগ্রী রয়েছে৷ সম্ভাব্য বাফার প্রকারগুলি হল:string, TypedArray, Buffer, ArrayBuffer, DataView.

উদাহরণ

publicDecrypt.js নামের একটি ফাইল তৈরি করুন এবং নীচের কোড স্নিপেটটি অনুলিপি করুন। ফাইল তৈরি করার পরে, নীচের উদাহরণে দেখানো এই কোডটি চালানোর জন্য নিম্নলিখিত কমান্ডটি ব্যবহার করুন −

node publicDecrypt.js

publicDecrypt.js

// crypto.publicDecrypt Demo Example

// Importing the crypto, fs and path module
var crypto = require('crypto');
var fs = require('fs');
const path = require('path');

// Creating below function for generating keys
function generateKeyFiles() {

   const keyPair = crypto.generateKeyPairSync('rsa', {
      modulusLength: 520,
      publicKeyEncoding: {
         type: 'spki',
         format: 'pem'
      },
      privateKeyEncoding: {
         type: 'pkcs8',
         format: 'pem',
         cipher: 'aes-256-cbc',
         passphrase: ''
      }
   });

   // Creating the public key file with the below name
   fs.writeFileSync("public_key", keyPair.publicKey);
   fs.writeFileSync("private_key", keyPair.privateKey);
}

// Calling Generate keys method
generateKeyFiles();

// Reading the Private and Public Key
var private = fs.readFileSync('private_key');
var public = fs.readFileSync('public_key');

// Defining the original data
var data = 'Welcome to TutorialsPoint';
console.log("Original Data is: "+data);

// Encrypting the data using private key
encrypted = crypto.privateEncrypt(private, Buffer.from(data, 'utf8')).toString('base64');

// Decrypting the data usig publicKey
originalData = crypto.publicDecrypt(public, Buffer.from(encrypted, 'base64'));

console.log();

// Printing encrypted msg
console.log("Encrypted with private key: " + encrypted);

console.log();

// Printing decrypted msg
console.log("Decrypted with public key: " + originalData.toString());

আউটপুট

C:\home\node>> node publicDecrypt.js
Original Data is: Welcome to TutorialsPoint

Encrypted with private key:
EFBihrKebXb0gfCF7nTnw82yXpToH5eVBpLc8O5QL/ZgfZ/qJT5I/BejSMwV4NFCp+AIKnz2lrjmFh
IhnpZWbF4=

Decrypted with public key: Welcome to TutorialsPoint

উদাহরণ

আসুন আরও একটি উদাহরণ দেখি।

// crypto.publicDecrypt Demo Example

// Importing the crypto and fs module
var crypto = require('crypto');
var fs = require('fs');

// Creating below function for generating keys
function generateKeyFiles() {

   const keyPair = crypto.generateKeyPairSync('rsa', {
      modulusLength: 520,
      publicKeyEncoding: {
         type: 'spki',
         format: 'pem'
      },
      privateKeyEncoding: {
         type: 'pkcs8',
         format: 'pem',
         cipher: 'aes-256-cbc',
         passphrase: ''
      }
   });

   // Creating the public key file with the below name
   fs.writeFileSync("public_key", keyPair.publicKey);
   fs.writeFileSync("private_key", keyPair.privateKey);
}

// Calling Generate keys method
generateKeyFiles();

// Reading the Private Key
privateKey = fs.readFileSync('private_key').toString();
var buffer = Buffer.from('Welcome to TutorialsPoint', 'utf8');
console.log("Data buffer before encryption")
console.log(buffer);

// Encrpting the buffer text
encrypted = crypto.privateEncrypt(privateKey, buffer);

// Printing the data after encryption
console.log("Data after encryption: ");
console.log(encrypted);

// Reading the Public key
publicKey = fs.readFileSync('public_key').toString();

// Decrypting the encrypted text using public key
decryptedData = crypto.publicDecrypt(publicKey, encrypted);

// Printing the original content
console.log("Data after decryption: ");
console.log(decryptedData);
মুদ্রণ করা

আউটপুট

C:\home\node>> node publicDecrypt.js
Data buffer before encryption
<Buffer 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 69
6e 74>
Data after encryption:
<Buffer a6 9d e3 86 9f 3f 4b b9 3f f7 a6 9c 7c 16 0f 04 b9 c4 16 0b 08 f1 06
39 de 32 75 7c 26 88 fa 49 bd 31 6b 4b 4d 02 e6 87 56 ee 9c 95 53 10 8f 28 49
f5 ... >
Data after decryption:
<Buffer 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 69
6e 74>

  1. Node.js-এ crypto.publicEncrypt() পদ্ধতি

  2. Node.js-এ crypto.privateEncrypt() পদ্ধতি

  3. Node.js-এ crypto.privateDecrypt() পদ্ধতি

  4. Node.js-এ crypto.getHashes() পদ্ধতি