Node.js 中的 crypto.publicEncrypt() 方法
crypto.publicEncrypt() 用于使用参数中传递的公钥加密缓冲区参数中给定的数据。返回的数据可以使用相应的私钥解密。
语法
crypto.publicEncrypt(key, buffer)
参数
上述参数描述如下:
key – 它可以包含以下 5 种类型的数据:对象、字符串、缓冲区或 KeyObject。
key – 此字段包含 PEM 编码的公钥或私钥。它可以是字符串、缓冲区或 keyObject 类型。
oaepHash – 此字段包含要用于 OAEP 填充和 MGF1 的哈希函数。默认值为:“sha1”。
oaepLabel – 此字段包含 OAEP 填充的值。如果未指定,则不使用标签。
passphrase – 这是私钥的可选密码。
padding – 这是在 crypto.constants 中定义的可选值。
encoding – 当 buffer、key、oaepLabel 或 passphrase 值为字符串时,需要使用的编码类型。
buffer – 此字段包含要加密的数据内容。可能的缓冲区类型为:字符串、TypedArray、Buffer、ArrayBuffer、DataView。
示例
创建一个名为 publicEncrypt.js 的文件并复制下面的代码片段。创建文件后,使用以下命令运行此代码,如以下示例所示:
node publicEncrypt.js
publicEncrypt.js
// Node.js program to demonstrate the flow of crypto.publicEncrypt() method // Importing crypto and fs module const crypto = require('crypto'); const fs = require("fs"); // Creating below function for generating keys function generateKeyFiles() { const keyPair = crypto.generateKeyPairSync('rsa', { modulusLength: 520, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: '' } }); // Creating the public key file with the below name fs.writeFileSync("public_key", keyPair.publicKey); } // Generating keys generateKeyFiles(); // Encrypting string using the below function function encryptString (plaintext, publicKeyFile) { const publicKey = fs.readFileSync(publicKeyFile, "utf8"); //Calling publicEncrypt() with below parameters const encrypted = crypto.publicEncrypt( publicKey, Buffer.from(plaintext)); return encrypted.toString("base64"); } // Text that will be encrypted const plainText = "TutorialsPoint"; // Defining the encrypted text const encrypted = encryptString(plainText, "./public_key"); // Printing plain text console.log("Plaintext:", plainText); // Printing the encrypted text console.log("Encrypted: ", encrypted);
输出
C:\home
ode>> node publicEncrypt.js Plaintext: TutorialsPoint Encrypted: kgnqPxy/n34z+/5wd7MZiMAL5LrQisTLfZiWoSChXSvxgtifMQaZ56cbF+twA55olM0rFfnuV6qqtc a8SXIHQrk=
示例
让我们再看一个例子。
// Node.js program to demonstrate the flow of crypto.publicEncrypt() method // Importing crypto and fs module const crypto = require('crypto'); const fs = require("fs"); // Creating below function for generating keys function generateKeyFiles() { const keyPair = crypto.generateKeyPairSync('rsa', { modulusLength: 520, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: '' } }); // Creating the public key file fs.writeFileSync("public_key", keyPair.publicKey); } // Generating keys generateKeyFiles(); // Encrypting string using the below function function encryptString (plaintext, publicKeyFile) { const publicKey = fs.readFileSync(publicKeyFile, "utf8"); //Calling publicEncrypt() with below parameters const encrypted = crypto.publicEncrypt( publicKey, Buffer.from(plaintext)); return encrypted; } // Text that will be encrypted const plainText = "Hello TutorialsPoint!"; // Defining the encrypted text const encrypted = encryptString(plainText, "./public_key"); // Printing plain text console.log("Plaintext:", plainText); // Printing the encrypted buffer console.log("Buffer: ", encrypted);
输出
C:\home
ode>> node publicEncrypt.js Plaintext: Hello TutorialsPoint! Buffer: <Buffer 33 0b 54 96 0e 8f 34 6c b4 d5 7a cf d4 d5 ef 7b 7e c5 ec 97 cf 75 05 07 df 5a 9e d4 3d cc 3e bb 55 e7 50 1b 64 f0 c8 89 19 61 81 99 e5 88 10 4a 3b 5a ... >
广告