<?php
/**
* $string Clear text or ciphertext
* $operation encrypt encode or decrypt decode
* $key Key
* $expiry Key validity
*/
function Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0) {
Dynamic key length, the same plaintext will generate different ciphertext is depending on the dynamic key
Join with the secret key, you can make ciphertext without any regularity, even if the original and key is identical, the encryption results will be different each time, increase the difficulty of cracking.
The larger the value, the greater the rule of ciphertext change, and the change of ciphertext = 16 $ckey _length of the second party
When this value is 0 o'clock, the secret key is not generated
$ckey _length = 4;
Secret Spoon
$GLOBALS [' Discuz_auth_key '] here can be modified according to your own needs
$key = MD5 ($key $key: $GLOBALS [' Discuz_auth_key ']);
Key A will participate 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 used to change the generated ciphertext
$KEYC = $ckey _length? ($operation = = ' DECODE ' substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): ";
Keys for participating operations
$cryptkey = $keya. MD5 ($keya. $KEYC);
$key _length = strlen ($cryptkey);
PlainText, the first 10 bits are used to hold the timestamp, verify the validity of the data when decrypting, 10 to 26 bits to save $keyb (key B), decryption will verify data integrity through this key
If it is decoded, it will start from the $ckey_length bit, because the ciphertext before the $ckey_length bit to save the dynamic key to ensure that the 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 ();
Create a key book
for ($i = 0; $i <= 255; $i + +) {
$rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
}
Using a fixed algorithm to disrupt the key book, increase randomness, seems very complex, in fact, will not increase the intensity of the 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 Plus 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;
To derive a key from a key book, or to convert it to a character.
$result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]);
}
if ($operation = = ' DECODE ') {
substr ($result, 0, 10) = 0 Validating data validity
substr ($result, 0,)-time () > 0 Validate data validation
substr ($result) = = substr (MD5 (substr ($result,). $keyb), 0, 16) validating data integrity
Verify data validation, see the format of unencrypted plaintext
if (substr ($result, 0,) = 0 | | substr ($result, 0)-time () > 0) && substr ($result,) = = SUBSTR (MD 5 (substr ($result, num). $keyb), 0, 16)) {
Return substr ($result, 26);
} else {
Return ";
}
} else {
Keep the dynamic key in the ciphertext, which is why the same clear text can be decrypted after producing a different cipher.
Because the encrypted ciphertext may be some special characters, the copy process may be lost, so use base64 encoding
Return $KEYC. Str_replace (' = ', ', Base64_encode ($result));
}
}
$a = www.jb51.net;
$b = Authcode ($a, "ENCODE", "abc123");
echo $b. " <br/> ";
Echo Authcode ($b, "DECODE", "abc123");
?>