เมธอด crypto.privateDecrypt() ใช้สำหรับถอดรหัสเนื้อหาข้อมูลที่กำหนดโดยใช้กุญแจส่วนตัวที่ส่งผ่านพารามิเตอร์ ซึ่งเคยถูกเข้ารหัสด้วยกุญแจสาธารณะที่สอดคล้องกันผ่านเมธอด crypto.publicEncrypt()
ไวยากรณ์
crypto.privateDecrypt(privateKey, buffer)
พารามิเตอร์
พารามิเตอร์ข้างต้นอธิบายดังนี้:
key – สามารถมีข้อมูล 5 ประเภทดังต่อไปนี้ ประเภท Object, String, Buffer หรือ KeyObject:
oaepHash – ฟิลด์นี้มีฟังก์ชันแฮชที่ใช้สำหรับ OAEP padding และ MGF1 ค่าเริ่มต้นคือ
'sha1'oaepLabel – ฟิลด์นี้มีค่าสำหรับ OAEP padding ไม่ใช้ label หากไม่ระบุ
padding – ค่าตัวเลือกที่กำหนดใน
crypto.constants
buffer – ฟิลด์นี้มีเนื้อหาข้อมูลที่จะถอดรหัส ประเภทบัฟเฟอร์ที่เป็นไปได้: string, TypedArray, Buffer, ArrayBuffer, DataView
ตัวอย่าง
สร้างไฟล์ชื่อ privateDecrypt.js และคัดลอกโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ใช้คำสั่งต่อไปนี้เพื่อรันโค้ด:
node privateDecrypt.js
privateDecrypt.js
// โปรแกรม Node.js สาธิตการทำงานของเมธอด crypto.privateDecrypt()
// นำเข้าโมดูล crypto และ fs
const crypto = require('crypto');
const fs = require("fs");
// อ่านกุญแจสาธารณะ
// สามารถสร้างกุญแจเหล่านี้ได้ด้วย generateKeyPair()
publicKey = fs.readFileSync('public_key').toString();
// ส่งข้อความด้านล่างเพื่อเข้ารหัส
var buf = Buffer.from('Hello TutorialsPoint', 'utf8');
// เข้ารหัสข้อความด้านบน
secretData = crypto.publicEncrypt(publicKey, buf);
// แสดงข้อความที่เข้ารหัส
console.log(secretData);
// อ่านกุญแจส่วนตัว
privateKey = fs.readFileSync('private_key').toString();
// ถอดรหัสข้อความที่เข้ารหัส
origData = crypto.privateDecrypt(privateKey, secretData);
console.log();
// แสดงข้อความต้นฉบับเป็นบัฟเฟอร์
console.log(origData);
ผลลัพธ์
C:\home\node>> node privateDecrypt.js <Buffer 15 4b 51 02 e7 1c ec 11 20 55 ee 92 b2 18 7b ce f1 e1 97 bd b7 0d 54 21 18 ea 0c e7 cd 51 36 f5 13 df fb 41 c1 63 bb ac ee 94 12 df f6 d6 04 b1 9c 11 ... > <Buffer 48 65 6c 6c 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 69 6e 74>
ตัวอย่างเพิ่มเติม
ลองดูตัวอย่างอีกหนึ่งกรณี:
// โปรแกรม Node.js สาธิตการทำงานของเมธอด crypto.privateDecrypt()
// นำเข้าโมดูล 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("public_key", keyPair.publicKey);
fs.writeFileSync("private_key", keyPair.privateKey);
}
// เรียกเมธอดสร้างกุญแจ
generateKeyFiles();
// เข้ารหัสสตริงที่ส่งผ่าน
function encryptString(plaintext, publicKeyFile) {
const publicKey = fs.readFileSync(publicKeyFile, "utf8");
// เข้ารหัสข้อมูลโดยใช้เมธอด publicEncrypt() และกุญแจสาธารณะ
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
return encrypted.toString("base64");
}
// ถอดรหัสสตริงที่ส่งผ่านด้วยกุญแจส่วนตัว
function decryptString(ciphertext, privateKeyFile) {
const privateKey = fs.readFileSync(privateKeyFile, "utf8");
// ถอดรหัสข้อมูลโดยใช้เมธอด privateDecrypt() และกุญแจส่วนตัวที่สอดคล้อง
const decrypted = crypto.privateDecrypt(
{
key: privateKey,
passphrase: ''
},
Buffer.from(ciphertext, "base64")
);
return decrypted.toString("utf8");
}
// ข้อมูลต่อไปนี้จะถูกเข้ารหัสและถอดรหัส
const plainText = "TutorialsPoint!";
// เรียกเมธอดด้านล่างเพื่อเข้ารหัสสตริง
const encrypted = encryptString(plainText, "./public_key");
// แสดงข้อความต้นฉบับ
console.log("Plaintext:", plainText);
console.log();
// แสดงข้อความที่เข้ารหัส
console.log("Encrypted Text: ", encrypted);
console.log();
// แสดงข้อความที่ถอดรหัส
console.log("Decrypted Text: ", decryptString(encrypted, "private_key"));
ผลลัพธ์
C:\home\node>> node privateDecrypt.js Plaintext: TutorialsPoint! Encrypted Text: AbSrqG4qFBG1q9KUBt8ddJxk9uNanOHXqY19N0mNHx0fm4M119dZVhcNrAvM8UaIRJvh7AsdWyjv1scPA25KpeJuJQ== Decrypted Text: TutorialsPoint!