This article mainly summarizes and introduces the PHP-encapsulated string encryption and decryption functions and usage methods, which are very simple and practical, if you have any need, you can refer to the next article to summarize and introduce the PHP-encapsulated string encryption and decryption functions and usage methods. This article is very simple and practical. if you need it, you can refer
Frequently used PHP encryption and decryption string functions in the program
The code is as follows:
/*************************************** * ***************************** Function name: encrypt function: encryption and decryption string usage: Encryption: encrypt ('str', 'e', 'nowamagic '); decryption: encrypt ('encrypted string ', 'D', 'nowamagic '); parameter description: $ string: string to be encrypted and decrypted $ operation: whether to encrypt or decrypt: E: encrypted D: decrypt $ key: encrypted Key (key ); **************************************** * ***************************/function encrypt ($ string, $ operation, $ key = '') {$ key = md5 ($ key); $ key_length = st Rlen ($ 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,) = substr (md5 (substr ($ result, 8 ). $ key),) {return substr ($ result, 8) ;}else {return '';}} else {return str_replace ('= ','', base64_encode ($ result ));}}
Usage:
$ Id = 132; $ token = encrypt ($ id, 'e', 'A'); echo 'encryption :'. encrypt ($ id, 'e', 'A'); echo'
'; Echo 'decryption:'. encrypt ($ token, 'D ', 'A ');
Powerful authcode encryption function, Discuz! Classic code (detailed description ):
Function authcode ($ string, $ operation = 'Decode', $ key = '', $ expiry = 0) {// The length of the dynamic key, the same plaintext will generate different ciphertext based on the dynamic key $ ckey_length = 4; // key $ key = md5 ($ key? $ Key: $ GLOBALS ['discuz _ auth_key ']); // key a Participates in encryption and decryption $ keya = md5 (substr ($ key, 0, 16 )); // key B is 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 )):''; // calculate the key $ cryptkey = $ keya. md5 ($ keya. $ keyc); $ key_length = strlen ($ cryptkey); // plaintext. the first 10 digits are used to save the timestamp. data validity is verified during decryption, 10 to 26 bits are used to save $ keyb (key B). // This key is used for data integrity verification during decryption. // if the key is decoded, it starts from the $ ckey_length bit, because the $ ckey_length bit before the ciphertext stores the dynamic key to ensure correct decryption $ 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 a key book for ($ I = 0; $ I <= 255; $ I ++) {$ rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);} // use a fixed algorithm to disrupt the key book and increase randomness. it seems complicated, in fact, the ciphertext strength is not added. 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 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 Keys obtained from the key book are different or, then convert it into a character $ result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);} if ($ operation = 'decode') {// verify the data validity. see the unencrypted plaintext format 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 also the reason why different ciphertext texts can be decrypted in the same plain text. // because the encrypted ciphertext may be special characters, the replication process may be lost, therefore, return $ keyc is encoded in base64 format. str_replace ('=', '', base64_encode ($ result ));}}
$ String: string, plaintext or ciphertext in the authcode ($ string, $ operation, $ key, $ expiry) function; $ operation: DECODE indicates decryption; others indicate encryption; $ key: key; $ expiry: ciphertext validity period.
Usage:
$ Str = 'abcdef '; $ key = 'www .helloweba.com'; echo authcode ($ str, 'encoding', $ key, 0 ); // encrypt $ str = '56f4yer1di2wtzwmqsfpps9hwyojnfp2mpc8sohrrxo7bok '; echo authcode ($ str, 'Decode', $ key, 0); // decrypt
3. encryption and decryption function encrypt ():
function encrypt($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)); } }
$ String in the encrypt ($ string, $ operation, $ key) function: the string to be encrypted and decrypted; $ operation: determines whether the string is encrypted or decrypted. E indicates encryption, and D indicates decryption; $ key: the key.
Usage:
$ Str = 'abc'; $ key = 'www .helloweba.com '; $ token = encrypt ($ str, 'e', $ key); echo 'encryption :'. encrypt ($ str, 'e', $ key); echo 'decryption :'. encrypt ($ str, 'D', $ key );