Encrypt.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Biz\Account;
  3. class Encrypt
  4. {
  5. protected $cipher = MCRYPT_RIJNDAEL_128;
  6. protected $mode = MCRYPT_MODE_ECB;
  7. protected $pad_method = NULL;
  8. protected $secret_key = '';
  9. protected $iv = '';
  10. public function set_cipher($cipher)
  11. {
  12. $this->cipher = $cipher;
  13. }
  14. public function set_mode($mode)
  15. {
  16. $this->mode = $mode;
  17. }
  18. public function set_iv($iv)
  19. {
  20. $this->iv = $iv;
  21. }
  22. public function set_key($key)
  23. {
  24. $this->secret_key = $key;
  25. }
  26. public function require_pkcs5()
  27. {
  28. $this->pad_method = 'pkcs5';
  29. }
  30. protected function pad_or_unpad($str, $ext)
  31. {
  32. if ( is_null($this->pad_method) )
  33. {
  34. return $str;
  35. }
  36. else
  37. {
  38. $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
  39. if ( is_callable($func_name) )
  40. {
  41. $size = mcrypt_get_block_size($this->cipher, $this->mode);
  42. return call_user_func($func_name, $str, $size);
  43. }
  44. }
  45. return $str;
  46. }
  47. protected function pad($str)
  48. {
  49. return $this->pad_or_unpad($str, '');
  50. }
  51. protected function unpad($str)
  52. {
  53. return $this->pad_or_unpad($str, 'un');
  54. }
  55. public function encrypt($str)
  56. {
  57. $str = $this->pad($str);
  58. $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
  59. if ( empty($this->iv) )
  60. {
  61. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  62. }
  63. else
  64. {
  65. $iv = $this->iv;
  66. }
  67. mcrypt_generic_init($td, $this->secret_key, $iv);
  68. $cyper_text = mcrypt_generic($td, $str);
  69. $rt=base64_encode($cyper_text);
  70. mcrypt_generic_deinit($td);
  71. mcrypt_module_close($td);
  72. return $rt;
  73. }
  74. public function decrypt($str){
  75. $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
  76. if ( empty($this->iv) )
  77. {
  78. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  79. }
  80. else
  81. {
  82. $iv = $this->iv;
  83. }
  84. mcrypt_generic_init($td, $this->secret_key, $iv);
  85. $decrypted_text = mdecrypt_generic($td, base64_decode($str));
  86. $rt = $decrypted_text;
  87. mcrypt_generic_deinit($td);
  88. mcrypt_module_close($td);
  89. return $this->unpad($rt);
  90. }
  91. public static function hex2bin($hexdata) {
  92. $bindata = '';
  93. $length = strlen($hexdata);
  94. for ($i=0; $i < $length; $i += 2)
  95. {
  96. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  97. }
  98. return $bindata;
  99. }
  100. public static function pkcs5_pad($text, $blocksize)
  101. {
  102. $pad = $blocksize - (strlen($text) % $blocksize);
  103. return $text . str_repeat(chr($pad), $pad);
  104. }
  105. public static function pkcs5_unpad($text)
  106. {
  107. $pad = ord($text{strlen($text) - 1});
  108. if ($pad > strlen($text)) return false;
  109. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
  110. return substr($text, 0, -1 * $pad);
  111. }
  112. public function getMillisecond() {
  113. list($t1, $t2) = explode(' ', microtime());
  114. return $t2 . ceil( ($t1 * 1000) );
  115. }
  116. function getOrderId($agent){
  117. list($usec, $sec) = explode(" ", microtime());
  118. $msec=round($usec*1000);
  119. return $agent.date("YmdHis").$msec;
  120. }
  121. }
  122. // $apiUrl = 'http://demo.ky34.com:89/channelHandle';
  123. // $aes = new Aes_ecb();
  124. // $agent = '';//代理编号
  125. // $aesKey = '';//AES密钥
  126. // $md5Key = '';//MD5密钥
  127. // $timestamp = str_pad($aes->getMillisecond(),13,0);//时间戳
  128. // $orderid = $aes->getOrderId($agent);//订单号
  129. // $params = '';//待加密字符串
  130. // $aes->set_key($aesKey);
  131. // $aes->require_pkcs5();
  132. // $param = urlencode($aes->encrypt($params));//参数加密字符串
  133. // $key = md5 ($agent.$timestamp.$md5Key);//MD5校验字符串
  134. // echo $apiUrl.'?agent='.$agent.'&timestamp='.$timestamp.'&param='.$param.'&key='.$key;
  135. ?>