Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Phương thức crypto.privateEncrypt() trong Node.js


Phương thức crypto.privateEncrypt() dùng để mã hóa dữ liệu bằng khóa riêng tư được truyền vào.

Cú pháp

crypto.privateEncrypt(privateKey, buffer)

Tham số

  • privateKey – Chứa các kiểu: Object, String, Buffer, KeyObject.

    • key – Khóa PEM encoded riêng tư. Kiểu string, buffer, KeyObject.

    • passphrase – Passphrase tùy chọn cho khóa riêng.

    • padding – Padding trong crypto.constants.

  • buffer – Dữ liệu cần mã hóa. Kiểu: string, TypedArray, Buffer, ArrayBuffer, DataView.

Ví dụ

Tạo file privateEncrypt.js và sao chép mã. Chạy lệnh:

node privateEncrypt.js

privateEncrypt.js

// Chương trình Node.js minh họa crypto.privateEncrypt()
// Import crypto và fs
const crypto = require('crypto');
const fs = require("fs");

// Hàm tạo file khóa dùng 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: ''
        }
    });
    // Ghi khóa vào file
    fs.writeFileSync("private_key", keyPair.privateKey);
}

generateKeyFiles();

// Hàm mã hóa chuỗi
function encryptString(plaintext, publicKeyFile) {
    const publicKey = fs.readFileSync(publicKeyFile, "utf8");
    // Mã hóa bằng privateEncrypt() và khóa công khai
    const encrypted = crypto.privateEncrypt(publicKey, Buffer.from(plaintext));
    return encrypted.toString("base64");
}

// Dữ liệu sẽ mã hóa/giải mã
const plainText = "TutorialsPoint!";
// Mã hóa
const encrypted = encryptString(plainText, "./private_key");

// In văn bản gốc
console.log("Văn bản gốc:", plainText);
// In văn bản mã hóa
console.log("Văn bản mã hóa: ", encrypted);
console.log();

Kết quả

C:\home\node>> node privateEncrypt.js
Văn bản gốc: TutorialsPoint!
Văn bản mã hóa: AhphoL+l/e739LkdfCAm2XuiddgTG7jjdGlLviiRqD4LyTtxJmpkgq5bSkyI7Og4XlBtszBB9HLQRH T5j850ZAxGYA==

Ví dụ 2

Ví dụ trả về buffer.

// Chương trình Node.js minh họa crypto.privateEncrypt()
// Import crypto và fs
const crypto = require('crypto');
const fs = require("fs");

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");
    const encrypted = crypto.privateEncrypt(publicKey, Buffer.from(plaintext));
    return encrypted;
}

const plainText = "TutorialsPoint!";
const encrypted = encryptString(plainText, "./private_key");

console.log("Văn bản gốc:", plainText);
console.log("Buffer mã hóa: ", encrypted);
console.log();

Kết quả

C:\home\node>> node privateEncrypt.js
Văn bản gốc: TutorialsPoint!
Buffer mã hóa: