Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. <?php
  2. function strToHex($string)
  3. {
  4. $hex="";
  5. for ($i=0;$i<strlen($string);$i++)
  6. $hex.=dechex(ord($string[$i]));
  7. $hex=strtoupper($hex);
  8. return $hex;
  9. }
  10. function hexToStr($hex)
  11. {
  12. $string="";
  13. for ($i=0;$i<strlen($hex)-1;$i+=2)
  14. $string.=chr(hexdec($hex[$i].$hex[$i+1]));
  15. return $string;
  16. }
  17.  
  18.  
  19. function keyED($txt,$encrypt_key){
  20. $encrypt_key = md5($encrypt_key);
  21. $ctr=0;
  22. $tmp = "";
  23. for ($i=0;$i<strlen($txt);$i++)
  24. {
  25. if ($ctr==strlen($encrypt_key)) $ctr=0;
  26. $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
  27. $ctr++;
  28. }
  29. return $tmp;
  30. }
  31. function encrypt($txt,$key){
  32. srand((double)microtime()*1000000);
  33. $encrypt_key = md5(rand(0,32000));
  34. $ctr=0;
  35. $tmp = "";
  36. for ($i=0;$i<strlen($txt);$i++)
  37. {
  38. if ($ctr==strlen($encrypt_key)) $ctr=0;
  39. $tmp.= substr($encrypt_key,$ctr,1) .
  40. (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
  41. $ctr++;
  42. }
  43. $ret=strtohex(base64_encode(keyED($tmp,$key)));
  44. return substr(md5($ret.$key),0,8).$ret;
  45. }
  46. function decrypt($txt,$key){
  47. //先检验校验码在不在;
  48. $first_8chars = substr($txt,0,8);
  49. $last_chars = substr($txt,8);
  50. $md5_verifier = substr(md5($last_chars.$key),0,8);
  51. if($md5_verifier !=$first_8chars){
  52. return "";
  53. }
  54. $txt = $last_chars;
  55. $txt=base64_decode(hextostr($txt));
  56. $txt = keyED($txt,$key);
  57. $tmp = "";
  58. for ($i=0;$i<strlen($txt);$i++)
  59. {
  60. $md5 = substr($txt,$i,1);
  61. $i++;
  62. $tmp.= (substr($txt,$i,1) ^ $md5);
  63. }
  64. return $tmp;
  65. }
  66. /**
  67. //$key="ooomygod这是一个密阴goooo";
  68. $key="o8342-hfsjfrr3rfs'fgtfjhnbffds'ghfwfrbvdsfsfjhssf'dsfs";
  69. $string = "需要加密的字符串,如密码等.";
  70. // 开始加密 encrypt $string, and store it in $enc_text
  71. $enc_text = encrypt($string,$key);
  72. // 开始解密 decrypt the encrypted text $enc_text, and store it in $dec_text
  73. $dec_text = decrypt($enc_text,$key);
  74. print "原始字符串 : $string <Br>\n";
  75. print "加密后字串 : $enc_text <Br>\n";
  76. print "解密后字串 : $dec_text <Br>\n";
  77. print "to decode:";
  78. print decrypt("78123be0417539566D515055552B38446F4172575562514A6777616B564F4A5A3851614355374E576E774F42564C42512B565753566555412B464C7857754E5336675332412B3447365153505537525438675456564C634F6F414B415665494439464F47412B454B2B6C48594353633Ds",$key);
  79. */
  80. ?>
Add Comment
Please, Sign In to add comment