fc2aecfe77c5b59d4100cb57d44591b0fc1a2eabe857da801e333ea0bebf03b913704aa3dfec29f79967a3d1954035c05660f96d5311b554e4d182b06a487f 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Website
  2. ======================
  3. http://travistidwell.com/jsencrypt
  4. Introduction
  5. ======================
  6. When browsing the internet looking for a good solution to RSA Javascript
  7. encryption, there is a whole slew of libraries that basically take the fantastic
  8. work done by Tom Wu @ http://www-cs-students.stanford.edu/~tjw/jsbn/ and then
  9. modify that code to do what they want.
  10. What I couldn't find, however, was a simple wrapper around this library that
  11. basically uses the library <a href="https://github.com/travist/jsencrypt/pull/6">practically</a> untouched, but adds a wrapper to provide parsing of
  12. actual Private and Public key-pairs generated with OpenSSL.
  13. This library is the result of these efforts.
  14. How to use this library.
  15. =======================
  16. This library should work hand-in-hand with openssl. With that said, here is how to use this library.
  17. - Within your terminal (Unix based OS) type the following.
  18. ```
  19. openssl genrsa -out rsa_1024_priv.pem 1024
  20. ```
  21. - This generates a private key, which you can see by doing the following...
  22. ```
  23. cat rsa_1024_priv.pem
  24. ```
  25. - You can then copy and paste this in the Private Key section of within index.html.
  26. - Next, you can then get the public key by executing the following command.
  27. ```
  28. openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
  29. ```
  30. - You can see the public key by typing...
  31. ```
  32. cat rsa_1024_pub.pem
  33. ```
  34. - Now copy and paste this in the Public key within the index.html.
  35. - Now you can then convert to and from encrypted text by doing the following in code.
  36. ```html
  37. <!doctype html>
  38. <html>
  39. <head>
  40. <title>JavaScript RSA Encryption</title>
  41. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  42. <script src="bin/jsencrypt.min.js"></script>
  43. <script type="text/javascript">
  44. // Call this code when the page is done loading.
  45. $(function() {
  46. // Run a quick encryption/decryption when they click.
  47. $('#testme').click(function() {
  48. // Encrypt with the public key...
  49. var encrypt = new JSEncrypt();
  50. encrypt.setPublicKey($('#pubkey').val());
  51. var encrypted = encrypt.encrypt($('#input').val());
  52. // Decrypt with the private key...
  53. var decrypt = new JSEncrypt();
  54. decrypt.setPrivateKey($('#privkey').val());
  55. var uncrypted = decrypt.decrypt(encrypted);
  56. // Now a simple check to see if the round-trip worked.
  57. if (uncrypted == $('#input').val()) {
  58. alert('It works!!!');
  59. }
  60. else {
  61. alert('Something went wrong....');
  62. }
  63. });
  64. });
  65. </script>
  66. </head>
  67. <body>
  68. <label for="privkey">Private Key</label><br/>
  69. <textarea id="privkey" rows="15" cols="65">-----BEGIN RSA PRIVATE KEY-----
  70. MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ
  71. WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR
  72. aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB
  73. AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv
  74. xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH
  75. m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd
  76. 8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF
  77. z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5
  78. rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM
  79. V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe
  80. aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil
  81. psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz
  82. uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876
  83. -----END RSA PRIVATE KEY-----</textarea><br/>
  84. <label for="pubkey">Public Key</label><br/>
  85. <textarea id="pubkey" rows="15" cols="65">-----BEGIN PUBLIC KEY-----
  86. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
  87. FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76
  88. xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
  89. gwQco1KRMDSmXSMkDwIDAQAB
  90. -----END PUBLIC KEY-----</textarea><br/>
  91. <label for="input">Text to encrypt:</label><br/>
  92. <textarea id="input" name="input" type="text" rows=4 cols=70>This is a test!</textarea><br/>
  93. <input id="testme" type="button" value="Test Me!!!" /><br/>
  94. </body>
  95. </html>
  96. ```
  97. - Look at how http://www.travistidwell.com/jsencrypt/example.html works to get a better idea.
  98. Other Information
  99. ========================
  100. This library heavily utilizes the wonderful work of Tom Wu found at http://www-cs-students.stanford.edu/~tjw/jsbn/.
  101. 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.
  102. 1024 bit RSA Private Key in Base64 Format
  103. -----------------------------------------
  104. ```
  105. -----BEGIN RSA PRIVATE KEY-----
  106. MIICXgIBAAKBgQDHikastc8+I81zCg/qWW8dMr8mqvXQ3qbPAmu0RjxoZVI47tvs
  107. kYlFAXOf0sPrhO2nUuooJngnHV0639iTTEYG1vckNaW2R6U5QTdQ5Rq5u+uV3pMk
  108. 7w7Vs4n3urQ6jnqt2rTXbC1DNa/PFeAZatbf7ffBBy0IGO0zc128IshYcwIDAQAB
  109. AoGBALTNl2JxTvq4SDW/3VH0fZkQXWH1MM10oeMbB2qO5beWb11FGaOO77nGKfWc
  110. bYgfp5Ogrql4yhBvLAXnxH8bcqqwORtFhlyV68U1y4R+8WxDNh0aevxH8hRS/1X5
  111. 031DJm1JlU0E+vStiktN0tC3ebH5hE+1OxbIHSZ+WOWLYX7JAkEA5uigRgKp8ScG
  112. auUijvdOLZIhHWq7y5Wz+nOHUuDw8P7wOTKU34QJAoWEe771p9Pf/GTA/kr0BQnP
  113. QvWUDxGzJwJBAN05C6krwPeryFKrKtjOGJIniIoY72wRnoNcdEEs3HDRhf48YWFo
  114. riRbZylzzzNFy/gmzT6XJQTfktGqq+FZD9UCQGIJaGrxHJgfmpDuAhMzGsUsYtTr
  115. iRox0D1Iqa7dhE693t5aBG010OF6MLqdZA1CXrn5SRtuVVaCSLZEL/2J5UcCQQDA
  116. d3MXucNnN4NPuS/L9HMYJWD7lPoosaORcgyK77bSSNgk+u9WSjbH1uYIAIPSffUZ
  117. bti+jc1dUg5wb+aeZlgJAkEAurrpmpqj5vg087ZngKfFGR5rozDiTsK5DceTV97K
  118. a3Y+Nzl+XWTxDBWk4YPh2ZlKv402hZEfWBYxUDn5ZkH/bw==
  119. -----END RSA PRIVATE KEY-----
  120. ```
  121. 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.
  122. Here are some good resources to investigate further.
  123. - http://etherhack.co.uk/asymmetric/docs/rsa_key_breakdown.html
  124. - http://www.di-mgt.com.au/rsa_alg.html
  125. - https://polarssl.org/kb/cryptography/asn1-key-structures-in-der-and-pem
  126. With this information, we can translate a private key format to the variables
  127. required with the jsbn library from Tom Wu by using the following mappings.
  128. ```
  129. modulus => n
  130. public exponent => e
  131. private exponent => d
  132. prime1 => p
  133. prime2 => q
  134. exponent1 => dmp1
  135. exponent2 => dmq1
  136. coefficient => coeff
  137. ```