通用RSA加密 - PHP+Java+Javascript加密解密

php端生成 公钥私钥

1、openssl genrsa -out rsa_private_key.pem 1024    私钥

2、openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem  公钥

3、openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt     将RSA私钥转换成PKCS8格式(java)

 

PHP端加密解密

class Index
{

    public function index(){
        // 私钥解密
        $private_key = "-----BEGIN RSA PRIVATE KEY-----
ERCCWwIBAAKBgQC4zGR6jo8bcR19eKxJ2IpOxTjwMVya5Egh2ZTf/lXJmVsoOBwO
ul/g0+fPI2aQ58mICjnxc0vpYz1M5INmcVIgJdOksN13CpTGpTLunD2xcHrE25Cx
5elNbQCPFICRQcnE1zqn8GPySm4EQO2UiHjoTalwfmhfYlS3cNaAcXrZfwIDAQAB
AoGACcmRfMg2Hh70j+OnRFX6pUdex6HlpLJ4e4p5jw96Xa4InO2z1rfaQh8XBOp8
ESqlitX30ajomVjPM1Za+z7/OWuMYV/KehnX9SCKlC9uU9a2zu/oMN+hf2LiqlOy
KWmJRzn1UduDKq6pejac8mltZhFduTFnyxOMtkW8/Qf1TvkCQQDqolU686aC5pQU
cKsOi23rI+5K4s+T4SAum1vKFvsT9ebLiqru+EauZhHDOUH8plQIuV4tiEFEhzjq
XVnwcpolAkEAyaBQ2Wsc/T1uDFGDpRy+nPdtHjQh6NTXvBF5VI3weeQJeCOl6hzQ
IV3OWEyfXsXbYq0PzHz077jbM2g0OeiJ0wJAPDYgTRqNlnX5di1bmEXrlTYJRqph
vn89NPilW3z+PacS7RTOvLh12vdSg5jD+EPuuLZpFqIqrZ1N28bM98lRCQJAKgs2
EMBXQS9oHUV6MBn4XX7y0BOK5qRLgDoclNcsSB6XIug8Fr9/XxXSi3Me4IJdpwvz
eW6tHLftbaDTEyg8ZQJAPy9IuyA8WsHwBtybIxhCppJPGlMojoPyRZ5JWqZskzUB
NQuu5zy2yio29hIILEJ6h46qnBdmEjd/C/fZ9oYjJw==
-----END RSA PRIVATE KEY-----";

        // 公钥加密
        $public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4zGR6jo8bcR19eKxJ2IpOxTjw
MVya5Egh2ZTf/lXJmVsoOBwOul/g0+fPI2aQ58mICjnxc0vpYz1M5INmcVIgJdOk
sN13CpTGpTLunD2xcHrE25Cx5elNbQCPFICRQcnE1zqn8GPySm4EQO2UiHjoTalw
fmhfYlS3cNaAcXrZfwIDAQAB
-----END PUBLIC KEY-----";

//        ====Java 私钥===
//        -----BEGIN PRIVATE KEY-----
//MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALjMZHqOjxtxHX14
//rEnYik7FOPAxXJrkSCHZlN/+VcmZWyg4HA66X+DT588jZpDnyYgKOfFzS+ljPUzk
//g2ZxUiAl06Sw3XcKlMalMu6cPbFwesTbkLHl6U1tAI8UgJFBycTXOqfwY/JKbgRA
//7ZSIeOhNqXB+aF9iVLdw1oBxetl/AgMBAAECgYAJyZF8yDYeHvSP46dEVfqlR17H
//oeWksnh7inmPD3pdrgic7bPWt9pCHxcE6nwRKqWK1ffRqOiZWM8zVlr7Pv85a4xh
//X8p6Gdf1IIqUL25T1rbO7+gw36F/YuKqU7IpaYlHOfVR24Mqrql6NpzyaW1mEV25
//MWfLE4y2Rbz9B/VO+QJBAOqiVTrzpoLmlBRwqw6Lbesj7kriz5PhIC6bW8oW+xP1
//5suKqu74Rq5mEcM5QfymVAi5Xi2IQUSHOOpdWfBymiUCQQDJoFDZaxz9PW4MUYOl
//HL6c920eNCHo1Ne8EXlUjfB55Al4I6XqHNAhXc5YTJ9exdtirQ/MfPTvuNszaDQ5
//6InTAkA8NiBNGo2Wdfl2LVuYReuVNglGqmG+fz00+KVbfP49pxLtFM68uHXa91KD
//mMP4Q+64tmkWoiqtnU3bxsz3yVEJAkAqCzYQwFdBL2gdRXowGfhdfvLQE4rmpEuA
//OhyU1yxIHpci6DwWv39fFdKLcx7ggl2nC/N5bq0ct+1toNMTKDxlAkA/L0i7IDxa
//wfAG3JsjGEKmkk8aUyiOg/JFnklapmyTNQE1C67nPLbKKjb2EggsQnqHjqqcF2YS
//N38L99n2hiMn
//-----END PRIVATE KEY-----

        //========加密=======
//        $pi_key =  openssl_pkey_get_private($private_key);// 可用返回资源id
//        $pu_key = openssl_pkey_get_public($public_key);
//        $data = '{"ip":"192.168.0.1","domain":"www.baidu.com"}';
//        openssl_public_encrypt($data, $encrypted, $pu_key); // 公钥加密
//        // 加密后的字符
//        var_dump($this->urlsafe_b64encode($encrypted));
//        die;

        //========解密========
        // php 加密后的值
//$str = "PXrR2AoSFPdMLis4HWKFbHsIsuAjHZVI8TXfsNvzrEYbafWLvpLm3wa_dqqRJwX2GCmOb2GKo087WCIAUPpZxpJHJ44uQKFEoagUQCyRi3LgCqL7CDPHJHmfEfFgp4hbk04hJ3LVPh4Pz4dOGc4To3OOgQSCIkvytY3fz7Fwv0U";      // js 使用php公钥加密后的值
     // q5BaGcWoKFSasuAEXeTTy8PfcaHR4xdlurviK4fAoNT3jMiuN/vAJQVxklcFjqql2bn0i6IxG8DiWMAXm8nVFfTOINp811BLYjsHBymFStrTiEiPekL8txK4z5bl6OFi43FLQT96h8XY8zcJL/UFq/NJShVlg/kWHvVixfZF4NI= // java 使用PHP生成公钥加密后的值 //O91XUcQV8pu_FqnuXO6qkfB-6tEVJVNdlHOnArEcj71JRq_rIHYkpYrOnEiMfKaXrNbyWISiPX14OkZ5mbK92ux58w8Vv8JLkizZ0QUESaJd974l1YcxiddBpKRcgxVB3Vh7RudchzFta64iiOru9fvGU5Uza4KiOLU9PqD8Z-w
$str = 'O91XUcQV8pu/FqnuXO6qkfB+6tEVJVNdlHOnArEcj71JRq/rIHYkpYrOnEiMfKaXrNbyWISiPX14OkZ5mbK92ux58w8Vv8JLkizZ0QUESaJd974l1YcxiddBpKRcgxVB3Vh7RudchzFta64iiOru9fvGU5Uza4KiOLU9PqD8Z+w='; $d = $this->urlsafe_b64decode($str); $pi_key = openssl_pkey_get_private($private_key); // 可用返回资源id @openssl_private_decrypt($d, $decrypted, $pi_key); // 私钥解密 echo $decrypted; } //url base64解码 protected function urlsafe_b64decode($string) { $data = str_replace(array('-','_'),array('+','/'),$string); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } return base64_decode($data); } //url base64编码 protected function urlsafe_b64encode($string) { $data = base64_encode($string); $data = str_replace(array('+','/','='),array('-','_',''),$data); return $data; } }

 

详情可以了解:

https://my.oschina.net/gKWW0kOYB/blog/1836342

 

附js文件下载:点击

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值