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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。