A powerful encryption and decryption function is found in discuz. This function can encrypt and restore strings within a specified period of time.
In this way, we can use this function for many purposes, such as the token encrypted transmission of Single Sign-On, temporary password, and so on.
Record it here to facilitate future application.
<? Php/*** @ param string $ string original text or ciphertext * @ param string $ operation (ENCODE | DECODE ), the default value is DECODE * @ param string $ key * @ param int $ expiry. The ciphertext validity period is valid during encryption, in seconds, 0 is permanently valid * @ return string the original text after processing or the ciphertext after base64_encode processing ** @ example ** $ a = authcode ('abc', 'encoding ', 'key'); * $ B = authcode ($ a, 'decode', 'key'); // $ B (abc) ** $ a = authcode ('abc', 'encoding', 'key', 3600); * $ B = authcode ('abc', 'decode', 'key '); // Within an hour, $ B (abc); otherwise, $ B is blank */function authcode ($ string, $ operation = 'decode', $ key = '', $ expiry = 3600) {$ ckey_length = 4; // the length of a random key ranges from 0 to 32. // Add a random key to make the ciphertext uncertain, even if the original text and key are identical, the encryption results are different each time, increasing the difficulty of cracking. // The larger the value, the larger the ciphertext change law. The ciphertext changes to the power of $ ckey_length = 16 // when this value is 0, no random key $ key = md5 ($ key? $ Key: EABAX: getAppInf ('key'); $ 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 ('0d', $ 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 {return $ keyc. str_replace ('=', '', base64_encode ($ result) ;}?>