Sometimes in the project we need to use PHP to encrypt specific information, that is, through the encryption algorithm to generate an encrypted string, the encrypted string can be decrypted by the decryption algorithm, so that the program to process the decrypted information.
The most common applications are in user login and some API data exchange scenarios.
I have included some of the more classic PHP encryption and decryption function code, share to everyone. Encryption and decryption principle is generally through a certain encryption and decryption algorithm, the key is added to the algorithm, and finally get the encryption and decryption results.
1, the very force of the authcode cryptographic function, discuz! Classic code (with detailed):
functionAuthcode ($string,$operation= ' DECODE ',$key= ",$expiry= 0) { //dynamic key length, the same plaintext will generate different ciphertext is dependent on the dynamic key $ckey _length= 4; //secret key $key=MD5($key?$key:$GLOBALS[' Discuz_auth_key ']); //key A will participate in 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 hold the timestamp, verify data validity when decrypting, 10 to 26 bits to save $keyb (key B),//decryption will verify data integrity through this key///If it is decoded, it will start from the $ckey_length bit, because the ciphertext before $ckey_ Length bit holds dynamic key to ensure decryption is correct $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); $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, to increase randomness, seems very complex, in fact, does not increase the intensity of 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 encryption and 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; //to derive a key from the key book, and then turn it into a character $result.=CHR(Ord($string[$i]) ^ ($box[($box[$a] +$box[$j])% 256])); } if($operation= = ' DECODE ') { //to verify data validation, see the format of unencrypted plaintext if((substr($result, 0, 10) = = 0 | |substr($result, 0, 10)- Time() > 0) &&substr($result, 10, 16) = =substr(MD5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } Else { return‘‘; } } Else { //Save the dynamic key in the ciphertext, this 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 copying process may be lost, so the base64 encoding return $KEYC.Str_replace(' = ', ',Base64_encode($result)); } }
$string in function Authcode ($string, $operation, $key, $expiry): string, plaintext or ciphertext; $operation: decode means decryption, other means encryption; $key: Key; $ Expiry: ciphertext validity period.
Usage:
$str = ' abcdef '$key = ' www.helloweba.com 'echo authcode ($str, ' ENCODE ',$key//$str = ' 56f4yer1di2wtzwmqsfpps9hwyojnfp2mpc8sohrrxo7bok ' Echo authcode ($str, ' DECODE ',$key// decryption
2, add decryption function encrypt ():
functionEncrypt$string,$operation,$key= ' '){ $key=MD5($key); $key _length=strlen($key); $string=$operation= = ' D '?Base64_decode($string):substr(MD5($string.$key), 0,8).$string; $string _length=strlen($string); $rndkey=$box=Array(); $result= ' '; for($i= 0;$i<=255;$i++){ $rndkey[$i]=Ord($key[$i%$key _length]); $box[$i]=$i; } for($j=$i= 0;$i<256;$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= = ' D '){ if(substr($result, 0,8) = =substr(MD5(substr($result, 8).$key), 0,8)){ return substr($result, 8); }Else{ return‘‘; } }Else{ return Str_replace(' = ', ',Base64_encode($result)); } }
function Encrypt ($string, $operation, $key) $string: A string that requires encryption and decryption, $operation: Determines whether it is encrypted or decrypted, e means encryption, and d means decryption; $key: Key.
Usage:
$str = ' abc '$key = ' www.helloweba.com '$token = Encrypt ($str $key Echo ' encryption: '. Encrypt ($str$keyecho ' decryption: '. Encrypt ($ Str$key
2 more classic PHP encryption and decryption function sharing