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

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

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

ไวยากรณ์

crypto.privateEncrypt(privateKey, buffer)

พารามิเตอร์

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

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

    • key – กุญแจนี้เป็นกุญแจส่วนตัวที่เข้ารหัสแบบ 'PEM' กุญแจสามารถเป็น string, buffer หรือ KeyObject

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

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

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

ตัวอย่าง

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

node privateEncrypt.js

privateEncrypt.js

// โปรแกรม Node.js สาธิตการทำงานของเมธอด crypto.privateEncrypt()
// นำเข้าโมดูล crypto และ fs
const crypto = require('crypto');
const fs = require("fs");
// สร้างไฟล์กุญแจโดยใช้เมธอด generateKeyPairSync()
function generateKeyFiles() {
    const keyPair = crypto.generateKeyPairSync('rsa', {
        modulusLength: 530,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: ''
        }
    });
    // เขียนกุญแจในไฟล์ต่อไปนี้
    fs.writeFileSync("private_key", keyPair.privateKey);
}
// เรียกเมธอดสร้างกุญแจ
generateKeyFiles();
// เข้ารหัสสตริงที่ส่งผ่าน
function encryptString(plaintext, publicKeyFile) {
    const publicKey = fs.readFileSync(publicKeyFile, "utf8");
    // เข้ารหัสข้อมูลโดยใช้เมธอด privateEncrypt() และกุญแจสาธารณะ
    const encrypted = crypto.privateEncrypt(publicKey, Buffer.from(plaintext));
    return encrypted.toString("base64");
}
// ข้อมูลต่อไปนี้จะถูกเข้ารหัสและถอดรหัส
const plainText = "TutorialsPoint!";
// เรียกเมธอดด้านล่างเพื่อเข้ารหัสสตริง
const encrypted = encryptString(plainText, "./private_key");
// แสดงข้อความต้นฉบับ
console.log("Plaintext:", plainText);
// แสดงข้อความที่เข้ารหัส
console.log("Encrypted Text: ", encrypted);
console.log();

ผลลัพธ์

C:\home\node>> node privateEncrypt.js
Plaintext: TutorialsPoint!
Encrypted Text: AhphoL+l/e739LkdfCAm2XuiddgTG7jjdGlLviiRqD4LyTtxJmpkg5bSkyI7Og4XlBtszBB9HLQRHT5j850ZAxGYA==

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

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

// โปรแกรม Node.js สาธิตการทำงานของเมธอด crypto.privateEncrypt()
// นำเข้าโมดูล crypto และ fs
const crypto = require('crypto');
const fs = require("fs");
// สร้างไฟล์กุญแจโดยใช้เมธอด generateKeyPairSync()
function generateKeyFiles() {
    const keyPair = crypto.generateKeyPairSync('rsa', {
        modulusLength: 530,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: ''
        }
    });
    // เขียนกุญแจในไฟล์ต่อไปนี้
    fs.writeFileSync("private_key", keyPair.privateKey);
}
// เรียกเมธอดสร้างกุญแจ
generateKeyFiles();
// เข้ารหัสสตริงที่ส่งผ่าน
function encryptString(plaintext, publicKeyFile) {
    const publicKey = fs.readFileSync(publicKeyFile, "utf8");
    // เข้ารหัสข้อมูลโดยใช้เมธอด privateEncrypt() และกุญแจสาธารณะ
    const encrypted = crypto.privateEncrypt(publicKey, Buffer.from(plaintext));
    return encrypted;
}
// ข้อมูลต่อไปนี้จะถูกเข้ารหัสและถอดรหัส
const plainText = "TutorialsPoint!";
// เรียกเมธอดด้านล่างเพื่อเข้ารหัสสตริง
const encrypted = encryptString(plainText, "./private_key");
// แสดงข้อความต้นฉบับ
console.log("Plaintext:", plainText);
// แสดงข้อความที่เข้ารหัส
console.log("Encrypted Buffer: ", encrypted);
console.log();

ผลลัพธ์

C:\home\node>> node privateEncrypt.js
Plaintext: TutorialsPoint!
Encrypted Buffer: <Buffer 00 a2 ce 5d 27 d7 15 ec a7 bc 5b 12 73 62 92 82 6d f9 6b 82 38 06 dd bf b3 b8 1f 23 b4 d9 e9 aa 1c 50 0f c9 d7 12 f9 17 db 91 dc 05 46 b0 0c 08 14 50 ... >