Home > Backend Development > PHP Tutorial > UCenter's reversible encryption function authcode

UCenter's reversible encryption function authcode

WBOY
Release: 2016-07-25 08:59:04
Original
885 people have browsed it
  1. //Reversible encryption function

  2. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  3. $ckey_length = 4; // Random key length value is 0-32;

  4. // Adding a random key can make the ciphertext irregular, even if the original text and the key are exactly the same, the encryption The results will also be different every time, increasing the difficulty of cracking.
  5. // The larger the value, the greater the ciphertext change pattern. The ciphertext change = 16 to the power of $ckey_length
  6. // When this value is 0, no random key will be generated
  7. $key = md5($key ? $key : UC_KEY);
  8. $keya = md5(substr($key, 0, 16));
  9. $keyb = md5(substr($key, 16, 16));
  10. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

  11. $cryptkey = $keya .md5($keya.$keyc);

  12. $key_length = strlen($cryptkey);

  13. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length )) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;

  14. $string_length = strlen($string );

  15. $result = '';

  16. $box = range(0, 255);

  17. $rndkey = array();

  18. for($ i = 0; $i <= 255; $i++) {
  19. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  20. }

  21. for( $j = $i = 0; $i < 256; $i++) {

  22. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  23. $tmp = $box[ $i];
  24. $box[$i] = $box[$j];
  25. $box[$j] = $tmp;
  26. }

  27. for($a = $j = $i = 0; $i < $string_length; $i++) {

  28. $a = ($a + 1) % 256;
  29. $j = ($j + $box[$a]) % 256;
  30. $tmp = $box[$a];
  31. $box[$a] = $box[$j];
  32. $box[$j] = $tmp;
  33. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  34. }

  35. if($operation == 'DECODE') {

  36. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5( substr($result, 26).$keyb), 0, 16)) {
  37. return substr($result, 26);
  38. } else {
  39. return '';
  40. }
  41. } else {
  42. return $keyc.str_replace( '=', '', base64_encode($result));
  43. }
  44. }

  45. //Call example

  46. $string = authcode("Hello","ENCODE","HTML_TCCJ_AUTH") ;
  47. echo $string,'
    ';
  48. echo authcode($string,"DECODE","HTML_TCCJ_AUTH"),'
    ';
  49. ?>

Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template