How to create a pair private/public keys using Node.js crypto?

后端 未结 9 690
逝去的感伤
逝去的感伤 2020-12-23 01:59

I have to generate two keys (private and public) to encrypt a text with the public and let the user with the private key decrypt the text.

Is it possible with the mo

9条回答
  •  鱼传尺愫
    2020-12-23 02:27

    nodejs v10.12 now supports this natively with crypto.generateKeyPair

    const { generateKeyPair } = require('crypto');
    generateKeyPair('rsa', {
      modulusLength: 4096,
      publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
      },
      privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: 'top secret'
      }
    }, (err, publicKey, privateKey) => {
      // Handle errors and use the generated key pair.
    });
    

提交回复
热议问题