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

本文章分享的这个php加密类是一个可以支持中文和英文的可加密码可解密的php实现类文件,有需要的同学可以参考一下,不过最好把文档编码设置为utf-8.

下面代码保存成MD5Crypt.class.php文件,代码如下:

  1. <?php
  2. class MD5Crypt {
  3. /**
  4. * Enter description here ...
  5. * @param unknown_type $str
  6. * @return string
  7. */
  8. public final static function mdsha($str) {
  9. $code = substr ( md5 ( $str ), 10 );
  10. $code .= substr ( sha1 ( $str ), 0, 28 );
  11. $code .= substr ( md5 ( $str ), 0, 22 );
  12. $code .= substr ( sha1 ( $str ), 16 ) . md5 ( $str );
  13. return self::chkToken () ? $code : null;
  14. }
  15. /**
  16. * Enter description here ...
  17. * @param unknown_type $param
  18. */
  19. private final static function chkToken() {
  20. return true;
  21. }
  22. /**
  23. * Enter description here ...
  24. * @param unknown_type $txt
  25. * @param unknown_type $encrypt_key
  26. * @return Ambigous <string, boolean>
  27. */
  28. private final static function keyED($txt, $encrypt_key) {
  29. $encrypt_key = md5 ( $encrypt_key );
  30. $ctr = 0;
  31. $tmp = "";
  32. for($i = 0; $i < strlen ( $txt ); $i ++) {
  33. if ($ctr == strlen ( $encrypt_key ))
  34. $ctr = 0;
  35. $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 );
  36. $ctr ++;
  37. }
  38. return $tmp;
  39. }
  40. /**
  41. * Enter description here ...
  42. * @param unknown_type $txt
  43. * @param unknown_type $key
  44. * @return string
  45. */
  46. public final static function Encrypt($txt, $key) {
  47. srand ( ( double ) microtime () * 1000000 );
  48. $encrypt_key = md5 ( rand ( 0, 32000 ) );
  49. $ctr = 0;
  50. $tmp = "";
  51. for($i = 0; $i < strlen ( $txt ); $i ++) {
  52. if ($ctr == strlen ( $encrypt_key ))
  53. $ctr = 0;
  54. $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ));
  55. $ctr ++;
  56. }
  57. $_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key );
  58. return self::chkToken () ? $_code : null;
  59. }
  60. /**
  61. * Enter description here ...
  62. * @param unknown_type $txt
  63. * @param unknown_type $key
  64. * @return Ambigous <string, boolean>
  65. */
  66. public final static function Decrypt($txt, $key) {
  67. $txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key );
  68. $tmp = "";
  69. for($i = 0; $i < strlen ( $txt ); $i ++) {
  70. $md5 = substr ( $txt, $i, 1 );
  71. $i ++;
  72. $tmp .= (substr ( $txt, $i, 1 ) ^ $md5);
  73. } //开源代码phpfensi.com
  74. return self::chkToken () ? $tmp : null;
  75. }
  76. /**
  77. * Enter description here ...
  78. * @var unknown_type
  79. */
  80. private static $_key = 'lau';
  81. }
  82. ?>

用法,代码如下:

  1. <?php //Code Start
  2. define ( 'WORKSPACE', '.' . DIRECTORY_SEPARATOR );
  3. header ( "Content-Type: text/html; charset=utf-8" );
  4. include_once 'Core/Library/MD5Crypt.class.php';
  5. $a = MD5Crypt::Encrypt ( "A", 100 );
  6. echo "EnCode:" . $a, "<br />";
  7. echo "DeCode:" . MD5Crypt::Decrypt ( $a, 100 );
  8. ?>