About Discuz encryption and decryption function, I believe we all have some understanding, the Authcode function can be said to the PHP community made a significant contribution, really found discuz this function is too wonderful to write.
A study of this algorithm, in general, can be summarized as follows three points:
1, dynamic, the same string using the same key, each cipher ciphertext is not the same, and the decryption method only one, in fact, is to put the decrypted information on the ciphertext above.
2, timeliness, you can add a time limit parameters, in seconds, this is actually in the ciphertext added to the validity of the period.
3, unity, encryption and decryption all use the same function, and a relatively simple XOR algorithm.
Because this function has the above function, so the applicable environment is also many, generally used for user login and development of the API Anti-brush interface.
The code is as follows
<?php/** * $string: plaintext or redaction * $operation: Decode means decryption, other means encryption * $ key: keys * $expiry: Ciphertext validity */function authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0) { // dynamic key length, The same plaintext will generate different ciphertext depending on the dynamic key $ckey _length = 4; // key $key = md5 ($key ? $key : $GLOBALS [' Discuz_auth_key ']); // key A will participate in the decryption $keya = md5 (substr ($key, 0, &NBSP;16); // key B will be used for data integrity verification $keyb = md5 (substr ( $key, 16, 16)); // key C for the redaction $KEYC of Change Generation = $ckey _length ? ($operation == ' DECODE ' ? substr ($string, 0, $ Ckey_length): substr (MD5 (MICRotime ()), -$ckey _length)) : '; // key to participate in the Operation $cryptkey = $keya. MD5 ($keya. $KEYC); $key _length = strlen ($cryptkey) ; // plaintext, the first 10 bits are used to hold the timestamp, verify the data validity when decrypting, 10 to 26 bits to save $keyb (key B), decryption will pass this key to verify data integrity // If it is decoding, it will start from the $ckey_length bit, because the $ckey_length bit is saved dynamic key before the ciphertext to ensure the decryption is correct $ 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 (); // generate secret key book for ($i = 0; $i <= 255; $i +) { $ rndkey[$i] = ord ($cryptkey [$i % $key _length]); } // using fixed algorithms to disrupt key books, to increase randomness, seems complex, and does not actually increase the strength of ciphertext 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 and Decryption section 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 key from the key book, and then turn it into character $result .= chr (Ord ($string [$i]) ^ ($box [($box [$a ] + $box [$j]) % 256]); } if ($operation == ' DECODE ') { // substr ($result, 0, &NBSP;10) == 0 Verify data validity // substr ($result, &NBSP;0,&NBSP;10) - time () > 0 Validate data validation // substr ($result, 10, 16) &NBSP;==&NBSP;SUBSTR (MD5 (substr ($result, 26). $keyb), 0, 16) Verifying data integrity // verifying data validity, see the format of unencrypted plaintext if ((substr ($result, 0, 10) == 0 | | substr ($result, 0, 10) - time () > 0) && substr ($ RESULT,&NBSP;10,&NBSP;16) &NBSP;==&NBSP;SUBSTR (MD5 (substr ($result, 26). $keyb), 0, 16)) { return substr ($result, 26); } else { return '; } } else { // keep the dynamic key in the ciphertext, which is why the same plaintext, Causes of decryption after producing different ciphertext // Because the encrypted ciphertext may be some special characters, the copying process may be lost, so use base64 encoding return $KEYC. str_ Replace (' = ', ', base64_encode ($result)); }}
Test code
<?php$key = "morcr3wo7bflcrvj"; $res = Authcode ("123456", "ENCODE", $key); Var_dump ($res); Var_dump (Authcode ($res, " DECODE ", $key));? >string (+) "D3e07w7lr+ba/vjomlzyhn8flo4bebujxdpx+zbkhar9kwe" string (6) "123456"
Original address: PHP encryption and decryption method
Tags: encrypt and decrypt PHP
Smart recommendations
- Iphone H5 upload photo is rotated
- 128 hours for a start-up company to fall
- The "solution" subscription number does not allow cross-number payments
- ubuntu12.04 installation php5.4/php5.5
- My first experience of "home projector without screen TV"
PHP encryption and Decryption methods