Php Reversible Encryption Method

Source: Internet
Author: User
This article mainly introduces php's method for implementing reversible encryption, and draws on the discuz's encryption principle to implement the function of Reversible Encryption Through keys. It has some reference value. For more information, see

This article mainly introduces php's method for implementing reversible encryption, and draws on the discuz's encryption principle to implement the function of Reversible Encryption Through keys. It has some reference value. For more information, see

This article describes how php implements reversible encryption. Share it with you for your reference. The details are as follows:

The Reversible Encryption Class described here is difficult to crack without a key.

The PHP code is as follows:

<? Phpclass encryptCalss {var $ key = 12; function encode ($ txt) {for ($ I = 0; $ I Key);} return $ txt = urlencode (base64_encode (urlencode ($ txt);} function decode ($ txt) {$ txt = urldecode (base64_decode ($ txt )); for ($ I = 0; $ I Key) ;}return $ txt ;}}?>

Discuz encryption and decryption:

<? 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', 'dec ODE ', 'key'); // $ B (abc) within an hour; otherwise, $ B is empty */function authcode ($ string, $ operation = 'decode', $ key = '', $ expiry = 0) {$ ckey_length = 4; $ key = md5 ($ key? $ Key: "kalvin.cn"); $ keya = md5 (substr ($ key,); $ 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 | substr ($ result,)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb),) {returnsubstr ($ result, 26) ;}else {return '';}} else {return $ keyc. str_replace ('=', '', base64_encode ($ result) ;}}?>

I hope this article will help you with php programming.

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.