Home  >  Article  >  Backend Development  >  Support Chinese PHP encryption and decryption class

Support Chinese PHP encryption and decryption class

WBOY
WBOYOriginal
2016-07-25 09:08:471058browse
  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. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn