Core tip: Hong Sing's authcode function can be said to have made a significant contribution to the PHP community in China. Including Hong Sing's own products, and most of the companies that use PHP in China Use this function for encryption, Authcode is encrypted and decrypted using an XOR operation.
Hong Sing's Authcode function can be said to have made a significant contribution to China's PHP community. Including Hong Sing's own products, and most of the companies that use PHP in China Use this function for encryption, Authcode is encrypted and decrypted using an XOR operation.
The principle is as follows, if:
Encrypt
PlainText: 1010 1001
Key: 1110 0011
Ciphertext: 0100 1010
The ciphertext 0100 1010, the decryption of the need and key can be different or down
decryption
Ciphertext: 0100 1010
Key: 1110 0011
PlainText: 1010 1001
There is no advanced algorithm, key importance is very high, so the key is how to generate the key.
Let's take a look at Hong Sing's authcode.
Parameter interpretation//$string: PlainText or ciphertext//$operation: Decode for decryption, other means encryption//$key: Key//$expiry: Ciphertext validity function Authcode ($str ing, $operation = ' DECODE ', $key = ', $expiry = 0) {//dynamic key length, the same plaintext will generate different ciphertext is dependent 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 will be used for data integrity verification $KEYB = MD5 (substr ($key, 16, 16)); Key C is used to change the generated ciphertext $KEYC = $ckey _length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): "; Key to participate in the operation $cryptkey = $keya. MD5 ($keya. $KEYC); $key _length = strlen ($cryptkey); PlainText, the first 10 bits are used to save the timestamp, the decryption of the validation data validity, 10 to 26 bits to save $keyb (key B), decryption will pass 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 that 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 key book for ($i = 0; $i <= 255; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $key _length]); }//using a fixed algorithm to disrupt the key book, adding randomness, as if complex, actually does not increase the intensity of the ciphertext for ($j = $i = 0; $i < $i) {$j = ($j + $box [$i] + $rn dkey[$i])% 256; $tmp = $box [$i]; $box [$i] = $box [$j]; $box [$j] = $tmp; }//Core plus decryption section 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 into a 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, Ten)-Time () & Gt 0 Verify data Validity//substr ($result, 16) = = SUBSTR (MD5 ($result, 0). $keyb), or (+). Verify data integrity//validation data validation, see unencrypted Format of plaintext if (substr ($result, 0, 0) = = 0 | | substr ($result,, Ten)-time () > 0) && substr ($result, ten, +) = = substr (M D5 ($keyb) ($result, 0)) {return substr ($result, 26); } else {return '; }} else {//The dynamic key is stored in the ciphertext, which is why the same plaintext, the production of different ciphertext can be decrypted after the reason//because the encrypted ciphertext may be some special characters, the copy process may be lost, so the base64 encoding retur N $keyc. Str_replace (' = ', ' ', Base64_encode ($result)); } }