3

I need to know how to generate 'rsa' key-pair on the client-side using angular2. I need to generate private/public key pair and save the private key into database and want to use public key inside the client side. How can I implement this?

I found this https://www.npmjs.com/package/generate-rsa-keypair for generating key pair. But its for node? Can I implement it into my client side? If yes how? Is any other way to implement this?

muetzerich
  • 5,600
  • 7
  • 37
  • 52
Khushi
  • 1,759
  • 6
  • 26
  • 45
  • What do you mean with _and save the private key into database_ ?. Do you want to store the key into the IndexedDB in browser? – pedrofb Jun 21 '17 at 11:48
  • i need to store it in my Database MongoDb – Khushi Jun 23 '17 at 04:05
  • For security reasons it is not recommended to send a private key along the network. If you need it in the server, generate the keypair in the server and send the public key to browser – pedrofb Jun 23 '17 at 06:03

1 Answers1

7

you must use https://github.com/juliangruber/keypair library

then import it in angular component like

import * as keypair from 'keypair';

and use library method

const pubprivkey = keypair();
console.log(pubprivkey);

it will return object of RSA public and private key

{ public: '-----BEGIN RSA PUBLIC KEY-----\r\nMIGJAoGBAM3CosR73CBNcJsLvAgMBAAE=\r\n-----END RSA PUBLIC KEY-----\n',
  private: '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQDNwqLEe9wgTXNHoyxi7Ia\r\nPQUCQCwWU4U+v4lD7uYBw00Ga/xt+7+UqFPlPVdz1yyr4q24Zxaw0LgmuEvgU5dycq8N7Jxj\r\nTubX0MIRR+G9fmDBBl8=\r\n-----END RSA PRIVATE KEY-----\n' }
sagar patel
  • 591
  • 5
  • 18