This article mainly introduces the fixed version code of the classic PHP encryption and decryption function Authcode (). For more information, see
This article mainly introduces the fixed version code of the classic PHP encryption and decryption function Authcode (). For more information, see
Many people use the Authcode function. This function comes from the Discuz program and is used to encrypt and decrypt strings. It can be used to set the key and expiration time. The original function code may generate characters such as +,/, and &. As a result, the function code is escaped when the value is retrieved through the URL and cannot be decrypted. The Network on the fire end is slightly modified. replace these characters with other characters, and then replace them with each other during decryption. This is perfect!
The Code is as follows:
Function authcode ($ string, $ operation = 'decode', $ key = '', $ expiry = 0) {if ($ operation = 'decode ') {$ string = str_replace ('[a]', '+', $ string); $ string = str_replace ('[B]', '&', $ string ); $ string = str_replace ('[c]',' http://www.jb51.net/ ', $ String) ;}$ ckey_length = 4; $ key = md5 ($ key? $ Key: 'livcmsencryption '); $ keya = md5 (substr ($ key, 0, 16); $ keyb = md5 (substr ($ key, 16, 16 )); $ keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 (microtime (),-$ ckey_length): ''; $ cryptkey = $ keya. md5 ($ keya. $ keyc); $ key_length = strlen ($ cryptkey); $ 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 (); for ($ I = 0; $ I <= 255; $ I ++) {$ rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);} for ($ j = $ I = 0; I 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 = 'decode') {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 {$ ustr = $ keyc. str_replace ('=', '', base64_encode ($ result); $ ustr = str_replace ('+', '[a]', $ ustr ); $ ustr = str_replace ('&', '[B]', $ ustr); $ ustr = str_replace (' http://www.jb51.net/ ',' [C] ', $ ustr); return $ ustr ;}}