Java教程

对称加密

本文主要是介绍对称加密,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<?php
/**
 * client
 * rsa aes md5签名
 * @author v_fanyinggang(v_fanyinggang@baidu.com)
 */
$serect = '范英刚';
$appkey = '1248995523';
//url
$url = "127.0.0.1?";
//参数
$params = array();
$params['appkey'] = $appkey;
$params['name']   = 'shiyi';
$params['password'] = 'fyg666666';
$params['time'] = time();
//http请求参
$queryString = http_build_query($params);
//获取签名
$sign = getSign($params, $serect);
//拼接签名
$queryString .= "&sign=".$sign;

//公钥加密
$public = '-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqqbyZuhT9dm84krq2lWt
76P82FQjBuGCNJtY7tgmdzW3iM2ixvH+fid0K4jx/0SbaBgH5dZiWYeaHthHuw1Z
CNNSrF/sT2Sc7gfUnroWZIJ8tbTEfNC+GUz/OCVoug2cP6pfWH/dttWfxBflcJ6o
uMv8ZygdfFeWKbwd5fucOAh8hg1EZlk3JVC5YcusHJalpFMYspBByGvW3hbXcqUK
jURNTgUjE3OpQ6N97EKJBC1cygJ0/8ifjQsCGYbxx1L4E//JkB4KVXGcyU6UNt9g
AYNd8L53svAG/DIzlxbbkW3veKc/jbOfY2HdcLOZN4BW9pQ9jlvlUshV2unA4UVK
DMG5bHJpU1z0/hv0QDAQwdihV88iLoYjEsW4jGJEVEtJuPRNsIK9GR2gbp4tAGGI
2ZbrW/W6f/fXRQffHY8MA5+s0mrsq+a11IXVCFytYCsi10kNRhdUny9MamUkNCAB
mfhR1jadJbpqOMyjAob0sezCa1E24KvCp+FXBG3TwTy2xoZ+SRpzrwWrPsAkZVSu
H5wCS3/4kEdvcgpEdfdok5aGIqKdgeCr9BsorqQXsB0SWY3Z1dnz9UlVqzFBZWs7
eXq02ukI730PN0MDb3wNBwBQ9kD0/1rVCYURLaJMTLpBGXei9jYNdLut0k84zBc/
80KedIhDjUQN9rtiIqLnwqsCAwEAAQ==
-----END PUBLIC KEY-----';
$encrypt = "";
openssl_public_encrypt($queryString, $encrypt, $public);
$encrypt = urlencode($encrypt);
//拼接url
$url .= "query=".$encrypt;
var_dump($url);
/**
 * 签名生成
 * @param $params
 * @param $serect
 * @return string
 */
function getSign($params, $serect)
{
    //字典序
    ksort($params);
    //参数拼接
    $query = http_build_query($params).$serect;
    return md5($query);
}


<?php
/**
 * serivce
 * @author v_fanyinggang(v_fanyinggang@baidu.com)
 */
$private = '-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQCqpvJm6FP12bzi
SuraVa3vo/zYVCMG4YI0m1ju2CZ3NbeIzaLG8f5+J3QriPH/RJtoGAfl1mJZh5oe
2Ee7DVkI01KsX+xPZJzuB9SeuhZkgny1tMR80L4ZTP84JWi6DZw/ql9Yf9221Z/E
F+Vwnqi4y/xnKB18V5YpvB3l+5w4CHyGDURmWTclULlhy6wclqWkUxiykEHIa9be
FtdypQqNRE1OBSMTc6lDo33sQokELVzKAnT/yJ+NCwIZhvHHUvgT/8mQHgpVcZzJ
TpQ232ABg13wvney8Ab8MjOXFtuRbe94pz+Ns59jYd1ws5k3gFb2lD2OW+VSyFXa
6cDhRUoMwblscmlTXPT+G/RAMBDB2KFXzyIuhiMSxbiMYkRUS0m49E2wgr0ZHaBu
ni0AYYjZlutb9bp/99dFB98djwwDn6zSauyr5rXUhdUIXK1gKyLXSQ1GF1SfL0xq
ZSQ0IAGZ+FHWNp0lumo4zKMChvSx7MJrUTbgq8Kn4VcEbdPBPLbGhn5JGnOvBas+
wCRlVK4fnAJLf/iQR29yCkR192iTloYiop2B4Kv0GyiupBewHRJZjdnV2fP1SVWr
MUFlazt5erTa6QjvfQ83QwNvfA0HAFD2QPT/WtUJhREtokxMukEZd6L2Ng10u63S
TzjMFz/zQp50iEONRA32u2IioufCqwIDAQABAoICACLkb7vK1R/3nT9Hwi1SR3Yw
OYqh6WeGxHCtMsnjODo3qYc3R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8rw1agWrPT6DkW68TfMMsEG41yZvcdOiGwDaT8vy2UyI8
kkzNsDtCc/pArN4NuKz+uJgDHN70/AdllL+fXFnAxTRiTCmSpLKjj1lm9sZ5F8bX
QSBbFi2rV7zIWHD9mAjsGKu69fZ3HVxLptl611ox5CtCTVO/M0IWO08uv+JOEgdG
riZ/sKLjmx5yyO+ACtmZcT77s0NdW24g0RpI3lYWEJcgp4zDnZ7HQZGm1CwBxfAd
UghFfeR6cgvGR+M6+xYoyWkMi8YC
-----END PRIVATE KEY-----';

//接收到请求参数
$query = $argv[1];
$query = urldecode($query);
//解密后的数据
$decrypt = '';
openssl_private_decrypt($query, $decrypt, $private);
//参数分割为数组
$params = [];
parse_str($decrypt, $params);
//验证签名
$sign = getSign($params);
if ($sign != $params['sign']) {
    throw new Exception('签名错误', 2001);
}
if (time() - $params['time'] > 600) {
    throw new Exception('请求超时', 4001);
}

echo "success";

/**
 * 获取签名
 *
 * @param $params
 * @return string
 */
function getSign($params)
{
    $token = [
        '1248995523'    => '范英刚',
    ];
    //  移除sign
    unset($params['sign']);
    //字典序
    ksort($params);
    //参数
    $query = http_build_query($params).$token[$params['appkey']];
    return md5($query);
}


这篇关于对称加密的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!