Node.js में crypto.publicEncrypt() मेथड का उपयोग बफर पैरामीटर में दिए गए डेटा को एन्क्रिप्ट करने के लिए किया जाता है, जिसमें पैरामीटर में पास की गई पब्लिक की का उपयोग किया जाता है। वापस किया गया डेटा संबंधित प्राइवेट की का उपयोग करके डिक्रिप्ट किया जा सकता है।
सिंटैक्स
crypto.publicEncrypt(key, buffer)
पैरामीटर्स
उपरोक्त पैरामीटर्स का विवरण नीचे दिया गया है:
key – इसमें निम्न 5 प्रकार के डेटा हो सकते हैं: Object, String, Buffer या KeyObject।
key – यह फील्ड PEM एन्कोडेड पब्लिक या प्राइवेट की रखता है। यह string, buffer या keyObject प्रकार का हो सकता है।
oaepHash – यह फील्ड OAEP पैडिंग और MGF1 के लिए उपयोग की जाने वाली हैश फंक्शन रखता है। डिफ़ॉल्ट मान: 'sha1'।
oaepLabel – यह फील्ड OAEP पैडिंग के लिए मान रखता है। यदि निर्दिष्ट न हो तो कोई लेबल उपयोग नहीं किया जाता।
passphrase – यह प्राइवेट की के लिए एक वैकल्पिक पासफ्रेज़ है।
padding – यह crypto.constants में परिभाषित एक वैकल्पिक मान है।
encoding – यह एन्कोडिंग का प्रकार है जिसका उपयोग तब किया जाता है जब buffer, key, oaepLabel या passphrase मान स्ट्रिंग होते हैं।
buffer – यह फील्ड एन्क्रिप्ट किए जाने वाले डेटा कंटेंट को रखता है। संभावित बफर प्रकार हैं: string, TypedArray, Buffer, ArrayBuffer, DataView।
उदाहरण 1: स्ट्रिंग एन्क्रिप्शन (Base64 आउटपुट)
publicEncrypt.js नाम से एक फाइल बनाएं और नीचे दिया गया कोड स्निपेट कॉपी करें। फाइल बनाने के बाद, नीचे दिखाए अनुसार कोड चलाने के लिए निम्न कमांड का उपयोग करें:
node publicEncrypt.js
publicEncrypt.js
// Node.js प्रोग्राम crypto.publicEncrypt() मेथड के फ्लो का प्रदर्शन करने के लिए
// crypto और fs मॉड्यूल इम्पोर्ट करना
const crypto = require('crypto');
const fs = require("fs");
// कीज़ जेनरेट करने के लिए नीचे दिया गया फंक्शन बनाना
function generateKeyFiles() {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 520,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: ''
}
});
// नीचे दिए गए नाम से पब्लिक की फाइल बनाना
fs.writeFileSync("public_key", keyPair.publicKey);
}
// कीज़ जेनरेट करना
generateKeyFiles();
// नीचे दिए गए फंक्शन का उपयोग करके स्ट्रिंग एन्क्रिप्ट करना
function encryptString(plaintext, publicKeyFile) {
const publicKey = fs.readFileSync(publicKeyFile, "utf8");
// नीचे दिए गए पैरामीटर्स के साथ publicEncrypt() कॉल करना
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
return encrypted.toString("base64");
}
// एन्क्रिप्ट किया जाने वाला टेक्स्ट
const plainText = "TutorialsPoint";
// एन्क्रिप्टेड टेक्स्ट परिभाषित करना
const encrypted = encryptString(plainText, "./public_key");
// प्लेन टेक्स्ट प्रिंट करना
console.log("Plaintext:", plainText);
// एन्क्रिप्टेड टेक्स्ट प्रिंट करना
console.log("Encrypted: ", encrypted);
आउटपुट
C:\home\node>> node publicEncrypt.js Plaintext: TutorialsPoint Encrypted: kgnqPxy/n34z+/5wd7MZiMAL5LrQisTLfZiWoSChXSvxgtifMQaZ56cbF+twA55olM0rFfnuV6qqtc a8SXIHQrk=
उदाहरण 2: बफर आउटपुट के साथ एन्क्रिप्शन
आइए एक और उदाहरण देखते हैं।
// Node.js प्रोग्राम crypto.publicEncrypt() मेथड के फ्लो का प्रदर्शन करने के लिए
// crypto और fs मॉड्यूल इम्पोर्ट करना
const crypto = require('crypto');
const fs = require("fs");
// कीज़ जेनरेट करने के लिए नीचे दिया गया फंक्शन बनाना
function generateKeyFiles() {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 520,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: ''
}
});
// पब्लिक की फाइल बनाना
fs.writeFileSync("public_key", keyPair.publicKey);
}
// कीज़ जेनरेट करना
generateKeyFiles();
// नीचे दिए गए फंक्शन का उपयोग करके स्ट्रिंग एन्क्रिप्ट करना
function encryptString(plaintext, publicKeyFile) {
const publicKey = fs.readFileSync(publicKeyFile, "utf8");
// नीचे दिए गए पैरामीटर्स के साथ publicEncrypt() कॉल करना
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
return encrypted;
}
// एन्क्रिप्ट किया जाने वाला टेक्स्ट
const plainText = "Hello TutorialsPoint!";
// एन्क्रिप्टेड टेक्स्ट परिभाषित करना
const encrypted = encryptString(plainText, "./public_key");
// प्लेन टेक्स्ट प्रिंट करना
console.log("Plaintext:", plainText);
// एन्क्रिप्टेड बफर प्रिंट करना
console.log("Buffer: ", encrypted);
आउटपुट
C:\home\node>> node publicEncrypt.js Plaintext: Hello TutorialsPoint! Buffer: