PHP接口数据加密、解密、签名验证
复制代码代码如下所示:
< PHP
/ /头('content-type:文本或XML;字符集= UTF-8);
include_once(目录(目录(__file__))。directory_separator。'phpsec'。directory_separator。'math'。directory_separator。'biginteger。php);
include_once(目录(目录(__file__))。directory_separator。'phpsec'。directory_separator。'crypt'。directory_separator。'aes。php);
include_once(目录(目录(__file__))。directory_separator。'phpsec'。directory_separator。'crypt'。directory_separator。'rsa。php);
/密文
crypttext美元= 'v66ykulhfld2jelhm / / / j9qik2edr1jhdzic K oesu2gbtx2usxyvf4jgvzvoihrre8fsfkmllmjsmijo5fdrs / / / fd20byfii4jw3bo3bzshxmz6aes2dwwg4sk9mnojfoc0ismov311x5 jlguoqxkdy4f5hhpye9d xgb0g2xe / hngssy2cpqcvqtblbmixwscknhseg92lovloz8ulwkqg5o7x + qb7p / / wafaxbjxdxvzx7lmgcon6wlhkjlgxorp6bixog6lgc6ux7bat3i9 EMII + 0lujngxiq / / sdsl23hsr3youpv5c5a813nrhx4hjyd hbt1uviuml + etmjwwcpsfs2cvxiur0ce57jazvyxjk13shk3iszhlppsm / / jcdcrdy0co d5uigjadzxdsq56xsju + tlvna1j6yq2tdifyk / x6k911a5wxlkyxztd1nq + btyn3gv / / ypmhnajjmrx4trnr9nxq9xazaw4lgrg87u5qdv + nqjj1thk0xhfc69n9g2 wffrzvtgwqbrh06ihs2cwvna0s9ev + dsagyes9tk6u0zq72hjzqzhbcdh1ukw0plyihjdxpgppowgp8 / / qvvu2jultekunvgaaec3n + zfqjscdi1s6t2mtnjwywnofrbhvezfd revgpastozdqa5nqr1b + muf70r6uw6mwlj7ct9tz3jq +钙;
aeskey美元= base64_decode('qze60qzfxuirub2ey4 + 7 + Q = =);
AES解密,ECB模式
$aes = new Crypt_AES (CRYPT_MODE_ECB);
设置AES密钥
为AES -> setkey($ aeskey);
解密密文
明文=美元美元->解密AES(base64_decode($ crypttext));
回波$纯文本;
回声';
AES加密明文
返回值;
RSA公钥
美元'migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqcffoeivyut9jw5w1l5ukx4advd837a8jhawm5s8yqnqfgemfd9t + rdknxlqmt公钥= + dxeqaq4hbmcbej1aomzn6hijhk3 / / tftatonn8fgwdothewstcbbvkqwtdtby3uwtovsri1i a0vfb0 + xm8mnf46ddhhrnzrycerbsbyrcwidaqab;
/ /回声base64_decode($公钥);
签名
签名= 'xhin4uufqrkdehkbd美元/ hqisxlffsxm6ezcvcpqnwcqjq3uep3ayxmfuugve0xoh4aiwjiisowdnatol1bxvafkwjctxnkarwupvwrk + q0eqwsoadywsvqdeceg5stas1ckptrznaiw2ebgxcwspoj + aumeacpyydxlhdn646krzw =;
/ /回声base64_decode($签名);
RSA =新crypt_rsa(美元);
设置RSA签名模型crypt_rsa_signature_pss或 / / crypt_rsa_signature_pkcs1
为RSA -> setsignaturemode(crypt_rsa_signature_pkcs1);
/ / var_dump($ RSA -> createkey());
公钥和私钥的生成
/ /提取物($ RSA -> createkey());
使用RSA签名私有密钥生成
/ /美元(美元-> loadkey RSA私钥);
签名($);
使用公钥验证签名
回波$纯文本;
为RSA -> loadkey(base64_decode($公钥));
echo RSA ->验证($明文,base64_decode($签名))'verified:'unverified;
回声';
公钥和私钥的生成
/ / var_dump($ RSA -> createkey());
提取物($ RSA -> createkey());
使用RSA的私钥对数据进行加密
RSA -> loadkey美元($私钥);
$密文=;
使用RSA密钥对数据进行解密。
RSA -> loadkey美元(约合公钥);
回波$ RSA -解密($密文);
以上是本文所描述的全部内容,希望这段代码能给您一些学习php加密和解密的帮助。