PHP string encryption function obtained from Discuz

Source: Internet
Author: User

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) ;}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.