Home  >  Article  >  Backend Development  >  Briefly describe the usage of data encryption, decryption and signature verification in PHP interface

Briefly describe the usage of data encryption, decryption and signature verification in PHP interface

墨辰丷
墨辰丷Original
2018-06-12 17:03:462396browse

This article mainly introduces the relevant information on PHP interface data encryption, decryption, and signature verification. Friends who need it can refer to it

The code is very simple, there will be no more nonsense here, just provide the code

The code is as follows:

<?php
//header(&#39;Content-Type: text/xml; charset=utf-8&#39;);
include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.&#39;phpsec&#39;.DIRECTORY_SEPARATOR.&#39;Math&#39;.DIRECTORY_SEPARATOR.&#39;BigInteger.php&#39;);
include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.&#39;phpsec&#39;.DIRECTORY_SEPARATOR.&#39;Crypt&#39;.DIRECTORY_SEPARATOR.&#39;AES.php&#39;);
include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.&#39;phpsec&#39;.DIRECTORY_SEPARATOR.&#39;Crypt&#39;.DIRECTORY_SEPARATOR.&#39;RSA.php&#39;);
//密文
$crypttext = &#39;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&#39;;
$aeskey = base64_decode(&#39;qZe60QZFxuirub2ey4+7+Q==&#39;);
//AES解密,采用ECB模式
$aes = new Crypt_AES(CRYPT_MODE_ECB);
//设置AES密钥
$aes->setKey($aeskey);
//解密AES密文
$plaintext = $aes->decrypt(base64_decode($crypttext));
echo $plaintext;
echo &#39;<hr />&#39;;
//AES加密明文
//echo $aes->encrypt($plaintext);
//rsa公钥
$publickey = &#39;MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCffOeIVYut9jW5w1L5uKX4aDvd837a8JhaWm5S8YqNQfgEmfD9T+rDknXLqMT+DXeQAqGo4hBmcbej1aoMzn6hIJHk3/TfTAToNN8fgwDotHewsTCBbVkQWtDTby3GouWToVsRi1i/A0Vfb0+xM8MnF46DdhhrnZrycERBSbyrcwIDAQAB&#39;;
//echo base64_decode($publickey);
//rsa签名
$signature = &#39;XHin4uUFqrKDEhKBD/hQisXLFFSxM6EZCvCPqnWCQJq3uEp3ayxmFuUgVE0Xoh4AIWjIIsOWdnaToL1bXvAFKwjCtXnkaRwUpvWrk+Q0eqwsoAdywsVQDEceG5stas1CkPtrznAIW2eBGXCWspOj+aumEAcPyYDxLhDN646Krzw=&#39;;
//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)) ? &#39;verified&#39; : &#39;unverified&#39;;
echo &#39;<hr />&#39;;
//生成RSA公钥、私钥
//var_dump($rsa->createKey());
extract($rsa->createKey());
//使用RSA私钥加密数据
$rsa->loadKey($privatekey);
$ciphertext = $rsa->encrypt($plaintext);
//使用RSA公钥解密数据
$rsa->loadKey($publickey);
echo $rsa->decrypt($ciphertext);

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

The concept and characteristics of PHP singleton mode

##The definition of the static keyword in PHP, late binding and The difference with the self keyword

php method for folder operations

The above is the detailed content of Briefly describe the usage of data encryption, decryption and signature verification in PHP interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn