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

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


crypto.privateEncrypt() ใช้สำหรับเข้ารหัสเนื้อหาข้อมูลที่กำหนดโดยใช้พารามิเตอร์คีย์ส่วนตัวที่กำหนดซึ่งส่งผ่านในฟังก์ชัน

ไวยากรณ์

crypto.privateEncrypt(privateKey, บัฟเฟอร์)

พารามิเตอร์

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

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

    • คีย์ – คีย์นี้เป็นคีย์ส่วนตัวที่เข้ารหัส 'PEM' คีย์อาจเป็นประเภท string, buffer หรือ KeyObject

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

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

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

ตัวอย่าง

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

โหนด privateEncrypt.js

privateEncrypt.js

// โปรแกรม Node.js เพื่อแสดงการไหลของวิธี crypto.privateEncrypt() // การนำเข้า crypto และ fs moduleconst crypto =require('crypto');const fs =require("fs");// การสร้างคีย์ ไฟล์โดยใช้ generateKeyPairSync () methodfunction generateKeyFiles () { const keyPair =crypto.generateKeyPairSync ('rsa', { โมดูลัสLength:530, publicKeyEncoding:{ ประเภท:'spki', รูปแบบ:'pem' }, privateKeyEncoding:{ ประเภท:'pkcs8' , รูปแบบ:'pem', รหัสลับ:'aes-256-cbc', ข้อความรหัสผ่าน:'' } }); // เขียนคีย์ในไฟล์ต่อไปนี้ fs.writeFileSync("private_key", keyPair.privateKey);}// Calling Generate keys methodgenerateKeyFiles();// การเข้ารหัสฟังก์ชันสตริงที่วาง encryptString (ข้อความธรรมดา, publicKeyFile) { const publicKey =fs. readFileSync (publicKeyFile, "utf8"); // การเข้ารหัสข้อมูลโดยใช้เมธอด privateEncrypt() และคีย์สาธารณะ const encrypted =crypto.privateEncrypt(publicKey, Buffer.from(plaintext)); return encrypted.toString("base64");}// ข้อมูลต่อไปนี้จะถูกเข้ารหัสและถอดรหัสconst plainText ="TutorialsPoint!";// การเรียกใช้เมธอดด้านล่างเพื่อเข้ารหัส stringconst encrypted =encryptString(plainText, "./private_key");/ / การพิมพ์ textconsole.log("Plaintext:", plainText);// การพิมพ์ textconsole.log ที่เข้ารหัสแล้ว ("Encrypted Text:", encrypted);console.log();

ผลลัพธ์

C:\home\node>> โหนด privateEncrypt.jsPlaintext:TutorialsPoint!Encrypted Text:AhphoL+l/e739LkdfCAm2XuiddgTG7jjdGlLviiRqD4LyTtxJmpkgq5bSkyI7Og4XlBtszBB9HLxGYRHTA=j850> 

ตัวอย่าง

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

// โปรแกรม Node.js เพื่อแสดงการไหลของวิธี crypto.privateEncrypt() // การนำเข้า crypto และ fs moduleconst crypto =require('crypto');const fs =require("fs");// การสร้างคีย์ ไฟล์โดยใช้ generateKeyPairSync () methodfunction generateKeyFiles () { const keyPair =crypto.generateKeyPairSync ('rsa', { โมดูลัสLength:530, publicKeyEncoding:{ ประเภท:'spki', รูปแบบ:'pem' }, privateKeyEncoding:{ ประเภท:'pkcs8' , รูปแบบ:'pem', รหัสลับ:'aes-256-cbc', ข้อความรหัสผ่าน:'' } }); // เขียนคีย์ในไฟล์ต่อไปนี้ fs.writeFileSync("private_key", keyPair.privateKey);}// Calling Generate keys methodgenerateKeyFiles();// การเข้ารหัสฟังก์ชันสตริงที่วาง encryptString (ข้อความธรรมดา, publicKeyFile) { const publicKey =fs. readFileSync (publicKeyFile, "utf8"); // การเข้ารหัสข้อมูลโดยใช้เมธอด privateEncrypt() และคีย์สาธารณะ const encrypted =crypto.privateEncrypt(publicKey, Buffer.from(plaintext)); return encrypted;}// ข้อมูลต่อไปนี้จะถูกเข้ารหัสและถอดรหัสconst plainText ="TutorialsPoint!";// การเรียกใช้เมธอดด้านล่างเพื่อเข้ารหัส stringconst encrypted =encryptString(plainText, "./private_key");// การพิมพ์ textconsole.log ธรรมดา ("ข้อความธรรมดา:", ข้อความธรรมดา); // การพิมพ์ textconsole.log ที่เข้ารหัสแล้ว ("บัฟเฟอร์ที่เข้ารหัส:", เข้ารหัส);console.log();

ผลลัพธ์

C:\home\node>> โหนด privateEncrypt.jsPlaintext:TutorialsPoint!Encrypted Buffer:<บัฟเฟอร์ 00 a2 ce 5d 27 d7 15 ec a7 bc 5b 12 73 62 92 82 6df9 6b 82 38 06 dd bf b3 b8 1f 23 b4 d9 e9 aa 1c 50 0f c9 d7 12 f9 17 db 91 dc05 46 b0 0c 08 14 50 ...>