当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js crypto.privateEncrypt()用法及代码示例


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

用法:

crypto.privateEncrypt( privateKey, buffer )

参数:该方法接受上述和以下所述的两个参数:



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

返回值:它返回带有加密内容的新缓冲区。

以下示例说明了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: <Buffer 14 e5 84 05 2f b5 59 64
22 d2 19 99 f9 66 e5 18 50 76 27 df 0b e6 9f 50 1d a2
51 6a 93 21 04 7b 8f 50 ba 11 82 fd 3c 6e c0 81 be 58
f9 d6 a6 c7 19 da ... >

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




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | crypto.privateEncrypt() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。