欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php接口数据加密、解密、验证签名

程序员文章站 2022-06-08 20:22:00
代码很简单,这里就不多废话了,直接奉上代码 复制代码 代码如下:

代码很简单,这里就不多废话了,直接奉上代码

复制代码 代码如下:

<?php
//header('content-type: text/xml; charset=utf-8');
include_once(dirname(dirname(__file__)).directory_separator.'phpsec'.directory_separator.'math'.directory_separator.'biginteger.php');
include_once(dirname(dirname(__file__)).directory_separator.'phpsec'.directory_separator.'crypt'.directory_separator.'aes.php');
include_once(dirname(dirname(__file__)).directory_separator.'phpsec'.directory_separator.'crypt'.directory_separator.'rsa.php');
//密文
$crypttext = 'v66ykulhfld2jelhm/j9qik2edr1jhdzic/k/oesu2gbtx2usxyvf4jgvzvoihrre8fsfkmllmjsmijo5fdrs/fd20byfii4jw3bo3bzshxmz6aes2dwwg4sk9mnojfoc0ismov311x5/jlguoqxkdy4f5hhpye9d/xgb0g2xe/hngssy2cpqcvqtblbmixwscknhseg92lovloz8ulwkqg5o7x+qb7p/emii/wafaxbjxdxvzx7lmgcogon6wlhkjlgxorp6bixog6lgc6ux7bat3i9+0lujngxiq/sdsl23hsr3youpv5c5a813nrhx4hjyd/hbt1uviuml+etmjwwcpsfs2cvxiur0ce57jazvyxjk13shk3iszhlppsm/jcdcrdy0co/d5uigjadzxdsq56xsju+tlvna1j6yq2tdifyk/x6k911a5wxlkyxztd1nq+btyn3gv/wffrzvtgwqbrh06ihs2cwvna0s9ev/ypmhnajjmrx4trnr9nxq9xazaw4lgrg87u5qdv+nqjj1thk0xhfc69n9g2+dsagyes9tk6u0zq72hjzqzhbcdh1ukw0plyihjdxpgppowgp8/qvvu2jultekunvgaaec3n+gozfqjscdi1s6t2mtnjwywnofrbhvezfd/revgpastozdqa5nqr1b+muf70r6uw6mwlj7ct9tz3jq+ca';
$aeskey = base64_decode('qze60qzfxuirub2ey4+7+q==');
//aes解密,采用ecb模式
$aes = new crypt_aes(crypt_mode_ecb);
//设置aes密钥
$aes->setkey($aeskey);
//解密aes密文
$plaintext = $aes->decrypt(base64_decode($crypttext));
echo $plaintext;
echo '<hr />';
//aes加密明文
//echo $aes->encrypt($plaintext);
//rsa公钥
$publickey = 'migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqcffoeivyut9jw5w1l5ukx4advd837a8jhawm5s8yqnqfgemfd9t+rdknxlqmt+dxeqaqgo4hbmcbej1aomzn6hijhk3/tftatonn8fgwdothewstcbbvkqwtdtby3gouwtovsri1i/a0vfb0+xm8mnf46ddhhrnzrycerbsbyrcwidaqab';
//echo base64_decode($publickey);
//rsa签名
$signature = 'xhin4uufqrkdehkbd/hqisxlffsxm6ezcvcpqnwcqjq3uep3ayxmfuugve0xoh4aiwjiisowdnatol1bxvafkwjctxnkarwupvwrk+q0eqwsoadywsvqdeceg5stas1ckptrznaiw2ebgxcwspoj+aumeacpyydxlhdn646krzw=';
//echo base64_decode($signature);
$rsa = new crypt_rsa();
//设置rsa签名模式 crypt_rsa_signature_pss or crypt_rsa_signature_pkcs1
$rsa->setsignaturemode(crypt_rsa_signature_pkcs1);
//var_dump($rsa->createkey());
//生成rsa公钥、私钥
//extract($rsa->createkey());
//使用rsa私钥生成签名
//$rsa->loadkey($privatekey);
//$signature = $rsa->sign($plaintext);
//使用rsa公钥验证签名
echo $plaintext;
$rsa->loadkey(base64_decode($publickey));
echo $rsa->verify($plaintext, base64_decode($signature)) ? 'verified' : 'unverified';
echo '<hr />';
//生成rsa公钥、私钥
//var_dump($rsa->createkey());
extract($rsa->createkey());
//使用rsa私钥加密数据
$rsa->loadkey($privatekey);
$ciphertext = $rsa->encrypt($plaintext);
//使用rsa公钥解密数据
$rsa->loadkey($publickey);
echo $rsa->decrypt($ciphertext);

以上就是本文所述的全部内容了,希望本代码能给大家学习php加密解密能够带来些帮助。