Php encryption: discuz content classic encryption example

Source: Internet
Author: User
This article mainly introduces the classic encryption method of discuz content in php encryption, and analyzes the usage skills of discuz encryption in combination with the specific instance form, which has some reference value, for more information about how to encrypt discuz in php, see the following example. We will share this with you for your reference. The details are as follows:

Introduction: sometimes, we want to encrypt some sensitive fields in the table. after thinking about the solution that has not been better for a long time, I checked it online in the background, rest assured that the discuz Forum solution can solve this situation well. let's copy it and share it with you. the code is as follows:

Header ("Content-type: text/html; charset = UTF-8"); echo $ string = 'garden Road 888 '; echo'
 Encryption is valid for 10 seconds. ciphertext content: '; // $ string = '000000'; $ sss = authcode ($ string, '','', 10); echo $ sss; echo'
 Decrypted immediately after Encryption:... '; echo authcode ($ sss); sleep (6); echo'
 6 seconds later decryption... '; $ aaaa = authcode ($ sss); var_dump ($ aaaa); sleep (5); echo'
 Decrypt again in 5 seconds... '; $ aaaa = authcode ($ sss); var_dump ($ aaaa ); /***** @ param string $ string plaintext or ciphertext string * @ param string $ operation DECODE indicates decryption, others indicate encryption * @ param string $ key * @ param int $ expiry ciphertext validity period, 0 Code never expires * @ return string */function authcode ($ string, $ operation = 'Decode', $ key = '', $ expiry = 0) {// dynamic key length, 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). during decryption, the key will be used to verify data integrity. // if the key is decoded, it will start 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 ('0d', $ 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') {// substr ($ result, 0, 10) = 0 verify the data validity // substr ($ result, 0, 10) -time ()> 0 verify data validity // substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, // 16) verify data integrity // verify the data validity. please refer to 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 ));}}

The code execution result is as follows:

The encryption period of No. 888 Huayuan Road is 10 seconds. the ciphertext content is ce9eelLd6jpd7hZJTRg +/fgg8cD9VG + 1NsHvkavEKhdvhk7jcfDoQTYSAAw, which is decrypted immediately after Encryption :... 6 seconds after Garden Road 888, decryption... string 'garden Road 888 '(length = 15) decrypted after 5 seconds again... string ''(length = 0)

I hope this article will help you with PHP programming.

For more details about the typical encryption mode instance of discuz content in php encryption, please follow the PHP Chinese network!

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.