SpiTdeClient.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace ACES\spi;
  3. define("digestMethod", "sha1");
  4. define("cipherMethod", "AES-128-CBC");
  5. define("localIv", "0000000000000000");
  6. class SpiTdeClient {
  7. /**
  8. * 加密
  9. * @param $string
  10. * @param $key
  11. * @return false|string
  12. */
  13. public function _encrypt($string, $key='')
  14. {
  15. // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
  16. $key = substr(openssl_digest(openssl_digest($key, digestMethod, true), digestMethod, true), 0, 16);
  17. // openssl_encrypt 加密不同Mcrypt,对秘钥长度要求,超出16加密结果不变
  18. $data = openssl_encrypt($string, cipherMethod, $key, OPENSSL_CIPHER_AES_128_CBC, localIv);
  19. return base64_encode($data);
  20. }
  21. /**
  22. * 解密
  23. * @param string $string 需要解密的字符串
  24. * @param string $key 密钥
  25. * @return string
  26. */
  27. public function _decrypt($string, $key='')
  28. {
  29. // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
  30. $key = substr(openssl_digest(openssl_digest($key, digestMethod, true), digestMethod, true), 0, 16);
  31. return openssl_decrypt(base64_decode($string), cipherMethod, $key, OPENSSL_CIPHER_AES_128_CBC, localIv);
  32. }
  33. }