Clone wiki

js-rsa-pem / Home

Description

This project is based on: http://www9.atwiki.jp/kurushima/pub/jsrsa/ and it's depends on RSA library from: http://www-cs-students.stanford.edu/~tjw/jsbn/

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.

Usage

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();
publicKey.readPublicKeyFromX509PEMString(pemKey);
// reading private and public key from PKCS#1 format (openssl default)
var privateKey = new RSAKey();
privateKey.readPrivateKeyFromPkcs1PemString(pemKey);
// reading private and public key from PKCS#8 format (openssl default)
var privateKey2 = new RSAKey();
privateKey2.readPrivateKeyFromPkcs8PemString(pemKey);

More examples

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

Updated