Phương thức crypto.publicEncrypt() dùng để mã hóa dữ liệu trong tham số buffer bằng khóa công khai được truyền vào. Dữ liệu trả về có thể giải mã bằng khóa riêng tư tương ứng.
Cú pháp
crypto.publicEncrypt(key, buffer)
Tham số
key – Chứa 5 loại dữ liệu kiểu Object, String, Buffer, KeyObject:
key – Khóa PEM encoded công khai hoặc riêng tư.
oaepHash – Hàm hash cho OAEP padding và MGF1. Mặc định:
'sha1'.oaepLabel – Giá trị cho OAEP padding. Không dùng label nếu không chỉ định.
passphrase – Passphrase tùy chọn cho khóa riêng tư.
padding – Giá trị padding trong crypto.constants.
encoding – Loại mã hóa khi buffer, key, oaepLabel, passphrase là chuỗi.
buffer – Dữ liệu cần mã hóa. Kiểu: string, TypedArray, Buffer, ArrayBuffer, DataView.
Ví dụ
Tạo file publicEncrypt.js và sao chép mã. Chạy lệnh:
node publicEncrypt.js
publicEncrypt.js
// Chương trình Node.js minh họa crypto.publicEncrypt()
// Import crypto và fs
const crypto = require('crypto');
const fs = require("fs");
// Hàm tạo file khóa
function generateKeyFiles() {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 520,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: ''
}
});
// Tạo file khóa công khai
fs.writeFileSync("public_key", keyPair.publicKey);
}
// Tạo khóa
generateKeyFiles();
// Hàm mã hóa chuỗi
function encryptString(plaintext, publicKeyFile) {
const publicKey = fs.readFileSync(publicKeyFile, "utf8");
// Gọi publicEncrypt()
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
return encrypted.toString("base64");
}
// Văn bản cần mã hóa
const plainText = "TutorialsPoint";
// Mã hóa
const encrypted = encryptString(plainText, "./public_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("Đã mã hóa: ", encrypted);
Kết quả
C:\home\node>> node publicEncrypt.js Văn bản gốc: TutorialsPoint Đã mã hóa: kgnqPxy/n34z+/5wd7MZiMAL5LrQisTLfZiWoSChXSvxgtifMQaZ56cbF+twA55olM0rFfnuV6qqtc a8SXIHQrk=
Ví dụ 2
Ví dụ trả về buffer thay vì base64.
// Chương trình Node.js minh họa crypto.publicEncrypt()
// Import crypto và fs
const crypto = require('crypto');
const fs = require("fs");
// Hàm tạo file khóa
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();
// Hàm mã hóa chuỗi
function encryptString(plaintext, publicKeyFile) {
const publicKey = fs.readFileSync(publicKeyFile, "utf8");
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
return encrypted;
}
const plainText = "Hello TutorialsPoint!";
const encrypted = encryptString(plainText, "./public_key");
console.log("Văn bản gốc:", plainText);
console.log("Buffer: ", encrypted);
Kết quả
C:\home\node>> node publicEncrypt.js Văn bản gốc: Hello TutorialsPoint! Buffer: