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


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

crypto.privateDecrypt()方法用於使用privateKey.buffer解密緩衝區的內容,該私鑰先前已使用相應的公鑰(即crypto.publicEncrypt())進行了加密。

用法:

crypto.privateDecrypt( privateKey, buffer )

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



  • privateKey:它可以保存對象,字符串,緩衝區或鍵對象類型的數據。
    1. oaepHash這是用於“ OAEP”填充的字符串類型的哈希函數。默認值為“ sha1”。
    2. oaepLabel:它是用於“ OAEP”填充的標簽。如果未指定,則不使用標簽。它的類型為Buffer,TypedArray或DataView。
    3. 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.privateDecrypt()方法的使用:

範例1:

// Node.js program to demonstrate the  
// crypto.privateDecrypt() method 
  
// Including the crypto and fs modules 
var crypto = require('crypto'); 
var fs = require('fs'); 
  
// Reading the Public Key 
pubK = fs.readFileSync('pub.key').toString(); 
  
// Passing the text to be encrypted using private key 
var buf = Buffer.from('This is secret code', 'utf8'); 
  
// Encrypting the text 
secretData = crypto.publicEncrypt(pubK, buf); 
  
// Printing the encrypted text 
console.log(secretData); 
  
// Reading the Private key 
privK = fs.readFileSync('priv.key').toString(); 
  
// Decrypting the text using public key 
origData = crypto.privateDecrypt(privK, secretData); 
console.log(); 
  
// Printing the original content 
console.log(origData);

輸出:

<Buffer 58 dd 76 8f cb 25 52 2b e7 3a b2 1b
0f 43 aa e0 df 65 fa 1d 3b 31 6f b7 f9 47 06
d5 f7 72 19 cd 2f 67 66 27 00 bb 43 8e 64 38
07 38 28 aa07 59 b4 60 ... >

<Buffer 54 68 69 73 20 69 73 20 73 65 63 72
65 74 20 63 6f 64 65 >

範例2:

// Node.js program to demonstrate the  
// crypto.privateDecrypt() 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:530, 
        publicKeyEncoding:{ 
            type:'spki', 
            format:'pem'
        }, 
        privateKeyEncoding:{ 
        type:'pkcs8', 
        format:'pem', 
        cipher:'aes-256-cbc', 
        passphrase:''
        } 
    }); 
       
    // Creating public and private key file  
    fs.writeFileSync("public_key", keyPair.publicKey); 
    fs.writeFileSync("private_key", keyPair.privateKey); 
} 
  
// 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"); 
} 
  
// Creating a function to decrypt string 
function decryptString (ciphertext, privateKeyFile) { 
    const privateKey = fs.readFileSync(privateKeyFile, "utf8"); 
  
    // privateDecrypt() method with its parameters 
    const decrypted = crypto.privateDecrypt( 
       privateKey, Buffer.from(ciphertext, "base64")); 
  
    return decrypted.toString("utf8"); 
} 
  
// Defining a text to be encrypted 
const plainText = "Geeks!"; 
  
// Defining encrypted text 
const encrypted = encryptString(plainText, "./public_key"); 
  
// Prints plain text 
console.log("Plaintext:", plainText); 
console.log(); 
  
// Prints buffer of encrypted content 
console.log("Encrypted Text:", encrypted); 
console.log(); 
  
// Prints buffer of decrypted content 
console.log("Decrypted Text:",  
    decryptString(encrypted, "private_key"));

輸出:

Plaintext:Geeks!

Encrypted Text: ACks6H7InpaeGdI4w9MObyD73YB7N1V0nVsG5Jl10SNeH3no6gfgjeD4ZFsSFhCXzFkognMGbRNsg0BReVOHxRs7eQ==

Decrypted Text:Geeks!

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




相關用法


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