onethink的对称加解密类

本文阅读 1 分钟
首页 PHP笔记 正文
  1. <?php
  2. /**
  3. * @descript: 对称加解密类
  4. * @source: onthink
  5. */
  6. class Encrypt{
  7. /**
  8. * 系统加密方法
  9. * @param string $data 要加密的字符串
  10. * @param string $key 加密密钥
  11. * @param int $expire 过期时间 单位 秒
  12. * @return string
  13. */
  14. public static function think_encrypt($data,&nbsp;$key = '', $expire = 0) {
  15. // $key&nbsp;&nbsp;=&nbsp;md5(empty($key) ? C('DATA_AUTH_KEY') : $key);
  16. $key&nbsp;&nbsp;=&nbsp;md5($key) ;
  17. $data&nbsp;=&nbsp;base64_encode($data);
  18. $x = 0;
  19. $len&nbsp;&nbsp;=&nbsp;strlen($data);
  20. $l&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;strlen($key);
  21. $char = '';
  22. for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
  23. if ($x&nbsp;==&nbsp;$l) $x = 0;
  24. $char&nbsp;.=&nbsp;substr($key, $x, 1);
  25. $x++;
  26. }
  27. $str&nbsp;=&nbsp;sprintf(&#39;%010d&#39;,&nbsp;$expire ? $expire + time():0);
  28. for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
  29. $str&nbsp;.=&nbsp;chr(ord(substr($data, $i,&nbsp;1))&nbsp;+&nbsp;(ord(substr($char, $i, 1)))%256);
  30. }
  31. return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
  32. }
  33. /**
  34. * 系统解密方法
  35. * @param string $data 要解密的字符串 (必须是think_encrypt方法加密的字符串)
  36. * @param string $key 加密密钥
  37. * @return string
  38. */
  39. public static function think_decrypt($data,&nbsp;$key = ''){
  40. $key&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;md5($key);
  41. $data&nbsp;&nbsp;&nbsp;=&nbsp;str_replace(array(&#39;-&#39;,&#39;_&#39;),array(&#39;+&#39;,&#39;/&#39;),$data);
  42. $mod4&nbsp;&nbsp;&nbsp;=&nbsp;strlen($data) % 4;
  43. if ($mod4) {
  44. $data&nbsp;.=&nbsp;substr(&#39;====&#39;,&nbsp;$mod4);
  45. }
  46. $data&nbsp;&nbsp;&nbsp;=&nbsp;base64_decode($data);
  47. $expire&nbsp;=&nbsp;substr($data,0,10);
  48. $data&nbsp;&nbsp;&nbsp;=&nbsp;substr($data,10);
  49. if($expire&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;$expire < time()) {
  50. return '';
  51. }
  52. $x = 0;
  53. $len&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;strlen($data);
  54. $l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;strlen($key);
  55. $char&nbsp;&nbsp;&nbsp;=&nbsp;$str = '';
  56. for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
  57. if ($x&nbsp;==&nbsp;$l) $x = 0;
  58. $char&nbsp;.=&nbsp;substr($key, $x, 1);
  59. $x++;
  60. }
  61. for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
  62. if (ord(substr($data,&nbsp;$i, 1))<ord(substr($char,&nbsp;$i, 1))) {
  63. $str&nbsp;.=&nbsp;chr((ord(substr($data, $i,&nbsp;1))&nbsp;+&nbsp;256)&nbsp;-&nbsp;ord(substr($char, $i, 1)));
  64. }else{
  65. $str&nbsp;.=&nbsp;chr(ord(substr($data, $i,&nbsp;1))&nbsp;-&nbsp;ord(substr($char, $i, 1)));
  66. }
  67. }
  68. return base64_decode($str);
  69. }
  70. }
  71. ?>
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

html5有哪些新特性、移除了那些元素?
« 上一篇 05-10
基于jQuery的颜色选择器-jQuery MiniColors
下一篇 » 05-15

发表评论