Rumah  >  Artikel  >  pembangunan bahagian belakang  >  支持中文的php加密解密类

支持中文的php加密解密类

WBOY
WBOYasal
2016-07-25 09:08:471069semak imbas
  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 if ($ctr == strlen ( $encrypt_key ))
  41. $ctr = 0;
  42. $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 );
  43. $ctr ++;
  44. }
  45. return $tmp;
  46. }
  47. /**
  48. * Enter description here ...
  49. * @param unknown_type $txt
  50. * @param unknown_type $key
  51. * @return string
  52. */
  53. public final static function Encrypt($txt, $key) {
  54. srand ( ( double ) microtime () * 1000000 );
  55. $encrypt_key = md5 ( rand ( 0, 32000 ) );
  56. $ctr = 0;
  57. $tmp = "";
  58. for($i = 0; $i if ($ctr == strlen ( $encrypt_key ))
  59. $ctr = 0;
  60. $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ));
  61. $ctr ++;
  62. }
  63. $_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key );
  64. return self::chkToken () ? $_code : null;
  65. }
  66. /**
  67. * Enter description here ...
  68. * @param unknown_type $txt
  69. * @param unknown_type $key
  70. * @return Ambigous
  71. */
  72. public final static function Decrypt($txt, $key) {
  73. $txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key );
  74. $tmp = "";
  75. for($i = 0; $i $md5 = substr ( $txt, $i, 1 );
  76. $i ++;
  77. $tmp .= (substr ( $txt, $i, 1 ) ^ $md5);
  78. }
  79. return self::chkToken () ? $tmp : null;
  80. }
  81. /**
  82. * Enter description here ...
  83. * @var unknown_type
  84. */
  85. private static $_key = 'lau';
  86. }
  87. ?>
复制代码
  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. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:mysql的行锁的另一种解决方案 Artikel seterusnya:wecis 2.0 RC 版