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

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

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

ไวยากรณ์

crypto.publicEncrypt(key, buffer)

พารามิเตอร์

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

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

    • key – ฟิลด์นี้มีกุญแจสาธารณะหรือส่วนตัวที่เข้ารหัสแบบ PEM สามารถเป็น string, buffer หรือ keyObject

    • oaepHash – ฟิลด์นี้มีฟังก์ชันแฮชที่ใช้สำหรับ OAEP padding และ MGF1 ค่าเริ่มต้นคือ 'sha1'

    • oaepLabel – ฟิลด์นี้มีค่าสำหรับ OAEP padding ไม่ใช้ label หากไม่ระบุ

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

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

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

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

ตัวอย่าง

สร้างไฟล์ชื่อ 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+twA55olM0rFfnuV6qqtca8SXIHQrk=

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

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

// โปรแกรม 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: <Buffer 33 0b 54 96 0e 8f 34 6c b4 d5 7a cf d4 d5 ef 7b 7e c5 ec 97 cf 75 05 07 df 5a 9e d4 3d cc 3e bb 55 e7 50 1b 64 f0 c8 89 19 61 81 99 e5 88 10 4a 3b 5a ... >