1. Preface
Hong Sing's Authcode function is a bull fork, is a cryptographic function with the validity period, the same character each time the result of encryption is inconsistent, and can be customized to set the expiration time.
Design principle: Authcode is encrypted and decrypted using XOR or operation.
Encryption
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
Decrypt
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.
2. Code parsing
1<?PHP2 3 /**4 * @param string $string original text or ciphertext5 * @param string $operation operation (ENCODE | DECODE), default is DECODE6 * @param string $key key7 * @param int $expiry ciphertext validity, encryption time valid, unit seconds, 0 for permanent effective8 * @return The original text after string processing or the ciphertext after Base64_encode processing9 *************************Ten * @example One * A * $a = authcode (' abc ', ' ENCODE ', ' key '); - * $b = Authcode ($a, ' DECODE ', ' key '); $b (ABC) - * the * $a = authcode (' abc ', ' ENCODE ', ' key ', 3600); - * $b = Authcode (' abc ', ' DECODE ', ' key ');//within one hours, $b (ABC), otherwise $b empty - */ - functionAuthcode ($string,$operation= ' DECODE ',$key= ",$expiry= 3600) { + - //random key length value 0-32; + //Join with secret key, can make ciphertext no rules, even if the original and key exactly the same, the encryption results will be different each time, increase the difficulty of cracking. A The greater the value, the greater the ciphertext variation , the ciphertext change = 16 $ckey _length at //When this value is 0 o'clock, the random key is not generated - $ckey _length= 4; - - $key=MD5($key?$key: ' Default_key ');//Here you can fill in the default key value - $keya=MD5(substr($key, 0, 16));//key A will participate in encryption and decryption [KEYA = MD5 new key Top 16] - $keyb=MD5(substr($key, 16, 16));//key B will be used for data integrity verification [KEYB = MD5 new key after 16 bits] in //Key C for ciphertext generated by change - //encryption: KEYC = Current time milliseconds do MD5 encryption, intercept end random key length character to //decryption: KEYC = intercepts the string at the end of the passed random key length character + $KEYC=$ckey _length? ($operation= = ' DECODE '?substr($string, 0,$ckey _length):substr(MD5(Microtime()), -$ckey _length)) : ‘‘; - //key to participate in the Operation the $cryptkey=$keya.MD5($keya.$KEYC); * $key _length=strlen($cryptkey); $ Panax Notoginseng //plaintext, the first 10 bits are used to hold the timestamp, verify data validation when decrypting, 10 to 26 bits to hold $keyb (key B), and the key to verify the integrity of the data during decryption - //If it is decoded, it will start from the $ckey_length bit, because the $ckey_length bit in the ciphertext saves the dynamic key to ensure the decryption is correct . the $string=$operation= = ' DECODE '?Base64_decode(substr($string,$ckey _length)) :sprintf('%010d ',$expiry?$expiry+ Time(): 0).substr(MD5($string.$keyb), 0, 16).$string; + $string _length=strlen($string); A $result= ' '; the $box=Range(0, 255); + - $rndkey=Array(); $ //Generate key Book $ for($i= 0;$i<= 255;$i++) { - $rndkey[$i] =Ord($cryptkey[$i%$key _length]); - } the - //using a fixed algorithm to disrupt the key book, to increase randomness, seems very complex, in fact, does not increase the intensity of ciphertextWuyi for($j=$i= 0;$i< 256;$i++) { the $j= ($j+$box[$i] +$rndkey[$i])% 256; - $tmp=$box[$i]; Wu $box[$i] =$box[$j]; - $box[$j] =$tmp; About } $ - //core encryption and decryption section - for($a=$j=$i= 0;$i<$string _length;$i++) { - $a= ($a+ 1)% 256; A $j= ($j+$box[$a])% 256; + $tmp=$box[$a]; the $box[$a] =$box[$j]; - $box[$j] =$tmp; $ //to derive a key from the key book, and then turn it into a character the $result.=CHR(Ord($string[$i]) ^ ($box[($box[$a] +$box[$j])% 256])); the } the the if($operation= = ' DECODE ') { - //substr ($result, 0, 10) = = 0 Verify data validity in //substr ($result, 0, Ten)-time () > 0 Verify data Validity the ///substr ($result, ten, +) = = substr (MD5 ($result, $keyb), 0, 16) Verifying data integrity the //Verify data validity, see the format of unencrypted plaintext About if((substr($result, 0, 10) = = 0 | |substr($result, 0, 10)- Time() > 0) &&substr($result, 10, 16) = =substr(MD5(substr($result, 26).$keyb), 0, 16)) { the return substr($result, 26); the}Else { the return‘‘; + } -}Else { the //The dynamic key is stored in the ciphertext, which is why the same clear text, the production of different ciphertext can be decrypted after the reasonBayi //Because the encrypted ciphertext may be some special characters, the copying process may be lost, so it is encoded with Base64 the return $KEYC.Str_replace(' = ', ',Base64_encode($result)); the } - -}
3. Reference documents
1, "Discuz Classic PHP encryption and decryption function Authcode analysis"
Hong Sing (discuz) the PHP Plus decryption algorithm function