Is there an opens source library to generate public/private keys in angular/ionic?

346 views Asked by At

I am not sure this is a SO question but I have been searching for a library similar to keypair or libsignal that allows you to generate public/private keypairs in angular and ionic. I am trying to create an end-to-end encrypted application using angular and ionic on the client side. I have tried Keypair but it comes with a polyfill warning and has not been updated for years. I have also looked at libsignal client for typescript but it looks like a node implementation. Maybe there's another solution to what I am trying to do using angular/ionic. I just want to create a public/private keypair at login or on request...send my public key to the server where others can grab it encrypt a small text and ..save the encrypt text on the server where I can grab in and decrypt in on my client? any suggestions would be greatly appreciated.

1

There are 1 answers

1
Talon On

You can use node-forge

rsa_generateKeyPair() {
    let pair = Forge.pki.rsa.generateKeyPair(2048, 0x10001)
    return {
      private: Forge.pki.privateKeyToPem(pair.privateKey),
      public: Forge.pki.publicKeyToPem(pair.publicKey),
    }
}
rsa_decrypt(key:string, data64:string) {
    let privateKey = Forge.pki.privateKeyFromPem(key) ;
    let data = Forge.util.decode64(data64) ;
    return privateKey.decrypt(data) ;
}
rsa_crypt(key:string, data:string) {
    let pubKey = Forge.pki.publicKeyFromPem(key);
    let data64 = Forge.util.encode64(pubKey.encrypt(data)) ;
    return data64 ;
}