支援中文的php加密解密類

WBOY
發布: 2016-07-25 09:08:47
原創
1067 人瀏覽過
  1. /**
  2. * Copyright (c) 2011 - 01 XatuDream
  3. * XatuDream All Rights Reserved.
  4. * Support:185390516.qzone.qq.com
  5. * QQ:185390516
  6. * Author:Lau Version:1.01
  7. * Date:2010-08-12 09:28:32
  8. */
  9. ! defined ( 'WORKSPACE' ) && exit ( "Access Denied !" );
  10. class MD5Crypt {
  11. /**
  12. * Enter description here ...
  13. * @param unknown_type $str
  14. * @return string
  15. */
  16. public final static function mdsha($str) {
  17. $code = substr ( md5 ( $str ), 10 );
  18. $code .= substr ( sha1 ( $str ), 0, 28 );
  19. $code .= substr ( md5 ( $str ), 0, 22 );
  20. $code .= substr ( sha1 ( $str ), 16 ) . md5 ( $str );
  21. return self::chkToken () ? $code : null;
  22. }
  23. /**
  24. * Enter description here ...
  25. * @param unknown_type $param
  26. */
  27. private final static function chkToken() {
  28. return true;
  29. }
  30. /**
  31. * Enter description here ...
  32. * @param unknown_type $txt
  33. * @param unknown_type $encrypt_key
  34. * @return Ambigous
  35. */
  36. private final static function keyED($txt, $encrypt_key) {
  37. $encrypt_key = md5 ( $encrypt_key );
  38. $ctr = 0;
  39. $tmp = "";
  40. for($i = 0; $i < strlen ( $txt ); $i ++) {
  41. if ($ctr == strlen ( $encrypt_key ))
  42. $ctr = 0;
  43. $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 );
  44. $ctr ++;
  45. }
  46. return $tmp;
  47. }
  48. /**
  49. * Enter description here ...
  50. * @param unknown_type $txt
  51. * @param unknown_type $key
  52. * @return string
  53. */
  54. public final static function Encrypt($txt, $key) {
  55. srand ( ( double ) microtime () * 1000000 );
  56. $encrypt_key = md5 ( rand ( 0, 32000 ) );
  57. $ctr = 0;
  58. $tmp = "";
  59. for($i = 0; $i < strlen ( $txt ); $i ++) {
  60. if ($ctr == strlen ( $encrypt_key ))
  61. $ctr = 0;
  62. $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ));
  63. $ctr ++;
  64. }
  65. $_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key );
  66. return self::chkToken () ? $_code : null;
  67. }
  68. /**
  69. * Enter description here ...
  70. * @param unknown_type $txt
  71. * @param unknown_type $key
  72. * @return Ambigous
  73. */
  74. public final static function Decrypt($txt, $key) {
  75. $txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key );
  76. $tmp = "";
  77. for($i = 0; $i < strlen ( $txt ); $i ++) {
  78. $md5 = substr ( $txt, $i, 1 );
  79. $i ++;
  80. $tmp .= (substr ( $txt, $i, 1 ) ^ $md5);
  81. }
  82. return self::chkToken () ? $tmp : null;
  83. }
  84. /**
  85. * Enter description here ...
  86. * @var unknown_type
  87. */
  88. private static $_key = 'lau';
  89. }
  90. ?>
复制代码
  1. 使用方法:
  2. /**
  3. * Copyright (c) 2011 XatuDream
  4. * XatuDream All Rights Reserved.
  5. * Support:185390516.qzone.qq.com
  6. * QQ:185390516
  7. * Author:LoveCrystal Version:1.01
  8. * Date:2011-9-2 04:00:37
  9. */
  10. define ( 'WORKSPACE', '.' . DIRECTORY_SEPARATOR );
  11. header ( "Content-Type: text/html; charset=utf-8" );
  12. include_once 'Core/Library/MD5Crypt.class.php';
  13. $a = MD5Crypt::Encrypt ( "A", 100 );
  14. echo "EnCode:" . $a, "
    ";
  15. echo "DeCode:" . MD5Crypt::Decrypt ( $a, 100 );
  16. ?>
复制代码


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板