This article mainly introduces php's method of implementing reversible encryption, and draws on the discuz's encryption principle to implement the function of performing reversible encryption through keys, which has some reference value, for more information about how to implement reversible encryption in php, see the example in this article. 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', 'encode', 'key', 3600); * $ B = authcode ('ABC', 'Deco DE ', 'key'); // within an hour, $ B (abc); otherwise, $ B is blank */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; $ tm P = $ 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.