Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

เมธอด crypto.publicDecrypt() ใน Node.js

เมธอด crypto.publicDecrypt() ใช้สำหรับถอดรหัสข้อมูลที่กำหนดใน buffer ด้วยกุญแจสาธารณะ Buffer นี้เคยถูกเข้ารหัสโดยใช้กุญแจส่วนตัวที่สอดคล้องกัน คือเมธอด crypto.privateEncrypt()

ไวยากรณ์

crypto.publicDecrypt(key, buffer)

พารามิเตอร์

พารามิเตอร์ข้างต้นอธิบายดังนี้:

  • key – สามารถมีข้อมูล 5 ประเภทดังต่อไปนี้ ประเภท Object, String, Buffer หรือ KeyObject:

    • passphrase – รหัสผ่านตัวเลือกสำหรับกุญแจส่วนตัว

    • padding – ค่าตัวเลือกที่กำหนดใน crypto.constants

    • encoding – ประเภทการเข้ารหัสที่ต้องใช้เมื่อ buffer, key, oaepLabel หรือ passphrase เป็นสตริง

  • buffer – ฟิลด์นี้มีเนื้อหาข้อมูลที่จะถอดรหัส ประเภทบัฟเฟอร์ที่เป็นไปได้: string, TypedArray, Buffer, ArrayBuffer, DataView

ตัวอย่าง

สร้างไฟล์ชื่อ publicDecrypt.js และคัดลอกโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ใช้คำสั่งต่อไปนี้เพื่อรันโค้ด:

node publicDecrypt.js

publicDecrypt.js

// ตัวอย่างการใช้ crypto.publicDecrypt
// นำเข้าโมดูล crypto, fs และ path
var crypto = require('crypto');
var fs = require('fs');
const path = require('path');
// สร้างฟังก์ชันด้านล่างสำหรับสร้างกุญแจ
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);
    fs.writeFileSync("private_key", keyPair.privateKey);
}
// เรียกเมธอดสร้างกุญแจ
generateKeyFiles();
// อ่านกุญแจส่วนตัวและสาธารณะ
var private = fs.readFileSync('private_key');
var public = fs.readFileSync('public_key');
// กำหนดข้อมูลต้นฉบับ
var data = 'Welcome to TutorialsPoint';
console.log("Original Data is: " + data);
// เข้ารหัสข้อมูลโดยใช้กุญแจส่วนตัว
encrypted = crypto.privateEncrypt(private, Buffer.from(data, 'utf8')).toString('base64');
// ถอดรหัสข้อมูลโดยใช้กุญแจสาธารณะ
originalData = crypto.publicDecrypt(public, Buffer.from(encrypted, 'base64'));
console.log();
// แสดงข้อความที่เข้ารหัส
console.log("Encrypted with private key: " + encrypted);
console.log();
// แสดงข้อความที่ถอดรหัส
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+AIKnz2lrjmFhIhnpZWbF4=
Decrypted with public key: Welcome to TutorialsPoint

ตัวอย่างเพิ่มเติม

ลองดูตัวอย่างอีกหนึ่งกรณี:

// ตัวอย่างการใช้ crypto.publicDecrypt
// นำเข้าโมดูล crypto และ fs
var crypto = require('crypto');
var 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);
    fs.writeFileSync("private_key", keyPair.privateKey);
}
// เรียกเมธอดสร้างกุญแจ
generateKeyFiles();
// อ่านกุญแจส่วนตัว
privateKey = fs.readFileSync('private_key').toString();
var buffer = Buffer.from('Welcome to TutorialsPoint', 'utf8');
console.log("Data buffer before encryption")
console.log(buffer);
// เข้ารหัสข้อความบัฟเฟอร์
encrypted = crypto.privateEncrypt(privateKey, buffer);
// แสดงข้อมูลหลังเข้ารหัส
console.log("Data after encryption: ");
console.log(encrypted);
// อ่านกุญแจสาธารณะ
publicKey = fs.readFileSync('public_key').toString();
// ถอดรหัสข้อความที่เข้ารหัสโดยใช้กุญแจสาธารณะ
decryptedData = crypto.publicDecrypt(publicKey, encrypted);
// แสดงเนื้อหาต้นฉบับ
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>