Analyze the usage of the PHP encryption and decryption function authcode. The specific example code of the PHP encryption and decryption function authcode is as follows: parameter explanation $ string: plaintext or ciphertext $ operation: DECODE indicates decryption, and others indicates encryption $ key: key $ expirThe sample code of the PHP encryption and decryption function authcode is as follows:
- // Parameter description
- // $ String: plaintext or ciphertext
- // $ Operation: DECODE indicates decryption, and others indicates encryption
- // $ Key: key
- // $ Expiry: ciphertext validity period
- Function authcode ($ string, $ operation =
'Decode', $ key = '', $ expiry = 0 ){
- // The length of the dynamic key. different ciphertext values are generated for the same plaintext based on the dynamic key.
- $ Ckey_length = 4;
- // Key
- $ Key = md5 ($ key? $ Key: $ GLOBALS ['discuz _ auth_key ']);
- // Key a is used for 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 is used to change the generated ciphertext
- $ Keyc = $ ckey_length? ($ Operation = 'decode'
? Substr ($ string, 0, $ ckey_length): substr (md5
(Microtime (),-$ ckey_length )):'';
- // PHP encryption and decryption function authcode involved in the calculation key
- $ Cryptkey = $ keya. md5 ($ keya. $ keyc );
- $ Key_length = strlen ($ cryptkey );
- // Plaintext. the first 10 digits are used to save the timestamp. data validity is verified during decryption,
10 to 26 bits are used to save $ keyb (key B). during decryption, the key is used to verify data integrity.
- // If it is decoded, it starts from the $ ckey_length bit, because $ ckey _
The length bit stores the dynamic key to ensure correct decryption.
- $ 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 (1, 0,255 );
- $ Rndkey = array ();
- // The authcode function of the PHP encryption and decryption function generates the key book
- For ($ I = 0; $ I <= 255; $ I ++ ){
- $ Rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);
- }
- // Use a fixed algorithm to disrupt the key book and increase randomness. it seems complicated,
In fact, it does not increase the ciphertext strength.
- 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;
- }
- // PHP encryption and decryption function authcode core encryption and decryption part
- 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 authcode function of the PHP encryption and decryption function obtains the keys from the key book and returns them to different or different characters.
- $ 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, 10)-time ()> 0 to verify data validity
- // Substr ($ result, 10, 16) = substr (md5 (substr
($ Result, 26). $ keyb), 0, 16) verify data integrity
- // Verify the data validity. see the unencrypted plaintext format.
- If (substr ($ result, 0, 10) = 0 |
Substr ($ result, 0, 10)-time ()> 0 )&&
Substr ($ result, 10, 16) = substr (md5
(Substr ($ result, 26). $ keyb), 0, 16 )){
- Return substr ($ result, 26 );
- } Else {
- Return '';
- }
- } Else {
- // The authcode function of the PHP encryption and decryption function stores the dynamic key in the ciphertext, which is why the same plaintext is used,
Cause of producing different ciphertext before decryption
- // Because the encrypted ciphertext may be special characters,
The replication process may be lost, so base64 encoding is used.
- Return $ keyc. str_replace ('= ','',
Base64_encode ($ result ));
- }
- }
The above code is the specific usage of the PHP encryption and decryption function authcode. I hope you can get a preliminary understanding of the meaning of this function through the content introduced in this article.
Encryption // parameter description // $ string: plaintext or ciphertext // $ operation: DECODE indicates decryption, and others indicates encryption // $ key: key // $ expir...