Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Javascript

crypto.publicDecrypt() วิธีการใน Node.js


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

ไวยากรณ์

crypto.publicDecrypt(คีย์ บัฟเฟอร์)

พารามิเตอร์

พารามิเตอร์ข้างต้นอธิบายไว้ด้านล่าง −

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

    • ข้อความรหัสผ่าน - นี่เป็นข้อความรหัสผ่านที่ไม่บังคับสำหรับคีย์ส่วนตัว

    • แพ็ดดิ้ง – เป็นค่าทางเลือกที่กำหนดไว้ใน crypto.constants

    • การเข้ารหัส – นี่คือประเภทของการเข้ารหัสที่ต้องใช้เมื่อค่าบัฟเฟอร์ คีย์ oaepLabel หรือข้อความรหัสผ่านเป็นสตริง

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

ตัวอย่าง

สร้างไฟล์ที่มีชื่อ – publicDecrypt.js และคัดลอกข้อมูลโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ให้ใช้คำสั่งต่อไปนี้เพื่อเรียกใช้โค้ดนี้ดังแสดงในตัวอย่างด้านล่าง −

โหนด publicDecrypt.js

publicDecrypt.js

// crypto.publicDecrypt ตัวอย่างการสาธิต// การนำเข้า crypto, fs และโมดูลพาธ crypto =require('crypto');var fs =require('fs');const path =require('path');/ / การสร้างฟังก์ชันด้านล่างสำหรับสร้างฟังก์ชันคีย์ generateKeyFiles () { const keyPair =crypto.generateKeyPairSync ('rsa', { โมดูลัสLength:520, publicKeyEncoding:{ ประเภท:'spki' รูปแบบ:'pem' }, privateKeyEncoding:{ ประเภท:'pkcs8 ' รูปแบบ:'pem' รหัสลับ:'aes-256-cbc' ข้อความรหัสผ่าน:'' } }); // การสร้างไฟล์พับลิกคีย์ด้วยชื่อด้านล่าง fs.writeFileSync("public_key", keyPair.publicKey); fs.writeFileSync("private_key", keyPair.privateKey);}// Calling Generate keys methodgenerateKeyFiles();// กำลังอ่าน Private และ Public Keyvar private =fs.readFileSync('private_key');var public =fs.readFileSync(' public_key');// การกำหนด datavar data ดั้งเดิม ='ยินดีต้อนรับสู่ TutorialsPoint';console.log("Original Data is:"+data);// การเข้ารหัสข้อมูลโดยใช้ private keyencrypted =crypto.privateEncrypt(private, Buffer.from (data, 'utf8')).toString('base64');// การถอดรหัสข้อมูลโดยใช้ publicKeyoriginalData =crypto.publicDecrypt(public, Buffer.from(encrypted, 'base64'));console.log();// การพิมพ์ที่เข้ารหัส msgconsole.log("เข้ารหัสด้วยคีย์ส่วนตัว:" + เข้ารหัส);console.log();// การพิมพ์ถอดรหัส msgconsole.log("ถอดรหัสด้วยกุญแจสาธารณะ:" + originalData.toString());

ผลลัพธ์

C:\home\node>> node publicDecrypt.jsOriginal Data is:ยินดีต้อนรับสู่ TutorialsPointEncrypted ด้วยไพรเวตคีย์:EFBihrKebXb0gfCF7nTnw82yXpToH5eVBpLc8O5QL/ZgfZ/qJT5I/BejSMwV4NFCrp+AIKnzh2l แบบสอนพิเศษ:

ตัวอย่าง

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

// ตัวอย่างการสาธิต crypto.publicDecrypt // การนำเข้า crypto และ fs modulevar crypto =ต้องการ ('crypto');var fs =ต้องการ ('fs'); // การสร้างฟังก์ชันด้านล่างสำหรับสร้างฟังก์ชันคีย์ generateKeyFiles () { const keyPair =crypto.generateKeyPairSync ('rsa', { โมดูลัสLength:520, publicKeyEncoding:{ ประเภท:'spki', รูปแบบ:'pem' }, privateKeyEncoding:{ ประเภท:'pkcs8', รูปแบบ:'pem', ตัวเลข:'aes -256-cbc', ข้อความรหัสผ่าน:'' } }); // การสร้างไฟล์พับลิกคีย์ด้วยชื่อด้านล่าง fs.writeFileSync("public_key", keyPair.publicKey); fs.writeFileSync("private_key", keyPair.privateKey);}// Calling Generate keys methodgenerateKeyFiles();// กำลังอ่าน Private KeyprivateKey =fs.readFileSync('private_key').toString();var buffer =Buffer.from( 'ยินดีต้อนรับสู่ TutorialsPoint', 'utf8');console.log("Data buffer before encryption")console.log(buffer);// Encrpting the buffer textencrypted =crypto.privateEncrypt(privateKey, buffer);// การพิมพ์ข้อมูล หลังจากการเข้ารหัสคอนโซล.log("ข้อมูลหลังการเข้ารหัส:");console.log(เข้ารหัส);// การอ่านกุญแจสาธารณะpublicKey =fs.readFileSync('public_key').toString();// ถอดรหัสข้อความที่เข้ารหัสโดยใช้คีย์สาธารณะถอดรหัสข้อมูล =crypto.publicDecrypt (publicKey, เข้ารหัส); // พิมพ์ contentconsole.log ดั้งเดิม ("ข้อมูลหลังถอดรหัส:");console.log (decryptedData);

ผลลัพธ์

C:\home\node>> โหนด publicDecrypt.js บัฟเฟอร์ข้อมูลก่อนการเข้ารหัส<บัฟเฟอร์ 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 696e 74>ข้อมูลหลังการเข้ารหัส:<บัฟเฟอร์ a6 9d e3 86 9f 3f 4b b9 3f f7 a6 9c 7c 16 0f 04 b9 c4 16 0b 08 f1 0639 de 32 75 7c 26 88 fa 49 bd 31 6b 4b 4d 02 e6 87 56 ee 9c 95 53 10 8f 28 49f5 ..>ข้อมูลหลังถอดรหัส:<บัฟเฟอร์ 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 696e 74>