encrypt
私钥、公钥生成
linux环境
[vagrant@localhost vagrant_data]$ openssl genrsa -out rsa_1024_priv.pem 1024
Generating RSA private key, 1024 bit long modulus
....++++++
.++++++
e is 65537 (0x10001)
[vagrant@localhost vagrant_data]$ cat rsa_1024_priv.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC+Ja5cIL4FEz5RtmNs1vdOQTllpjimuF98zR6d2gZ1tZaqO2cV
0M8UbMwqXnikavARhz9dCMC5odzj1F1nWhc08GRrSEDQQc1SAXsMb0j2vsVDvnsW
bFcJPii/hDFkOKYc/Wbx/uFOJVawNmnUqa4doBfeOtLZt///53mwwPodhQIDAQAB
AoGBALE97I5h0t5mNqPyUDFnG7UtJTR0QcBegluR0+UuEvFr59ocbxWtwyl11MX1
s50HNWoRWOX+v68Jix4vmTU6i/+nWan97O6rmJgnb4kMXvsSi/rAq/qqkUBKMefN
Cm+1FgJQBTdEmQkPftrcIe8mSb6mm+xn1/v4u1uyCI22vyXhAkEA7X5ty3TUuHJX
tEygae0dGr1XvKHDrqGYdSkY/6ZDnxhocFSQpgeDLFMk4miXPecZAHhHlQ52eWU/
W7WmDEbKSQJBAMz2xaqHevu3R/YxqusLvIjmlqvRHurvIy+RLo/U+9ERVKDAu8Ov
UGl4Ga3OZOQy+x4kz98J3VFwnSEmyUF1mV0CQFzrDzgAAM4dZnUTWdu3uqBgnjge
Ljpf3Q9Rwgcb3O8dFJeGboWXANpRuWeftjSw+xY0+QS3gODkHRLbkhjRhYkCQQCJ
pHLr6R2a6uNlnsckH3aWDRxD+CSmKr/bUskNCD+vj87gQW4WpKYAngnIoaE5ak8f
WCNMqgg5h8HNoIlo7zt1AkBBctg2+VzHmeqwGX9mvulR29PBGJq/mChrF5uQaUUP
ZAZf+8/iQzirFlwxMdFDXSbRiMC4DwxVTOSYyPGGLxsY
-----END RSA PRIVATE KEY-----
[vagrant@localhost vagrant_data]$ openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
writing RSA key
[vagrant@localhost vagrant_data]$ cat rsa_1024_pub.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+Ja5cIL4FEz5RtmNs1vdOQTll
pjimuF98zR6d2gZ1tZaqO2cV0M8UbMwqXnikavARhz9dCMC5odzj1F1nWhc08GRr
SEDQQc1SAXsMb0j2vsVDvnsWbFcJPii/hDFkOKYc/Wbx/uFOJVawNmnUqa4doBfe
OtLZt///53mwwPodhQIDAQAB
-----END PUBLIC KEY-----
windows环境的话 可以使用支付宝的secret_key_tools_RSA_win.zip
JSEncrypt下载
使用的话 就比较简单了
js里加载上bin目录下jsencrypt.min.js
// Encrypt with the public key...
var encrypt = new JSEncrypt();
encrypt.setPublicKey(pubkey); // pubkey是公钥内容
var encrypted = encrypt.encrypt($data);
PHP端
// 解密
function decrypt($data){
if (!$data) {
return false;
}
if(!openssl_private_decrypt(base64_decode($data), $decrypt_data, RSA_PRIVATE_KEY)) {
return false;
}
return $decrypt_data;
}