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


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


crypto.publicDecrypt()方法用于使用先前已使用相应私钥加密的 key (即crypto.privateEncrypt()方法)解密缓冲区的内容。

用法:

crypto.publicDecrypt( key, buffer )

参数:此方法接受两个参数,如avobe所述,如下所述:



  • key:它的类型为Object,string,Buffer或KeyObject,并包含以下两个参数:
    1. passphrase:对于私钥,它是可选的,其类型为字符串或缓冲区。
    2. padding:它是在crypto.constants中定义的可选填充值,可以是crypto.constants.RSA_NO_PADDING或crypto.constants.RSA_PKCS1_PADDING。它是crypto.constants类型。
  • buffer它的类型为Buffer,TypedArray或DataView。

返回类型:它返回带有解密内容的新缓冲区。

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

范例1:

// Node.js program to demonstrate the  
// crypto.publicDecrypt() method 
  
// Including crypto, path, and fs module 
var crypto = require('crypto'); 
var fs = require('fs'); 
const path = require('path'); 
  
// Generating key files 
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 and private key file  
    fs.writeFileSync("public_key", keyPair.publicKey); 
    fs.writeFileSync("private_key", keyPair.privateKey); 
} 
  
// Generate keys 
generateKeyFiles(); 
  
// Reading private key file 
var PRIVKEY = fs.readFileSync(path.join(__dirname, 
                          'private_key'), 'utf8'); 
  
// Reading public key file 
var PUBKEY = fs.readFileSync(path.join(__dirname, 
                          'public_key'), 'utf8'); 
  
// Defining my msg 
myMSG = "GeeksforGeeks!"; 
console.log("Original msg is:"+myMSG); 
  
// RSA PRIVATE ENCRYPT -> PUBLIC DECRYPT 
function privENC_pubDEC(originMSG){ 
  
  // Encrypting msg with privateEncrypt method 
 encmsg = crypto.privateEncrypt(PRIVKEY, 
               Buffer.from(originMSG, 'utf8') ) 
.toString('base64'); 
  
 // Decrypting msg with publicDecrypt method 
 msg = crypto.publicDecrypt(PUBKEY, 
              Buffer.from(encmsg, 'base64')); 
  
 console.log(); 
  
 // Prints encrypted msg 
 console.log("Encrypted with private key:"
                      + encmsg); 
  
 console.log(); 
  
 // Prints decrypted msg 
 console.log("Decrypted with public key:"
                     + msg.toString()); 
} 
  
// Calling privENC_pubDEC() method 
privENC_pubDEC(myMSG);

输出:

Original msg is:GeeksforGeeks!

Encrypted with private key:
knwqke0ZrpJj1sLtL978OyqBMnJUEAEgTy1qJbyEnJyWbjoQ6hO7f
2FPnVhJnZwpZlxLbFQZCV1GMmr6WWJenFo=

Decrypted with public key:GeeksforGeeks!

范例2:

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

输出:

// Buffer 27 62 a1 2a 53 8d 0d 52 c7 3f e8 cc 89 42 c6
3e 8e 60 cd d3 57 06 d4 c9 1e 31 ba e6 23 8b 2c 10 be 
c1 fc ed 53 a4 9f f9 e0 5b da 74 d7 c2ca d0 98 f4 ... 
// Buffer 72 69 73 68 61 62 68

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




相关用法


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