Crypt.class.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. private static $handler = '';
  17. public static function init($type=''){
  18. $type = $type?:C('DATA_CRYPT_TYPE');
  19. $class = strpos($type,'\\')? $type: 'Think\\Crypt\\Driver\\'. ucwords(strtolower($type));
  20. self::$handler = $class;
  21. }
  22. /**
  23. * 加密字符串
  24. * @param string $str 字符串
  25. * @param string $key 加密key
  26. * @param integer $expire 有效期(秒) 0 为永久有效
  27. * @return string
  28. */
  29. public static function encrypt($data,$key,$expire=0){
  30. if(empty(self::$handler)){
  31. self::init();
  32. }
  33. $class = self::$handler;
  34. return $class::encrypt($data,$key,$expire);
  35. }
  36. /**
  37. * 解密字符串
  38. * @param string $str 字符串
  39. * @param string $key 加密key
  40. * @return string
  41. */
  42. public static function decrypt($data,$key){
  43. if(empty(self::$handler)){
  44. self::init();
  45. }
  46. $class = self::$handler;
  47. return $class::decrypt($data,$key);
  48. }
  49. }