PHP对称加密与非对称加密URL

作者: Darren 分类: php 发布时间: 2019-08-21 16:05

客户端的代码

<?php
$public = '-----BEGIN PUBLIC KEY-----
MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQBnTj4S97mYkJePENDd1SR6
sFjFFCdPQ1KTa+fPsv1VRP4+9FtDwj4Oz5MQeaw0I0hOFIjEy2wVNds5dXbY3njK
KVSw13CcQ7AtsoEy5i/gGhwEiaZewC8Of2FsHGBc5Agw99Pbbbgj+nXlPGfax4x1
+3uB3xIWc9ivnWKvTC3eV2bjvXj46d7Cz0Vn9Y0xAFY8hiY2F+jV2iJO9cNuyPWP
icIEHCfDv3WnXNU3WvwdaxVXTrGsOMIVjL4w9yhdoSFLigmUKvY0pQOaakSQaR4a
GvkznndEzVyjaQqmIAZHeoaUeGAdKyyk4GTFJyHz+QeW2S4vdatSwHoQX/yunLDV
AgMBAAE=
-----END PUBLIC KEY-----';

$appKey = 'fdjdcpcoododpddd';
$secretKey = 'ferg84ergg8gdg8v34v35dv5d';

$url = 'http://127.0.0.1:5555/lianxi/server.php?';

$params['appKey'] = $appKey;
$params['orderId'] = 1;
$params['name']     ='Darren';
$params['password'] = '123321';
$params['time']     = time();

$queryString = http_build_query($params);

$sign = getSign($params,$secretKey);
$queryString .="&sign=" .$sign;
//$url .= $queryString;

$encrypt = '';
openssl_public_encrypt($queryString,$encrypt,$public);
$encrypt = urlencode($encrypt);     //进行url编码
$url .='q=' . $encrypt;
var_dump($url);

function getSign($params,$secretKey)
{
    ksort($params);     //将数组里面的进行排序
    $q = http_build_query($params);     //将排序的数组,进行组合为一个url

    $q.=$secretKey;         //拼接好,secretKey
    return md5($q);     //最后进行md5
}

服务端代码

<?php


$private = '-----BEGIN RSA PRIVATE KEY-----
MIIEoQIBAAKCAQBnTj4S97mYkJePENDd1SR6sFjFFCdPQ1KTa+fPsv1VRP4+9FtD
wj4Oz5MQeaw0I0hOFIjEy2wVNds5dXbY3njKKVSw13CcQ7AtsoEy5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rTimUYppERfcqR//jidZqBbswXR0ef9w2i5uawTx8mUbRgSD20cYV70m2n
2nAOtOxOnnUts87pvHcZAoGAUWqMSX6cuCqvlL8NE+ecj+HV2ePtWWw94ZF3G6NO
yPtoHm/U+L3VKtW8/iLuRn2jfPhOJ3UhDJ3M4iQkSEEPUZ2NAYIpEVQFU1ZgMy5l
7ynVCkP1EL0W3GTYkbkEsdqLjl0ntOfsbFcJfeiCMgIcseyOtaR1vAmMDRuPEvVs
p8ECgYB/GnGM1HAShL1ruS/Gu8JbZN671PsG83/xMMadB6eAicS+HAnWA+BEaJ9G
/XK+r2wk+mO8WQdoEl7Z9z7ga/02WuMIGwNSR4FjY/ZtK3ei/Dd4x3a/Iv/Mb1eo
z50zvSHGS1icNS0ESJlG0UdDNefohsex8Ylfd9pPr+4fwPxcJQ==
-----END RSA PRIVATE KEY-----';

//接收q参数的
//解析编码
$q = $_GET['q'];
//解密
//将数据赋值给param
$value = '';
openssl_private_decrypt($q,$value,$private);
$params = [];
parse_str($value,$params);
//$params = $_GET;
$sign = getSign($params);
if ($sign !=$params['sign']){
    echo 'error';
}

echo 'succ';


function getSign($params)
{
    $conf = [
        'fdjdcpcoododpddd' => 'ferg84ergg8gdg8v34v35dv5d',
    ];
    if (abs($params['time']-time()>=600)){
        echo '连接超时!';
    }
    unset($params['sign']);

    ksort($params);

    $q = http_build_query($params);
    return md5($q.$conf[$params['appKey']]);
}

 

发表评论

电子邮件地址不会被公开。

7 − 2 =