Node.js 的 crypto.privateEncrypt() 方法


crypto.privateEncrypt() 用于通过函数中传递的给定私钥参数对给定数据内容进行加密。

语法

crypto.privateEncrypt(privateKey, buffer)

参数

上述参数描述如下 −

  • privateKey – 它可以包含以下数据类型 – Object、String、Buffer 或 KeyObject。

    • key – 这个键是 'PEM' 编码的私钥。此键可以是字符串、缓冲区或 KeyObject。

    • passphrase – 这是私钥的可选密码值。

    • padding – 这是在 crypto.constants 中定义的可选值。

  • buffer – 此字段包含要解密的数据内容。可能的缓冲区类型是:string、TypedArray、Buffer、ArrayBuffer、DataView。

示例

创建一个名为 – privateEncrypt.js 的文件并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 −

node privateEncrypt.js

privateEncrypt.js

// Node.js program to demonstrate the flow of crypto.privateEncrypt() method

// Importing crypto and fs module
const crypto = require('crypto');
const fs = require("fs");

// Generating key files using generateKeyPairSync() method
function generateKeyFiles() {

   const keyPair = crypto.generateKeyPairSync('rsa', {
      modulusLength: 530,
      publicKeyEncoding: {
         type: 'spki',
         format: 'pem'
      },
      privateKeyEncoding: {
         type: 'pkcs8',
         format: 'pem',
         cipher: 'aes-256-cbc',
         passphrase: ''
      }
   });

   // Writing the keys in the following files
   fs.writeFileSync("private_key", keyPair.privateKey);
}

// Calling Generate keys method
generateKeyFiles();

// Encrypting the pased string
function encryptString (plaintext, publicKeyFile) {
   const publicKey = fs.readFileSync(publicKeyFile, "utf8");

   // Encrypting data using privateEncrypt() method and a public key
   const encrypted = crypto.privateEncrypt(
      publicKey, Buffer.from(plaintext));

   return encrypted.toString("base64");
}

// Following data will be encrypted and decrypted
const plainText = "TutorialsPoint!";

// Calling the below method to encrypt string
const encrypted = encryptString(plainText, "./private_key");

// Printing the plain text
console.log("Plaintext:", plainText);

// Printing the encrypted text
console.log("Encrypted Text: ", encrypted);
console.log();

输出

C:\home
ode>> node privateEncrypt.js Plaintext: TutorialsPoint! Encrypted Text: AhphoL+l/e739LkdfCAm2XuiddgTG7jjdGlLviiRqD4LyTtxJmpkgq5bSkyI7Og4XlBtszBB9HLQRH T5j850ZAxGYA==

示例

让我们再看一个示例。

// Node.js program to demonstrate the flow of crypto.privateEncrypt() method

// Importing crypto and fs module
const crypto = require('crypto');
const fs = require("fs");

// Generating key files using generateKeyPairSync() method
function generateKeyFiles() {

   const keyPair = crypto.generateKeyPairSync('rsa', {
      modulusLength: 530,
      publicKeyEncoding: {
         type: 'spki',
         format: 'pem'
      },
      privateKeyEncoding: {
         type: 'pkcs8',
         format: 'pem',
         cipher: 'aes-256-cbc',
         passphrase: ''
      }
   });

   // Writing the keys in the following files
   fs.writeFileSync("private_key", keyPair.privateKey);
}

// Calling Generate keys method
generateKeyFiles();

// Encrypting the pased string
function encryptString (plaintext, publicKeyFile) {
   const publicKey = fs.readFileSync(publicKeyFile, "utf8");

   // Encrypting data using privateEncrypt() method and a public key
   const encrypted = crypto.privateEncrypt(
      publicKey, Buffer.from(plaintext));

   return encrypted;
}

// Following data will be encrypted and decrypted
const plainText = "TutorialsPoint!";

// Calling the below method to encrypt string
const encrypted = encryptString(plainText, "./private_key");

// Printing the plain text
console.log("Plaintext:", plainText);

// Printing the encrypted text
console.log("Encrypted Buffer: ", encrypted);
console.log();

输出

C:\home
ode>> node privateEncrypt.js Plaintext: TutorialsPoint! Encrypted Buffer: <Buffer 00 a2 ce 5d 27 d7 15 ec a7 bc 5b 12 73 62 92 82 6d f9 6b 82 38 06 dd bf b3 b8 1f 23 b4 d9 e9 aa 1c 50 0f c9 d7 12 f9 17 db 91 dc 05 46 b0 0c 08 14 50 ... >

更新于: 2021-5-20

597 次浏览

开启你的 职业 生涯

完成课程后获得认证

开始学习
广告