Base64.class.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. /**
  12. * Base64 加密实现类
  13. * @category ORG
  14. * @package ORG
  15. * @subpackage Crypt
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. class Base64 {
  19. /**
  20. * 加密字符串
  21. * @access static
  22. * @param string $str 字符串
  23. * @param string $key 加密key
  24. * @return string
  25. */
  26. public static function encrypt($data,$key) {
  27. $key = md5($key);
  28. $data = base64_encode($data);
  29. $x=0;
  30. $len = strlen($data);
  31. $l = strlen($key);
  32. for ($i=0;$i< $len;$i++) {
  33. if ($x== $l) $x=0;
  34. $char .=substr($key,$x,1);
  35. $x++;
  36. }
  37. for ($i=0;$i< $len;$i++) {
  38. $str .=chr(ord(substr($data,$i,1))+(ord(substr($char,$i,1)))%256);
  39. }
  40. return $str;
  41. }
  42. /**
  43. * 解密字符串
  44. * @access static
  45. * @param string $str 字符串
  46. * @param string $key 加密key
  47. * @return string
  48. */
  49. public static function decrypt($data,$key) {
  50. $key = md5($key);
  51. $x=0;
  52. $len = strlen($data);
  53. $l = strlen($key);
  54. for ($i=0;$i< $len;$i++) {
  55. if ($x== $l) $x=0;
  56. $char .=substr($key,$x,1);
  57. $x++;
  58. }
  59. for ($i=0;$i< $len;$i++) {
  60. if (ord(substr($data,$i,1))<ord(substr($char,$i,1))) {
  61. $str .=chr((ord(substr($data,$i,1))+256)-ord(substr($char,$i,1)));
  62. }else{
  63. $str .=chr(ord(substr($data,$i,1))-ord(substr($char,$i,1)));
  64. }
  65. }
  66. return base64_decode($str);
  67. }
  68. }