Encrypt.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace app\service;
  3. use laytp\traits\Error;
  4. use think\facade\Cache;
  5. use think\facade\Config;
  6. use think\facade\Request;
  7. class Encrypt{
  8. use Error;
  9. /**
  10. * 初始化
  11. * @param $token
  12. * @return bool
  13. * @throws \think\db\exception\DataNotFoundException
  14. * @throws \think\db\exception\DbException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. */
  17. public function encrypt($str, $key='A68AA4BA448752AC147A81ACBE2A8B4C'){
  18. $mixStr = md5(date('Y-m-d H:i:s').rand(1000,10000));
  19. $tmp = '';
  20. $strLen = strlen($str);
  21. for($i=0, $j=0; $i<$strLen; $i++, $j++){
  22. $j = $j == 32 ? 0 : $j;
  23. $tmp .= $mixStr[$j].($str[$i] ^ $mixStr[$j]);
  24. }
  25. return base64_encode($this->bind_key($tmp, $key));
  26. }
  27. public function bind_key($str, $key){
  28. $encrypt_key = md5($key);
  29. $tmp = '';
  30. $strLen = strlen($str);
  31. for($i=0, $j=0; $i<$strLen; $i++, $j++){
  32. $j = $j == 32 ? 0 : $j;
  33. $tmp .= $str[$i] ^ $encrypt_key[$j];
  34. }
  35. return $tmp;
  36. }
  37. }