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

这篇文章主要介绍了php接口数据加密、解密、验证签名的相关资料,需要的朋友可以参考下,代码很简单,这里就不多废话了,直接奉上代码:

  1. <?php
  2. //header('Content-Type: text/xml; charset=utf-8');
  3. include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Math'.DIRECTORY_SEPARATOR.'BigInteger.php');
  4. include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Crypt'.DIRECTORY_SEPARATOR.'AES.php');
  5. include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Crypt'.DIRECTORY_SEPARATOR.'RSA.php');
  6. //密文
  7. $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';
  8. $aeskey = base64_decode('qZe60QZFxuirub2ey4+7+Q==');
  9. //AES解密,采用ECB模式
  10. $aes = new Crypt_AES(CRYPT_MODE_ECB);
  11. //设置AES密钥
  12. $aes->setKey($aeskey);
  13. //解密AES密文
  14. $plaintext = $aes->decrypt(base64_decode($crypttext));
  15. echo $plaintext;
  16. echo '<hr />';
  17. //AES加密明文
  18. //echo $aes->encrypt($plaintext);
  19. //rsa公钥
  20. $publickey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCffOeIVYut9jW5w1L5uKX4aDvd837a8JhaWm5S8YqNQfgEmfD9T+rDknXLqMT+DXeQAqGo4hBmcbej1aoMzn6hIJHk3/TfTAToNN8fgwDotHewsTCBbVkQWtDTby3GouWToVsRi1i/A0Vfb0+xM8MnF46DdhhrnZrycERBSbyrcwIDAQAB';
  21. //echo base64_decode($publickey);
  22. //rsa签名
  23. $signature = 'XHin4uUFqrKDEhKBD/hQisXLFFSxM6EZCvCPqnWCQJq3uEp3ayxmFuUgVE0Xoh4AIWjIIsOWdnaToL1bXvAFKwjCtXnkaRwUpvWrk+Q0eqwsoAdywsVQDEceG5stas1CkPtrznAIW2eBGXCWspOj+aumEAcPyYDxLhDN646Krzw=';
  24. //echo base64_decode($signature);
  25. $rsa = new Crypt_RSA();
  26. //设置RSA签名模式 CRYPT_RSA_SIGNATURE_PSS or CRYPT_RSA_SIGNATURE_PKCS1
  27. $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
  28. //var_dump($rsa->createKey());
  29. //生成RSA公钥、私钥
  30. //extract($rsa->createKey());
  31. //使用RSA私钥生成签名
  32. //$rsa->loadKey($privatekey);
  33. //$signature = $rsa->sign($plaintext);
  34. //使用RSA公钥验证签名
  35. echo $plaintext;
  36. $rsa->loadKey(base64_decode($publickey));
  37. echo $rsa->verify($plaintext, base64_decode($signature)) ? 'verified' : 'unverified';
  38. echo '<hr />';
  39. //生成RSA公钥、私钥
  40. //var_dump($rsa->createKey());
  41. extract($rsa->createKey());
  42. //使用RSA私钥加密数据
  43. $rsa->loadKey($privatekey);
  44. $ciphertext = $rsa->encrypt($plaintext);
  45. //使用RSA公钥解密数据
  46. $rsa->loadKey($publickey);
  47. echo $rsa->decrypt($ciphertext);

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