1. Installing OPENSS
Download the installation package directly under Window
http://houjixin.blog.163.com/blog/static/3562841020144143494875/
In my blog, the example now shows that everyone is a 64-bit operating system.
Choose the latest version of the Win64 version, the larger 33MB installation program Download the installation
Select Install directory fool install next step to finish
2. Generate RSA key file I installed to C:\OpenSSL-Win64 under the C:\OpenSSL-Win64\bin directory click Openssl.exe
After start input genrsa-out RSA_PRIVATE_KEY.PEM 1024 return car
At this point, we can see a file named Rsa_private_key.pem in the Bin folder, open it in Notepad, you can see-----begin RSA private key-----,-----END RSA Private Key-----The end of the string without a newline, this is the original private key.
3. Convert RSA private key to PKCS8 format
Use the CMD CD to the directory to execute the command, adjust the cmd command box width, why not directly dot Openssl.exe I said
Input command Pkcs8-topk8-inform pem-in rsa_private_key.pem-outform pem–nocrypt, and enter
The result of a successful birth is the private key in the PKCS8 format.
(If you do not execute openssl.exe in cmd above, you will find that you cannot copy the full converted file)
Right-click the top edge of the OpenSSL window, select edit → tag, select the text you want to copy (for example),
Now continue right click on the top edge of the OpenSSL window, select edit → copy,
The copied content clay into a new Notepad, renamed as Rsa_private.key file, as long as you know this is the PKCS8 format of the private key can be
4. Generating the public key
Enter command rsa-in rsa_private_key.pem-pubout-out Rsa_public_key.pem, and enter,
Get the results of a successful build, such as:
5.node.js instances, third-party libraries are used Node-rsa
var Nodersa = require (' Node-rsa '); var fs= require (' FS '); var onerror = (E) =>{console.log ("Decryption Failed");} var onfinally = () =>{console.log ("What to do eventually");} Fs.readfile (' Rsa_private.key ', ' utf-8 ', function (err,data) {if (err) {console.log (data); }else{console.log (data); privatekey= new Nodersa (data); var text = ' Hello rsa! '; var encrypted = Privatekey.encrypt (text, ' base64 '); Console.log (' Encryption encrypted: ', encrypted); var decrypted = Privatekey.decrypt (encrypted, ' UTF8 '); Console.log (' Decryption decrypted: ', decrypted); var encrypted1 = ' mijocrff+mws3j6+ fkpsm9zamszhxxhc+/w0uut+t6zmlnoamq6wkwobpq+o+mazk61mfdyimwvw+51dylc7ke4eey4y+qb23/ahc9mi/ Bjqivwguhelpjflmqxcnvrfsbob33+7n/fswawljmhufhanwp1dqplh9ny501qomvy= '; var decrypted = Privatekey.decrypt ( encrypted1, ' UTF8 '); Console.log (' Decrypt decrypted with the correct encryption string: ', decrypted); Try{var encrypted2 = ' c7ke4eey4y+qb23/ahc9mi/ Bjqivwguhelpjflmqxcnvrfsbob33+7n/fswawljmhufhanwp1dqplh9ny501qomvy= '; var decrypted = Privatekey.decrypt ( Encrypted2, ' UTF8 ');} catch (e) {OnError (e);} Finally{onfinally ();} } })
6. How to place the private key file in the global variables, closures written, can be improved can be raised, do not spit groove, can also be placed in Redis or memcache
Rsakey.js
var fs= require (' FS '); var path = require (' path '); module.exports = (function () { var private = ');//Declare a variable within the function, scope is inside the function return function () {//Returns a function that accesses the _value, so _value is not a global-level variable, but can be accessed through this interface to the if (private== ') { var paths = Path.resolve (__dirname, ' ... /rsa_private.key '); Fs.readfile (Paths, ' Utf-8 ', function (err, data) { private = data; if (private.length!=0) { Console.log ("Success read RSA file"); } return private; }); else{ return private;}} ) ();//An anonymous function that executes immediately
When initializing, introduce
var Rsakey = require ('./util/rsakey ');
Called when introduced
Using RSA encryption cases under node. JS (Windows)