Classic PHP Cryptographic decryption function authcode () fix code, encrypt decrypt authcode_php tutorial

Source: Internet
Author: User
Tags ord

Classic PHP Cryptographic decryption function Authcode () repair code, encryption decryption Authcode


Authcode This function is used by many people, this function from the Discuz program, used to encrypt and decrypt the string, you can set the key (key) and expiration time, in many cases it is useful. The original function code may generate characters such as +,/, &, resulting in the retrieval of value via URL being escaped, resulting in an inability to decrypt. A little modification of the fire-side network, to replace these characters with other characters, decryption and then replace back, so it is perfect!

The code is as follows:

function Authcode ($string, $operation = ' DECODE ', $key = ", $expiry = 0) {if ($operation = = ' DECODE ') {$string = Str_rep Lace (' [a] ', ' + ', $string); $string = Str_replace (' [b] ', ' & ', $string); $string = Str_replace (' [C] ', '/', $string); } $ckey _length = 4; $key = MD5 ($key? $key: ' Livcmsencryption '); $keya = MD5 (substr ($key, 0, 16)); $KEYB = MD5 (substr ($key, 16, 16)); $KEYC = $ckey _length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): "; $cryptkey = $keya. MD5 ($keya. $KEYC); $key _length = strlen ($cryptkey); $string = $operation = = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', $expiry? $expiry + Time (): 0). substr (MD5 ($string. $keyb ), 0, (+). $string; $string _length = strlen ($string); $result = "; $box = Range (0, 255); $rndkey = Array ();   for ($i = 0; $i <= 255; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $key _length]);} for ($j = $i = 0; $i <; $i + +) {  $j = ($j + $box [$i] + $rndkey [$i])% 256;$tmp = $box [$i];  $box [$i] = $box [$j]; $box [$j] = $tmp;  } for ($a = $j = $i = 0; $i < $string _length; $i + +) {$a = ($a + 1)% 256;  $j = ($j + $box [$a])% 256;  $tmp = $box [$a];  $box [$a] = $box [$j];  $box [$j] = $tmp; $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256])); if ($operation = = ' DECODE ') {if (substr ($result, 0, ten) = = 0 | | substr ($result, 0,)-time () > 0) && Subs  TR ($result, ten, +) = = substr (MD5 (substr ($result, $keyb), 0, +)) {return substr ($result, 26);  } else {return '; }} else {$ustr = $keyc. Str_replace (' = ', ' ', Base64_encode ($result)); $ustr = str_replace (' + ', ' [a] ', $ustr); $ustr = Str_r Eplace (' & ', ' [b] ', $USTR);  $ustr = Str_replace ('/', ' [C] ', $USTR); return $ustr; }}

http://www.bkjia.com/PHPjc/979705.html www.bkjia.com true http://www.bkjia.com/PHPjc/979705.html techarticle Classic PHP Encryption decryption function Authcode () repair code, encryption and decryption Authcode Authcode This function many people use, this function from the Discuz program, used to encrypt the decryption string, you can ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.