I am a firm believer that JavaScript will eventually be the ubiquitous coding language of the future. Although there are many objections to this statement, I don’t think anyone can argue that it is the best poised for ubiquity considering it is now used in both server side (node.js) and obviously client side open web standards. And considering that this language did start out as a ‘toy’ language, I am often amazing at how far it has come in maturity in recent years. There is an area where I am most amazed at its progression, and that is with Encryption. For years, this area was pretty much dominated by the C and C++ languages (to which I am also a seasoned developer in), but now that JavaScript has made its way to the server, it is becoming recognized as a potential contender to this domination.
Much of this recognition should be directed toward the fantastic work of Tom Wu of Standford. It was his work with the JSBN library that really opened peoples eyes to the effectiveness of JavaScript in the field of encryption. However, there was just minor drawback to his work… The only real way to utilize his library is to have a full understanding behind the equations of encryption, which comes at as a huge hurtle considering the industry has been spoiled with the ease of use of the OpenSSL library generation of standardized keys used in the encryption. In this system, there is really only two things to worry about… The Public Key and the Private Key, used for encryption and decryption respectively. But in order to use Tom Wu’s library, you must learn how to utilize eight different variables to the encryption equations! I recognized this as a perfect opportunity to create a simple wrapper library around his library that simply speaks in terms of the Public and Private keys everyone is familiar with. Of course I found other libraries out there that claim to do the same, but what I couldn’t find in these libraries was a simple wrapper around this library that basically uses Tom’s library practically untouched, but simply adds a wrapper to provide parsing of actual Private and Public key-pairs generated with OpenSSL. Enter JSEncrypt.
Download
Go to https://github.com/travist/jsencrypt to download this library.
Website & Demo
http://travistidwell.com/jsencrypt
How to use this library.
This library should work hand-in-hand with openssl. With that said, here is how to use this library.
- Within your terminal (Unix based OS) type the following.
openssl genrsa -out rsa_1024_priv.pem 1024
- This generates a private key, which you can see by doing the following…
cat rsa_1024_priv.pem
- You can then copy and paste this in the Private Key section of within index.html.
- Next, you can then get the public key by executing the following command.
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
- You can see the public key by typing…
cat rsa_1024_pub.pem
- Now copy and paste this in the Public key within the index.html.
- Now you can then convert to and from encrypted text by doing the following in code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Here is a demo… http://travistidwell.com/jsencrypt/demo/index.html
Other Information
This library heavily utilizes the wonderful work of Tom Wu found at http://www-cs-students.stanford.edu/~tjw/jsbn.
This jsbn library was written using the raw variables to perform encryption. This is great for encryption, but most private keys use a Private Key in the PEM format seen below.
1024 bit RSA Private Key in Base64 Format
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDHikastc8+I81zCg/qWW8dMr8mqvXQ3qbPAmu0RjxoZVI47tvs
kYlFAXOf0sPrhO2nUuooJngnHV0639iTTEYG1vckNaW2R6U5QTdQ5Rq5u+uV3pMk
7w7Vs4n3urQ6jnqt2rTXbC1DNa/PFeAZatbf7ffBBy0IGO0zc128IshYcwIDAQAB
AoGBALTNl2JxTvq4SDW/3VH0fZkQXWH1MM10oeMbB2qO5beWb11FGaOO77nGKfWc
bYgfp5Ogrql4yhBvLAXnxH8bcqqwORtFhlyV68U1y4R+8WxDNh0aevxH8hRS/1X5
031DJm1JlU0E+vStiktN0tC3ebH5hE+1OxbIHSZ+WOWLYX7JAkEA5uigRgKp8ScG
auUijvdOLZIhHWq7y5Wz+nOHUuDw8P7wOTKU34QJAoWEe771p9Pf/GTA/kr0BQnP
QvWUDxGzJwJBAN05C6krwPeryFKrKtjOGJIniIoY72wRnoNcdEEs3HDRhf48YWFo
riRbZylzzzNFy/gmzT6XJQTfktGqq+FZD9UCQGIJaGrxHJgfmpDuAhMzGsUsYtTr
iRox0D1Iqa7dhE693t5aBG010OF6MLqdZA1CXrn5SRtuVVaCSLZEL/2J5UcCQQDA
d3MXucNnN4NPuS/L9HMYJWD7lPoosaORcgyK77bSSNgk+u9WSjbH1uYIAIPSffUZ
bti+jc1dUg5wb+aeZlgJAkEAurrpmpqj5vg087ZngKfFGR5rozDiTsK5DceTV97K
a3Y+Nzl+XWTxDBWk4YPh2ZlKv402hZEfWBYxUDn5ZkH/bw==
-----END RSA PRIVATE KEY-----
This library simply takes keys in the following format, and translates it to those variables needed to perform the encryptions used in Tom Wu’s library.
Here are some good resources to investigate further.
- http://etherhack.co.uk/asymmetric/docs/rsa_key_breakdown.html
- http://www.di-mgt.com.au/rsa_alg.html
- https://polarssl.org/kb/cryptography/asn1-key-structures-in-der-and-pem
With this information, we can translate a private key format to the variables required with the jsbn library from Tom Wu by using the following mappings.
modulus => n
public exponent => e
private exponent => d
prime1 => p
prime2 => q
exponent1 => dmp1
exponent2 => dmq1
coefficient => coeff