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


Node.js crypto.publicEncrypt()用法及代碼示例


crypto.publicEncrypt()方法是加密模塊的內置應用程序編程接口,用於使用參數“ key ”對緩衝區中規定的內容進行加密。

用法:

crypto.publicEncrypt( key, buffer )

參數:該方法接受上述和以下所述的兩個參數:



  • key:此參數保存對象,字符串,緩衝區或KeyObject類型的數據,並包含五個參數,如下所示:
    1. key:它是“ PEM”編碼的公鑰或私鑰。它的類型為字符串,緩衝區和鍵對象。
    2. oaepHash:這是用於“ OAEP”填充的字符串類型的哈希函數。默認值為“ sha1”。
    3. oaepLabel:它是用於“ OAEP”填充的標簽。如果未指定,則不使用標簽。它的類型為Buffer,TypedArray或DataView。
    4. passphrase:這是私鑰的可選密碼短語,它可以是字符串或緩衝區。
    5. padding:它是在crypto.constants中定義的可選填充值,可以是crypto.constants.RSA_NO_PADDING,crypto.constants.RSA_PKCS1_PADDING或crypto.constants.RSA_PKCS1_OAEP_PADDING。它是crypto.constants類型。
  • buffer:它的類型為Buffer,TypedArray或DataView。

返回值:它返回帶有加密內容的新緩衝區。

以下示例說明了Node.js中crypto.publicEncrypt()方法的使用:

範例1:

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

輸出:

Plaintext:GfG
Encrypted: l0touwFaNv1DIgPE365VQD0G4rg+IbRD5G6IBQ1arLgWtFOStKO7duYJ6/JzlOJl3eBG7obqzAEJ0V2WrxtYRTg=

範例2:

// Node.js program to demonstrate the  
// crypto.publicEncrypt() 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 public key file  
    fs.writeFileSync("public_key", keyPair.publicKey); 
} 
  
// Generate keys 
generateKeyFiles(); 
  
// Creating a function to encrypt string 
function encryptString (plaintext, publicKeyFile) { 
    const publicKey = fs.readFileSync(publicKeyFile, "utf8"); 
  
    // publicEncrypt() method with its parameters 
    const encrypted = crypto.publicEncrypt( 
             publicKey, Buffer.from(plaintext)); 
    return encrypted; 
} 
  
// Defining a text to be encrypted 
const plainText = "Hello!"; 
  
// Defining encrypted text 
const encrypted = encryptString(plainText, "./public_key"); 
  
// Prints plain text 
console.log("Plaintext:", plainText); 
  
// Prints buffer 
console.log("Buffer:", encrypted);

輸出:

Plaintext:Hello!
Buffer:<Buffer 1b 2a c7 4d 10 44 45 8e 9d e6
53 9d 8a 5e 39 0f ea e2 96 00 d7 d3 b3 eb 54 7e
74 7d a4 62 b8 eb 68 85 cb 8e 85 a5 f7 71 2f b7
93 d6 14 1c 38 cd 45 85 ... >

參考: https://nodejs.org/api/crypto.html#crypto_crypto_publicencrypt_key_buffer




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | crypto.publicEncrypt() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。