📜  Node.js crypto.privateEncrypt() 方法

📅  最后修改于: 2022-05-13 01:56:33.366000             🧑  作者: Mango

Node.js crypto.privateEncrypt() 方法

crypto.privateEncrypt() 方法用于使用参数“privateKey”加密缓冲区的规定内容。

句法:

crypto.privateEncrypt( privateKey, buffer )

参数:此方法接受上面提到的两个参数,如下所述:

  • privateKey:可以保存 Object、 字符串、Buffer 或 KeyObject 类型的数据。
    1. 密钥:它是一个“PEM”编码的私钥。它的类型为字符串、 Buffer 和 KeyObject
    2. 密码:它是私钥的可选密码,可以是字符串或缓冲区。
    3. padding它是一个可选的填充值,在 crypto.constants 中定义,可以是 crypto.constants.RSA_NO_PADDING 或 crypto.constants.RSA_PKCS1_PADDING。它是crypto.constants类型。
  • buffer:它包含 Buffer、TypedArray 或 DataView 类型的数据。

返回值:它返回一个带有加密内容的新 Buffer。

下面的例子说明了在 Node.js 中crypto.privateEncrypt() 方法的使用:

示例 1:

// Node.js program to demonstrate the 
// crypto.privateEncrypt() method
  
// Including crypto and fs module
const crypto = require('crypto');
const fs = require("fs");
  
// Using a function generateKeyFiles
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 private key file 
    fs.writeFileSync("private_key", keyPair.privateKey);
}
  
// Generate keys
generateKeyFiles();
  
// Creating a function to encrypt string
function encryptString (plaintext, privateKeyFile) {
    const privateKey = fs.readFileSync(privateKeyFile, "utf8");
  
    // privateEncrypt() method with its parameters
    const encrypted = crypto.privateEncrypt(
          privateKey, Buffer.from(plaintext));
  
    return encrypted.toString("base64");
}
  
// Defining a text to be encrypted
const plainText = "GfG";
  
// Defining encrypted text
const encrypted = encryptString(plainText, "./private_key");
  
// Prints plain text
console.log("Plaintext:", plainText);
  
// Prints encrypted text
console.log("Encrypted: ", encrypted);

输出:

Plaintext: GfG
Encrypted:  c60eR17GTQFkTI1ipTq5qFbYS58lIQqpDiou2UlYeOUE+u7agbtHvvwKaBpzBx4SvTCh5abpaqmyXCyGcUpGc7s=

示例 2:

// Node.js program to demonstrate the 
// crypto.privateEncrypt() method
  
// Including crypto and fs module
const crypto = require('crypto');
const fs = require("fs");
  
// Using a function generateKeyFiles
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 private key file 
    fs.writeFileSync("private_key", keyPair.privateKey);
}
  
// Generate keys
generateKeyFiles();
  
// Creating a function to encrypt string
function encryptString (plaintext, privateKeyFile) {
    const privateKey = fs.readFileSync(privateKeyFile, "utf8");
  
    // privateEncrypt() method with its parameters
    const encrypted = crypto.privateEncrypt(
         privateKey, Buffer.from(plaintext));
  
    // Returns buffer as its not encoded
    return encrypted;
}
  
// Defining a text to be encrypted
const plainText = "GfG";
  
// Defining encrypted text
const encrypted = encryptString(plainText, "./private_key");
  
// Prints plain text
console.log("Plaintext:", plainText);
  
// Prints encrypted text
console.log("Encrypted buffer: ", encrypted);

输出:

Plaintext: GfG
Encrypted buffer:  

参考: https://nodejs.org/api/crypto.html#crypto_crypto_privateencrypt_privatekey_buffer