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

后端 未结 9 685
逝去的感伤
逝去的感伤 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:25

    Use the crypto module from npm to generate KeyPair.

    var crypto = require('crypto');
    
    var prime_length = 60;
    var diffHell = crypto.createDiffieHellman(prime_length);
    
    diffHell.generateKeys('base64');
    console.log("Public Key : " ,diffHell.getPublicKey('base64'));
    console.log("Private Key : " ,diffHell.getPrivateKey('base64'));
    
    console.log("Public Key : " ,diffHell.getPublicKey('hex'));
    console.log("Private Key : " ,diffHell.getPrivateKey('hex'));
    

    Above is a example snippet. To know more checkout documentation http://nodejs.org/api/crypto.html

提交回复
热议问题