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


crypto.publicDecrypt() 用于使用公钥对缓冲区中给定数据进行解密。此缓冲区是使用相应的私钥(即 crypto.privateEncrypt() 方法)加密的。

语法

crypto.publicDecrypt(key, buffer)

参数

上述参数说明如下 −

  • key – 它可以包含以下 5 种以下类型的类型的数据 – 对象、字符串、缓冲区或 KeyObject。

    • passphrase - 这是私钥的可选密码短语。

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

    • encoding – 这是在缓冲区、键、oaepLabel 或密码短语值是字符串时需要使用的编码类型。

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

示例

使用名称创建文件 – publicDecrypt.js 并复制以下代码段。创建文件后,使用以下命令运行此代码,如下面的示例中所示 −

node publicDecrypt.js

publicDecrypt.js

// crypto.publicDecrypt Demo Example

// Importing the crypto, fs and path module
var crypto = require('crypto');
var fs = require('fs');
const path = require('path');

// 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);
   fs.writeFileSync("private_key", keyPair.privateKey);
}

// Calling Generate keys method
generateKeyFiles();

// Reading the Private and Public Key
var private = fs.readFileSync('private_key');
var public = fs.readFileSync('public_key');

// Defining the original data
var data = 'Welcome to TutorialsPoint';
console.log("Original Data is: "+data);

// Encrypting the data using private key
encrypted = crypto.privateEncrypt(private, Buffer.from(data, 'utf8')).toString('base64');

// Decrypting the data usig publicKey
originalData = crypto.publicDecrypt(public, Buffer.from(encrypted, 'base64'));

console.log();

// Printing encrypted msg
console.log("Encrypted with private key: " + encrypted);

console.log();

// Printing decrypted msg
console.log("Decrypted with public key: " + originalData.toString());

输出

C:\home
ode>> node publicDecrypt.js Original Data is: Welcome to TutorialsPoint Encrypted with private key: EFBihrKebXb0gfCF7nTnw82yXpToH5eVBpLc8O5QL/ZgfZ/qJT5I/BejSMwV4NFCp+AIKnz2lrjmFh IhnpZWbF4= Decrypted with public key: Welcome to TutorialsPoint

示例

让我们再看一个示例。

// crypto.publicDecrypt Demo Example

// Importing the crypto and fs module
var crypto = require('crypto');
var 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);
   fs.writeFileSync("private_key", keyPair.privateKey);
}

// Calling Generate keys method
generateKeyFiles();

// Reading the Private Key
privateKey = fs.readFileSync('private_key').toString();
var buffer = Buffer.from('Welcome to TutorialsPoint', 'utf8');
console.log("Data buffer before encryption")
console.log(buffer);

// Encrpting the buffer text
encrypted = crypto.privateEncrypt(privateKey, buffer);

// Printing the data after encryption
console.log("Data after encryption: ");
console.log(encrypted);

// Reading the Public key
publicKey = fs.readFileSync('public_key').toString();

// Decrypting the encrypted text using public key
decryptedData = crypto.publicDecrypt(publicKey, encrypted);

// Printing the original content
console.log("Data after decryption: ");
console.log(decryptedData);

输出

C:\home
ode>> node publicDecrypt.js Data buffer before encryption <Buffer 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 69 6e 74> Data after encryption: <Buffer a6 9d e3 86 9f 3f 4b b9 3f f7 a6 9c 7c 16 0f 04 b9 c4 16 0b 08 f1 06 39 de 32 75 7c 26 88 fa 49 bd 31 6b 4b 4d 02 e6 87 56 ee 9c 95 53 10 8f 28 49 f5 ... > Data after decryption: <Buffer 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 50 6f 69 6e 74>

更新于: 2021 年 5 月 20 日

579 次浏览

开启你的 职业生涯

完成课程认证

开始吧
广告