Node.js 中的 crypto.privateDecrypt() 方法


crypto.privateDecrypt() 用于解密给定数据内容,方法是使用先前使用 crypto.publicEncrypt() 方法与相应的公钥一起加密的参数中传递的私钥。

语法

crypto.privateDecrypt(privateKey, buffer)

参数

以上参数的描述如下 -

  • key – 它可以包含以下类型的 5 种数据 - Object、String、Buffer 或 KeyObject。

    • oaepHash – 此字段包含要用于 OAEP 填充和 MGF1 的哈希函数。默认值为: 'sha1'。

    • oaepLabel – 此字段包含 OAEP 填充的值。如果没有指定,则不使用标签。

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

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

示例

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

node privateDecrypt.js

privateDecrypt.js

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

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

// Reading the Public Key.
//You can generate these keys using generateKeyPair()
publicKey = fs.readFileSync('public_key').toString();

// Passing the below text to be encrypted
var buf = Buffer.from('Hello TutorialsPoint', 'utf8');

// Encrypting the above text
secretData = crypto.publicEncrypt(publicKey, buf);

// Printing the encrypted text
console.log(secretData);

// Reading the Private key
privateKey = fs.readFileSync('private_key').toString();

// Decrypting the encrypted text
origData = crypto.privateDecrypt(privateKey, secretData);
console.log();

// Printing the original text as buffer
console.log(origData);

输出

C:\home
ode>> node privateDecrypt.js <Buffer 15 4b 51 02 e7 1c ec 11 20 55 ee 92 b2 18 7b ce f1 e1 97 bd b7 0d 54 21 18 ea 0c e7 cd 51 36 f5 13 df fb 41 c1 63 bb ac ee 94 12 df f6 d6 04 b1 9c 11 ... > <Buffer 48 65 6c 6c 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 69 6e 74>

示例

我们再来看一个示例。

// Node.js program to demonstrate the flow of crypto.privateDecrypt() 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("public_key", keyPair.publicKey);
   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 publicEncrypt() method and a public key
   const encrypted = crypto.publicEncrypt(
      publicKey, Buffer.from(plaintext));

   return encrypted.toString("base64");
}

// Decrypting the passed string with private Key
function decryptString (ciphertext, privateKeyFile) {
   const privateKey = fs.readFileSync(privateKeyFile, "utf8");

   // Decrypting data using privateDecrypt() method
   // and the corresponding private key
   const decrypted = crypto.privateDecrypt(
      {
         key: privateKey,
         passphrase: '',
      },
      Buffer.from(ciphertext, "base64")
   );
   return decrypted.toString("utf8");
}

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

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

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

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

// Printing the decrypted text
console.log("Decrypted Text: ",
   decryptString(encrypted, "private_key"));

输出

C:\home
ode>> node privateDecrypt.js Plaintext: TutorialsPoint! Encrypted Text: AbSrqG4qFBG1q9KUBt8ddJxk9uNanOHXqY19N0mNHx0fm4M119dZVhcNrAvM8UaIRJvh7AsdWyjv1s cPA25KpeJuJQ== Decrypted Text: TutorialsPoint!

更新日期:20-5-2021

2 千 + 访问量

开启你的 职业生涯

通过完成课程获得认证

现在开始
广告