Crypt.class.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. /**
  13. * 加密解密类
  14. */
  15. class Crypt
  16. {
  17. private static $handler = '';
  18. public static function init($type = '')
  19. {
  20. $type = $type ?: C('DATA_CRYPT_TYPE');
  21. $class = strpos($type, '\\') ? $type : 'Think\\Crypt\\Driver\\' . ucwords(strtolower($type));
  22. self::$handler = $class;
  23. }
  24. /**
  25. * 加密字符串
  26. * @param string $str 字符串
  27. * @param string $key 加密key
  28. * @param integer $expire 有效期(秒) 0 为永久有效
  29. * @return string
  30. */
  31. public static function encrypt($data, $key, $expire = 0)
  32. {
  33. if (empty(self::$handler)) {
  34. self::init();
  35. }
  36. $class = self::$handler;
  37. return $class::encrypt($data, $key, $expire);
  38. }
  39. /**
  40. * 解密字符串
  41. * @param string $str 字符串
  42. * @param string $key 加密key
  43. * @return string
  44. */
  45. public static function decrypt($data, $key)
  46. {
  47. if (empty(self::$handler)) {
  48. self::init();
  49. }
  50. $class = self::$handler;
  51. return $class::decrypt($data, $key);
  52. }
  53. }