當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。