The specific sample code for the PHP cryptographic decryption function Authcode is as follows:
- Parameter interpretation
- $string: Clear Text or redaction
- $operation: Decode means decryption, other means encryption
- $key: Key
- $expiry: Ciphertext validity
- function Authcode ($string, $operation =
' DECODE ', $key = ' ', $expiry = 0) {
- Dynamic key length, the same plaintext will generate different ciphertext is dependent on the dynamic key
- $ Ckey_length = 4 ;
- Secret key
- $ Key = MD5 ($key $key: $GLOBALS [' Discuz_auth_key ']);
- Key A will participate in encryption and decryption
- $ Keya = MD5 (substr ($key, 0,));
- Key B will be used for data integrity verification.
- $ keyb = MD5 (substr ($key, +));
- Key C is used to change the generated ciphertext
- $ KEYC = $ckey _length? ($ Operation = = ' DECODE '
? substr ($string, 0, $ckey _length): substr (MD5
(Microtime ()),-$ckey _length)): ";
- PHP Cryptographic decryption function Authcode 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, to verify the validity of the data when decrypting,
10 to 26 bits are used to hold the $KEYB (key B), and the decryption will verify the integrity of the data through this key.
- If it is decoded, it will start from the $ckey_length bit, because the $ckey_ before the ciphertext
Length bit holds dynamic key to ensure decryption is correct
- $ string = $ Operation = = ' DECODE '? Base64_decode (substr
($string, $ckey _length)) : sprintf ('%010d ', $expiry?
$expiry + Time (): 0). substr (MD5 ($string. $keyb), 0, +). $string;
- $ String_length = strlen ($string);
- $ result = '' ;
- $ Box = Range (0, 255);
- $ Rndkey = Array ();
- PHP Cryptographic decryption function Authcode generate key book
- For ($i = 0; $i <= 255; $i + +) {
- $rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
- }
- Using a fixed algorithm to disrupt the key book, to increase randomness, seems very complex,
Actually, it doesn't increase the intensity of the ciphertext.
- For ($J = $i = 0; $i < $i + +) {
- $ J = ($j + $box [$i] + $rndkey [$i])%;
- $ tmp = $box [$i];
- $box [$i] = $box [$j];
- $box [$j] = $tmp;
- }
- PHP Cryptographic decryption function Authcode core plus decryption part
- For ($a = $J = $i = 0; $i < $string _length; $i + +) {
- $ a = ($a + 1)%;
- $ J = ($j + $box [$a])%;
- $ tmp = $box [$a];
- $box [$a] = $box [$j];
- $box [$j] = $tmp;
- PHP Cryptographic decryption function Authcode The key from the key book to the XOR, and then into the character
- $result . = CHR (Ord ($string [$i]) ^ (
$box [($box [$a] + $box [$j]) (% 256]));
- }
- if ($operation = = ' DECODE ') {
- substr ($result, 0, 10) = = 0 Verify data validity
- //substr ($result, 0, Ten)-time () > 0 Verifying data validity
- SUBSTR ($result, ten, +) = = substr (MD5 (substr
($result, +). $keyb), 0, 16) Verifying data integrity
- To verify data validation, see the format of unencrypted plaintext
- if ((substr ($result, 0, 10) = = 0 | |
substr ($result, 0, Ten)-time () > 0) &&
SUBSTR ($result, ten, +) = = SUBSTR (MD5
(Substr ($result, +). $keyb), 0, 16)) {
- Return substr ($result, 26);
- } else {
- Return ';
- }
- } else {
- PHP Cryptographic decryption function authcode The dynamic key in the ciphertext, which is why the same plaintext,
Reasons for decrypting after producing different ciphertext
- Because encrypted ciphertext can be some special characters,
The copy process may be lost, so it is encoded with base64
- Return $KEYC. Str_replace (' = ', ',
Base64_encode ($result));
- }
- }
The above code is the PHP cryptographic decryption function Authcode the specific use of the method, I hope you can through the content of this article to master the meaning of this function.
http://www.bkjia.com/PHPjc/446263.html www.bkjia.com true http://www.bkjia.com/PHPjc/446263.html techarticle the specific sample code for PHP Cryptographic decryption function Authcode is as follows://Parameter Interpretation//$string: PlainText or ciphertext//$operation: Decode means decryption, other means encryption//$key: Key//$expir ...