ASCWH

PHP、Java、Javascript通用RSA加密

HUA
04-20
/
0 评论
/
1,275 阅读
/
正在检测是否收录...
04/20

在系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理;然而由于系统与系统之间的开发语言不同。本次需求是生成二维码是通过java生成,由php来解密。基于这类需求所以选择了RSA进行加解密。

一、生成RSA公私钥

分成三步生成RSA公私钥,第1、2步可以满足php的使用,由于java的私钥要转化为PKCS8格式才能使用,所以执行第三步来实现

1、生成私钥

openssl genrsa -out rsa_private_key.pem 1024
如下:

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDOU3dwYw5oh4qzNv7YcgD0gxrqvGv3nFMbBFvXPDVuy/2AZsJC
OurkwjveVcq2AdtAc+RD409uwNgrf0f6pxl60cNF81qHujCqmrIjPyyokpXwCjBj
L9NlG3RmZ3btN87LDeKvIQt6n85pULOUVyOI7qlCvN8y3zlgsEM41Xqx1wIDAQAB
AoGAK0gvIhBlkV/9pKLAyz7/HEv0dZGZRJQyitRbP8eNnfSE9U6ZKbOaWuq0DGwK
3it9hwPIusUQgLdLobRNsTMkkNJRfPAnuUelZVwAkkNPwN4i4uZAqvifhkoFM7Dt
3pUlNMyxBRVwg6zdjYr26x0AZy+FdGU8QE/cofPT6j6dOIECQQD91itWOzufUKxj
/os89HCWtOs74VxsDiLW/GSQJFp9/+UHSSrbcPuS6t4d0dE6oi9byjUPC10O1Jmc
AJa60CpxAkEA0BWjCXvHdiRZp0jkIg+fLtSPjCzj4ePRP0OwF8vXawIudoO8A6fh
8Fa5l6ZJG9gLl0EAOM1AeO6v+wzRR5b0xwJAJs8r6Q89WmpXMSKl3zfwTtpybMFU
BCFVKlGEfpVQUJtjRO3m444uqJPISf7eYnyRiRMtcZNnMvFGWxer8/wHwQJBAIal
+aIhXqisTPday4Te5vq+wUTLp4p8iOKPQlujo/myp645AGi9Mg77j4z6JUTHQHjX
rvUSnC2lBmtFmAbjP/ECQQCL1a2QUUVS8MynYvUinnkwPedqqC/mWBgSgO0ytatA
T+Stue6pYPvdpqK5iLncrgTPZxUTYA8ulvCM6oewXfl/
-----END RSA PRIVATE KEY-----

2、生成公钥

openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOU3dwYw5oh4qzNv7YcgD0gxrq
vGv3nFMbBFvXPDVuy/2AZsJCOurkwjveVcq2AdtAc+RD409uwNgrf0f6pxl60cNF
81qHujCqmrIjPyyokpXwCjBjL9NlG3RmZ3btN87LDeKvIQt6n85pULOUVyOI7qlC
vN8y3zlgsEM41Xqx1wIDAQAB
-----END PUBLIC KEY-----

3、将RSA私钥转换成PKCS8格式

-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAM5Td3BjDmiHirM2
/thyAPSDGuq8a/ecUxsEW9c8NW7L/YBmwkI66uTCO95VyrYB20Bz5EPjT27A2Ct/
R/qnGXrRw0XzWoe6MKqasiM/LKiSlfAKMGMv02UbdGZndu03zssN4q8hC3qfzmlQ
s5RXI4juqUK83zLfOWCwQzjVerHXAgMBAAECgYArSC8iEGWRX/2kosDLPv8cS/R1
kZlElDKK1Fs/x42d9IT1Tpkps5pa6rQMbAreK32HA8i6xRCAt0uhtE2xMySQ0lF8
8Ce5R6VlXACSQ0/A3iLi5kCq+J+GSgUzsO3elSU0zLEFFXCDrN2NivbrHQBnL4V0
ZTxAT9yh89PqPp04gQJBAP3WK1Y7O59QrGP+izz0cJa06zvhXGwOItb8ZJAkWn3/
5QdJKttw+5Lq3h3R0TqiL1vKNQ8LXQ7UmZwAlrrQKnECQQDQFaMJe8d2JFmnSOQi
D58u1I+MLOPh49E/Q7AXy9drAi52g7wDp+HwVrmXpkkb2AuXQQA4zUB47q/7DNFH
lvTHAkAmzyvpDz1aalcxIqXfN/BO2nJswVQEIVUqUYR+lVBQm2NE7ebjji6ok8hJ
/t5ifJGJEy1xk2cy8UZbF6vz/AfBAkEAhqX5oiFeqKxM91rLhN7m+r7BRMuninyI
4o9CW6Oj+bKnrjkAaL0yDvuPjPolRMdAeNeu9RKcLaUGa0WYBuM/8QJBAIvVrZBR
RVLwzKdi9SKeeTA952qoL+ZYGBKA7TK1q0BP5K257qlg+92mormIudyuBM9nFRNg
Dy6W8Izqh7Bd+X8=
-----END PRIVATE KEY-----

二、PHP实现RSA的加密解密。


<?php

namespace Home\Controller;


use Think\Controller;

class TestController extends Controller {
    public function index(){

        //2018000000&sh001&u001&223.2&2
        //订单号&商户标识&用户标识&实际支付金额&支付方式



        $private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDOU3dwYw5oh4qzNv7YcgD0gxrqvGv3nFMbBFvXPDVuy/2AZsJC
OurkwjveVcq2AdtAc+RD409uwNgrf0f6pxl60cNF81qHujCqmrIjPyyokpXwCjBj
L9NlG3RmZ3btN87LDeKvIQt6n85pULOUVyOI7qlCvN8y3zlgsEM41Xqx1wIDAQAB
AoGAK0gvIhBlkV/9pKLAyz7/HEv0dZGZRJQyitRbP8eNnfSE9U6ZKbOaWuq0DGwK
3it9hwPIusUQgLdLobRNsTMkkNJRfPAnuUelZVwAkkNPwN4i4uZAqvifhkoFM7Dt
3pUlNMyxBRVwg6zdjYr26x0AZy+FdGU8QE/cofPT6j6dOIECQQD91itWOzufUKxj
/os89HCWtOs74VxsDiLW/GSQJFp9/+UHSSrbcPuS6t4d0dE6oi9byjUPC10O1Jmc
AJa60CpxAkEA0BWjCXvHdiRZp0jkIg+fLtSPjCzj4ePRP0OwF8vXawIudoO8A6fh
8Fa5l6ZJG9gLl0EAOM1AeO6v+wzRR5b0xwJAJs8r6Q89WmpXMSKl3zfwTtpybMFU
BCFVKlGEfpVQUJtjRO3m444uqJPISf7eYnyRiRMtcZNnMvFGWxer8/wHwQJBAIal
+aIhXqisTPday4Te5vq+wUTLp4p8iOKPQlujo/myp645AGi9Mg77j4z6JUTHQHjX
rvUSnC2lBmtFmAbjP/ECQQCL1a2QUUVS8MynYvUinnkwPedqqC/mWBgSgO0ytatA
T+Stue6pYPvdpqK5iLncrgTPZxUTYA8ulvCM6oewXfl/
-----END RSA PRIVATE KEY-----";

        $public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOU3dwYw5oh4qzNv7YcgD0gxrq
vGv3nFMbBFvXPDVuy/2AZsJCOurkwjveVcq2AdtAc+RD409uwNgrf0f6pxl60cNF
81qHujCqmrIjPyyokpXwCjBjL9NlG3RmZ3btN87LDeKvIQt6n85pULOUVyOI7qlC
vN8y3zlgsEM41Xqx1wIDAQAB
-----END PUBLIC KEY-----";
        
          //加密
        $pi_key =  openssl_pkey_get_private($private_key);// 可用返回资源id
        $pu_key = openssl_pkey_get_public($public_key);
        $data = '{"e":"sign_check_fail","c":2}';
        openssl_public_encrypt($data, $encrypted, $pu_key);//公钥加密
        
        //dump ($this->urlsafe_b64encode($encrypted));
        //解密
        $d =  $this->urlsafe_b64decode(I("data"));
        $pi_key =  openssl_pkey_get_private($private_key);// 可用返回资源id
        openssl_private_decrypt($d, $decrypted, $pi_key);//私钥解密
            
        echo $decrypted;
        
    }
    //url base64解码
    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编码
    function urlsafe_b64encode($string) {
        
        $data = base64_encode($string);
        $data = str_replace(array('+','/','='),array('-','_',''),$data);
        return $data;
    }

}
?>

三、javascript rsa加解密

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jsencrypt.js"></script>
</head>
<body>

<script>
    String.prototype.replaceAllStr=function(f,e){
        var reg=new RegExp(f,"g");
        return this.replace(reg,e);
    }


    function urlsafeEncode(e) {
        return e.replaceAllStr("\\+","-").replaceAllStr("/","_").replaceAllStr("=","");
    }

    function urlsafeDecode(e) {
        e =  e.replaceAllStr("-","+").replaceAllStr("_","/");
        var mob = e.length%4;
        if(mob>0){
            e += "====".substr(mob);
        }
        return e;
    }


    // 后台生成的
    var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOU3dwYw5oh4qzNv7YcgD0gxrqvGv3nFMbBFvXPDVuy/2AZsJCOurkwjveVcq2AdtAc+RD409uwNgrf0f6pxl60cNF81qHujCqmrIjPyyokpXwCjBjL9NlG3RmZ3btN87LDeKvIQt6n85pULOUVyOI7qlCvN8y3zlgsEM41Xqx1wIDAQAB";;
    var privateKeyStr = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAM5Td3BjDmiHirM2/thyAPSDGuq8a/ecUxsEW9c8NW7L/YBmwkI66uTCO95VyrYB20Bz5EPjT27A2Ct/R/qnGXrRw0XzWoe6MKqasiM/LKiSlfAKMGMv02UbdGZndu03zssN4q8hC3qfzmlQs5RXI4juqUK83zLfOWCwQzjVerHXAgMBAAECgYArSC8iEGWRX/2kosDLPv8cS/R1kZlElDKK1Fs/x42d9IT1Tpkps5pa6rQMbAreK32HA8i6xRCAt0uhtE2xMySQ0lF88Ce5R6VlXACSQ0/A3iLi5kCq+J+GSgUzsO3elSU0zLEFFXCDrN2NivbrHQBnL4V0ZTxAT9yh89PqPp04gQJBAP3WK1Y7O59QrGP+izz0cJa06zvhXGwOItb8ZJAkWn3/5QdJKttw+5Lq3h3R0TqiL1vKNQ8LXQ7UmZwAlrrQKnECQQDQFaMJe8d2JFmnSOQiD58u1I+MLOPh49E/Q7AXy9drAi52g7wDp+HwVrmXpkkb2AuXQQA4zUB47q/7DNFHlvTHAkAmzyvpDz1aalcxIqXfN/BO2nJswVQEIVUqUYR+lVBQm2NE7ebjji6ok8hJ/t5ifJGJEy1xk2cy8UZbF6vz/AfBAkEAhqX5oiFeqKxM91rLhN7m+r7BRMuninyI4o9CW6Oj+bKnrjkAaL0yDvuPjPolRMdAeNeu9RKcLaUGa0WYBuM/8QJBAIvVrZBRRVLwzKdi9SKeeTA952qoL+ZYGBKA7TK1q0BP5K257qlg+92mormIudyuBM9nFRNgDy6W8Izqh7Bd+X8=";


    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    var encryptStr= encrypt.encrypt("test")

    var urlsafe = urlsafeEncode(encryptStr);
    // 加密
    document.write("<br/>");
     document.write(urlsafe);

    //解密
    encrypt.setPrivateKey(privateKeyStr);
     var decryptStr = encrypt.decrypt(urlsafeDecode(urlsafe));
    document.write("<br/>");
    document.write(decryptStr);


</script>


</body>
</html>
phpRSA
朗读
赞 · 1
版权属于:

ASCWH

本文链接:

https://www.ascwh.com/php-198.html(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

今日天气

少数派热文榜

  • 获取失败!

标签云