Phương thức crypto.publicDecrypt() dùng để giải mã dữ liệu trong buffer bằng khóa công khai. Buffer này đã được mã hóa bằng khóa riêng tư tương ứng (dùng crypto.privateEncrypt()).
Cú pháp
crypto.publicDecrypt(key, buffer)
Tham số
key – Chứa 5 loại dữ liệu kiểu Object, String, Buffer, KeyObject:
passphrase – Passphrase tùy chọn cho khóa riêng tư.
padding – Padding trong crypto.constants.
encoding – Mã hóa khi buffer, key, oaepLabel, passphrase là chuỗi.
buffer – Dữ liệu cần giải mã. Kiểu: string, TypedArray, Buffer, ArrayBuffer, DataView.
Ví dụ
Tạo file publicDecrypt.js và sao chép mã. Chạy lệnh:
node publicDecrypt.js
publicDecrypt.js
// Demo crypto.publicDecrypt
// Import crypto, fs, path
var crypto = require('crypto');
var fs = require('fs');
const path = require('path');
// 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);
fs.writeFileSync("private_key", keyPair.privateKey);
}
// Gọi tạo khóa
generateKeyFiles();
// Đọc khóa riêng và công khai
var private = fs.readFileSync('private_key');
var public = fs.readFileSync('public_key');
// Dữ liệu gốc
var data = 'Welcome to TutorialsPoint';
console.log("Dữ liệu gốc: " + data);
// Mã hóa bằng khóa riêng
encrypted = crypto.privateEncrypt(private, Buffer.from(data, 'utf8')).toString('base64');
// Giải mã bằng khóa công khai
originalData = crypto.publicDecrypt(public, Buffer.from(encrypted, 'base64'));
console.log();
// In dữ liệu mã hóa
console.log("Mã hóa bằng khóa riêng: " + encrypted);
console.log();
// In dữ liệu giải mã
console.log("Giải mã bằng khóa công khai: " + originalData.toString());
Kết quả
C:\home\node>> node publicDecrypt.js Dữ liệu gốc: Welcome to TutorialsPoint Mã hóa bằng khóa riêng: EFBihrKebXb0gfCF7nTnw82yXpToH5eVBpLc8O5QL/ZgfZ/qJT5I/BejSMwV4NFCp+AIKnz2lrjmFh IhnpZWbF4= Giải mã bằng khóa công khai: Welcome to TutorialsPoint
Ví dụ 2
Ví dụ làm việc trực tiếp với buffer.
// Demo crypto.publicDecrypt
// Import crypto và fs
var crypto = require('crypto');
var 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);
fs.writeFileSync("private_key", keyPair.privateKey);
}
generateKeyFiles();
// Đọc khóa riêng
privateKey = fs.readFileSync('private_key').toString();
var buffer = Buffer.from('Welcome to TutorialsPoint', 'utf8');
console.log("Buffer trước mã hóa")
console.log(buffer);
// Mã hóa buffer
encrypted = crypto.privateEncrypt(privateKey, buffer);
console.log("Dữ liệu sau mã hóa: ");
console.log(encrypted);
// Đọc khóa công khai
publicKey = fs.readFileSync('public_key').toString();
// Giải mã
decryptedData = crypto.publicDecrypt(publicKey, encrypted);
console.log("Dữ liệu sau giải mã: ");
console.log(decryptedData);
Kết quả
C:\home\node>> node publicDecrypt.js Buffer trước mã hóaDữ liệu sau mã hóa: Dữ liệu sau giải mã: