From
Http://hi.baidu.com/studyphp
Function authcode ($ string, $ operation, $ key = ''){
# String is the string to be encrypted or decrypted
# Decryption during Operation decode
$ Key = MD5 ($ key? $ Key: $ globals ['discuz _ auth_key ']);
$ Key_length = strlen ($ key );
$ String = $ operation = 'decode '? Base64_decode ($ string): substr (MD5 ($ string. $ Key), 0, 8). $ string;
# Note: if it is not decryption. String is the first 8 bits of MD5 of string & Key.
$ String_length = strlen ($ string );
$ Rndkey = $ box = array ();
$ Result = '';
For ($ I = 0; $ I <= 255; $ I ++ ){
$ Rndkey [$ I] = ord ($ key [$ I % $ key_length]);
$ Box [$ I] = $ I;
}
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;
# The above process is the process of encryption and decryption. Although the writing is complex, the principle is very simple. As long as the key remains unchanged, ($ box [($ box [$ A] + $ box [$ J]) % 256]) is a unique value, during encryption, 1 ^ 0 changes to 1, and 1 ^ 0 changes to 1. In this case, 0 ^ 1 Changes to 1 during encryption, when decryption is performed, 1 ^ 1 is changed to 0.
$ Result. = CHR (ord ($ string [$ I]) ^ ($ box [($ box [$ A] + $ box [$ J]) % 256]);
Therefore, during decryption, $ result is restored.
}
If ($ operation = 'decode '){
Echo substr ($ result, 0, 8). '<br> ';
Previously, the first 8 bits of $ string are the first 8 bits of MD5 of the key, followed by the string, so the following judgment is not difficult to understand.
If (substr ($ result, 0, 8) = substr (MD5 (substr ($ result, 8). $ Key), 0, 8 )){
Return substr ($ result, 8 );
} Else {
Return '';
}
} Else {
Return str_replace ('=', '', base64_encode ($ result ));
}
}
Reprinted Please note: FromHttp://hi.baidu.com/studyphp
Appendix: base64 encoding and decoding principles
Base64 encoding converts three 8-bit bytes to four 6-bit bytes (3*8 = 4*6 = 24) the four six-byte values are still 8 bytes, but the height of the two bytes is set to 0. when a byte is valid for only 6 bits, its value space is 0 to the power of 6 of 2 minus 1, that is, 63, that is to say, the value space of each encoded base64 to be converted is (0 ~ 63 ).
In fact, 0 ~ There are many invisible characters in the ASCII code between 63, so we should make another ing. The ing table is
'A '~ 'Z '? ASCII (0 ~ 25)
'A '~ 'Z '? ASCII (26 ~ 51)
'0 '~ '9 '? ASCII (52 ~ 61)
'+ '? ASCII (62)
'/'? ASCII (63)
In this way, three 8-bit bytes can be converted to four visible characters.
Specific byte splittingMethodFor: (picture (not good, Understanding Spirit :-))
Aaaaaabb ccccdddd eeffffff // abcdef is actually 1 or 0. Instead of abcdef for clarity.
~~~~~~~~ ~~~~~~~~ ~~~~~~~~
1 byte 2 byte 3
|
//
00 aaaaaa 00 bbcccc 00 ddddee 00 ffffff
Note: The above three bytes are original, and the following four bytes are base64 encoded, and the first two are both 0.
In this case, the number of bytes in the original text should be a multiple of 3. When this condition cannot be met, the full zero bytes are used.
Complete. during conversion, the base64 encoding is replaced by the = sign, which is why some base64 encoding ends with one or two equal signs.
Because F (origin) represents the number of bytes in the original text, F (remain) represents
Table remainder, then
F (remain) = f (origin) mod 3 was established.
Therefore, the possible values of F (remain) are 0, 1, 2.
If n = [F (origin)-f (remain)]/3
When F (remain) = 0, it is converted to 4 * n Bytes of base64 encoding.
When F (remain) = 1, since a Source byte can be split into two base64 encoded bytes
Make base64 encoding a multiple of 4, so we should add two equal signs.
When F (remain) = 2, because the two original bytes can be split into three base64 encoded bytes, similarly,
An equal sign should be added.