Clone wiki

js-rsa-pem / Home


This project is based on: and it's depends on RSA library from:

It is only an extension of rsa-pem.js library. I've added methods for decoding a public key from .pem files, and for encoding a key to public/private key .pem file.


Writing RSA keys to .pem file

var rsaKey = new RSAKey();
rsaKey.generate(512, "10001");
// writing public key only in .x509 format
var pubPemKey = rsaKey.publicKeyToX509PemString();
// writing private key in PKCS#1 format (openssl default)
var privPemKey = rsaKey.privateKeyToPkcs1PemString();
// writing private key in PKCS#8 format
var privPemKey = rsaKey.privateKeyToPkcs8PemString();

Reading RSA keys from .pem file

// reading public key from .x509 format
var publicKey = new RSAKey();
// reading private and public key from PKCS#1 format (openssl default)
var privateKey = new RSAKey();
// reading private and public key from PKCS#8 format (openssl default)
var privateKey2 = new RSAKey();

More examples

You can find more examples of usage in QUnit tests here.