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

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


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

ไวยากรณ์

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

พารามิเตอร์

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

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

    • คีย์ – ฟิลด์นี้มีคีย์สาธารณะหรือคีย์ส่วนตัวที่เข้ารหัส PEM อาจเป็นประเภท string, buffer หรือ keyObject

    • oaepHash – ช่องนี้มีฟังก์ชันแฮชที่ใช้สำหรับ OAEP padding และ MGF1 ค่าเริ่มต้นคือ:'sha1'

    • oaepLabel – ฟิลด์นี้มีค่าสำหรับการเติม OAEP ไม่มีการใช้ฉลากหากไม่มีการระบุ

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

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

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

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

ตัวอย่าง

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

โหนด publicEncrypt.js

publicEncrypt.js

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

ผลลัพธ์

C:\home\node>> โหนด publicEncrypt.jsPlaintext:TutorialsPointEncrypted:kgnqPxy/n34z+/5wd7MZiMAL5LrQisTLfZiWoSChXSvxgtifMQaZ56cbF+twA55olM0rFfnuV6qqtca8preSXI 

ตัวอย่าง

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

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

ผลลัพธ์

C:\home\node>> โหนด publicEncrypt.jsPlaintext:สวัสดี TutorialsPoint!บัฟเฟอร์:<บัฟเฟอร์ 33 0b 54 96 0e 8f 34 6c b4 d5 7a cf d4 d5 ef 7b 7e c5 ec 97cf 75 05 07 df 5a 9e d4 3d cc 3e bb 55 e7 50 1b 64 f0 c8 89 19 61 81 99 e5 8810 4a 3b 5a ...>