找回密码
 注册

QQ登录

只需一步,快速开始

查看: 200|回复: 0

支持中文的php加密解密类代码

[复制链接]
发表于 2014-1-8 10:37:04 | 显示全部楼层 |阅读模式
支持中文的php加密解密类代码:
  1. <?php
  2. /**
  3. * Copyright (c) 2011 - 01 XatuDream
  4. * XatuDream All Rights Reserved.
  5. * Support:185390516.qzone.qq.com
  6. * QQ:185390516
  7. * Author:Lau Version:1.01
  8. * Date:2010-08-12 09:28:32
  9. */
  10. ! defined ( 'WORKSPACE' ) && exit ( "Access Denied !" );
  11. class MD5Crypt {
  12. /**
  13. * Enter description here ...
  14. * @param unknown_type $str
  15. * @return string
  16. */
  17. public final static function mdsha($str) {
  18. $code = substr ( md5 ( $str ), 10 );
  19. $code .= substr ( sha1 ( $str ), 0, 28 );
  20. $code .= substr ( md5 ( $str ), 0, 22 );
  21. $code .= substr ( sha1 ( $str ), 16 ) . md5 ( $str );
  22. return self::chkToken () ? $code : null;
  23. }
  24. /**
  25. * Enter description here ...
  26. * @param unknown_type $param
  27. */
  28. private final static function chkToken() {
  29. return true;
  30. }
  31. /**
  32. * Enter description here ...
  33. * @param unknown_type $txt
  34. * @param unknown_type $encrypt_key
  35. * @return Ambigous <string, boolean>
  36. */
  37. private final static function keyED($txt, $encrypt_key) {
  38. $encrypt_key = md5 ( $encrypt_key );
  39. $ctr = 0;
  40. $tmp = "";
  41. for($i = 0; $i < strlen ( $txt ); $i ++) {
  42. if ($ctr == strlen ( $encrypt_key ))
  43. $ctr = 0;
  44. $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 );
  45. $ctr ++;
  46. }
  47. return $tmp;
  48. }
  49. /**
  50. * Enter description here ...
  51. * @param unknown_type $txt
  52. * @param unknown_type $key
  53. * @return string
  54. */
  55. public final static function Encrypt($txt, $key) {
  56. srand ( ( double ) microtime () * 1000000 );
  57. $encrypt_key = md5 ( rand ( 0, 32000 ) );
  58. $ctr = 0;
  59. $tmp = "";
  60. for($i = 0; $i < strlen ( $txt ); $i ++) {
  61. if ($ctr == strlen ( $encrypt_key ))
  62. $ctr = 0;
  63. $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ));
  64. $ctr ++;
  65. }
  66. $_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key );
  67. return self::chkToken () ? $_code : null;
  68. }
  69. /**
  70. * Enter description here ...
  71. * @param unknown_type $txt
  72. * @param unknown_type $key
  73. * @return Ambigous <string, boolean>
  74. */
  75. public final static function Decrypt($txt, $key) {
  76. $txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key );
  77. $tmp = "";
  78. for($i = 0; $i < strlen ( $txt ); $i ++) {
  79. $md5 = substr ( $txt, $i, 1 );
  80. $i ++;
  81. $tmp .= (substr ( $txt, $i, 1 ) ^ $md5);
  82. }
  83. return self::chkToken () ? $tmp : null;
  84. }
  85. /**
  86. * Enter description here ...
  87. * @var unknown_type
  88. */
  89. private static $_key = 'lau';
  90. }
  91. ?>
复制代码

使用方法:
  1. <?php //Code Start
  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, "<br />";
  15. echo "DeCode:" . MD5Crypt::Decrypt ( $a, 100 );
  16. ?>
复制代码

来自圈子: Demo俱乐部
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-12 03:51 , Processed in 0.017523 second(s), 20 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表