PHP encryption and decryption function Authcode usage detailed analysis _php instance

Source: Internet
Author: User

Core tip: Kangsheng's authcode function can be said to make a significant contribution to China's PHP industry. Including Kangsheng's own products, and most Chinese companies using PHP use this function for encryption, Authcode is encrypted and decrypted using XOR. The Authcode function of the
 
Kangsheng can be said to have made a significant contribution to China's PHP industry. Including Kangsheng's own products, and most Chinese companies using PHP use this function for encryption, Authcode is encrypted and decrypted using XOR. The
  
principle is as follows:
 
encryption
 
Clear text: 10 1001
 
Key: 1110 0011
 
redaction: 0100 1010
 
Get redaction 0100 1010, decryption needs and keys different or under the
&NB Sp
decrypting
 
redaction: 0100 1010
 
Key: 1110 0011
 
PlainText: 1010 1001
&N Bsp
There is no advanced algorithm, the key is very important, so the key is how to generate the key.
 
So let's see what Kangsheng's Authcode do.

 Parameter interpretation//$string: PlainText or ciphertext//$operation: Decode means decryption, other means encryption//$key: Key//$expiry: redaction validity function Authcod  
     
   E ($string, $operation = ' DECODE ', $key = ', $expiry = 0) {//dynamic key length, the same plaintext will generate different ciphertext is depending on the dynamic key $ckey _length = 4;  
     
   Key $key = MD5 ($key $key: $GLOBALS [' Discuz_auth_key ']);  
   Key A will participate in the encryption and decryption $keya = MD5 (substr ($key, 0, 16));  
   Key B is used to do data integrity validation $keyb = MD5 (substr ($key, 16, 16)); Key C used to change the generated ciphertext $KEYC = $ckey _length?  
   ($operation = = ' DECODE ' substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): ";  
   The key $cryptkey = $keya. MD5 ($keya. $KEYC) that participate in the operation;  
   $key _length = strlen ($cryptkey); Clear text, the first 10 bits to save the timestamp, decrypt the data validation, 10 to 26 bits to save $keyb (key B), decryption will be through this key to verify the integrity of the data//if it is decoded, will start from the $ckey_length bit, because the ciphertext before $ckey_ The length bit holds the dynamic key to ensure the decryption is correct $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 ();  
   Generate the key book for ($i = 0; $i <= 255 $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $key _length]); //Using fixed algorithm to disturb the key book, increase the randomness, it seems very complex, in fact, will not increase the intensity of the ciphertext for ($j = $i = 0; $i < 256 $i + +) {$j = ($j + $box [$i] +  
     $rndkey [$i])% 256;  
     $tmp = $box [$i];  
     $box [$i] = $box [$j];  
   $box [$j] = $tmp;  
     }//Core decryption part 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;  
   The key is derived from the key book, and then converted to the character $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j]) (% 256])); } if ($operation = = ' DECODE ') {//substr ($result, 0, 10) = = 0 Verify data validity//substr ($result, 0,)-Time ( > 0 Validation of data validation//substr ($result,) = = substr (MD5 ($result, 26). $keyb), 0, 16 Verify data integrity//validation data validation, see unencrypted plaintext format if (substr ($result, 0,) = 0 | | substr ($result, 0,)-time () > 0) && substr ($result) = = substr (MD5 (substr ($result,). $keyb), 0) {return substr (  
     $result, 26);  
     else {return '; } else {//Keep the dynamic key in the ciphertext, which is why the same plaintext, the reason why the production of different ciphertext can be decrypted//because the encrypted ciphertext may be some special characters, the copy process may be lost, so use base64 code R  
   Eturn $keyc. Str_replace (' = ', ', Base64_encode ($result));
 }  
 }

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.