Linux用户(以Ubuntu为例)
$ openssl 进入OpenSSL程序
OpenSSL> genrsa -out rsa_private_key.pem 1024
生成私钥
OpenSSL> pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt Java开发者需要将私钥转换成PKCS8格式
OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 生成公钥
OpenSSL> exit ## 退出OpenSSL程序
Windows用户在cmd窗口中进行以下操作: C:UsersHammer>cd C:OpenSSL-Win32in
进入OpenSSL安装目录 C:OpenSSL-Win32in>openssl.exe
进入OpenSSL程序 OpenSSL> genrsa -out rsa_private_key.pem 1024
生成私钥 OpenSSL> pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
Java开发者需要将私钥转换成PKCS8格式
OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 生成公钥
OpenSSL> exit ## 退出OpenSSL程序
https://github.com/reflux/refluxjs/issues/300
var crypto = require('crypto') ,fs = require('fs'); const padding = crypto.constants.RSA_PKCS1_PADDING; module.exports = { Decrypt: function(data){ var publicPem = fs.readFileSync('./pem/rsa_public_key.pem'); var pubkey = publicPem.toString(); var buf = Buffer.from(data, 'hex'); var plaintext = crypto.publicDecrypt({ key: pubkey, padding: crypto.constants.RSA_PKCS1_PADDING }, buf); return plaintext.toString(); }, Encrypt: function(data){ var privatePem = fs.readFileSync('./pem/rsa_private_key.pem'); var key = privatePem.toString(); var result = crypto.privateEncrypt({ key: key, padding: padding }, new Buffer(data,'utf8')); return result.toString('hex'); } };