The encryption and decryption function in UCenterAPI, known as a classic in the php field, is also a major contribution of Kangsheng to php. it can generate dynamic ciphertext through a KEY, you can use this KEY to solve the problem.
The encryption and decryption function in the UCenter API is called a classic in the php field and is also a major contribution made by Kangsheng for php.
This function can generate dynamic ciphertext through a KEY, and then use this KEY to parse
- // $ String: plaintext or ciphertext
- // $ Operation: DECODE indicates decryption, and others indicates encryption
- // $ Key: key
- // $ Expiry: ciphertext validity period
- // String decryption and encryption
- 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; // The random key length ranges from 0 to 32;
- // Adding a random key can make the ciphertext irregular. even if the original text and the key are identical, the encryption results will be different each time, increasing the difficulty of cracking.
- // The larger the value, the larger the ciphertext change law. The ciphertext change is equal to the power of $ ckey_length of 16.
- // When this value is 0, no random key is generated
- // Key
- $ Key = md5 ($ key? $ Key: UC_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 ('000000'),-$ ckey_length )):'';
- // Key used for calculation
- $ 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, and 10 to 26 digits are used to save $ keyb (key B). Data integrity is verified through this key during decryption.
- // If it is decoded, it starts from the $ ckey_length bit, because the $ ckey_length bit before the ciphertext 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 ();
- // Generate a 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;
- }
- // Core encryption/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 Keys obtained from the key book are different or converted into characters.
- $ Result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);
- }
- If ($ operation = 'decode '){
- // 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 {
- // Save the dynamic key in the ciphertext, which is why different ciphertext can be decrypted in the same plain text.
- // Because the encrypted ciphertext may be special characters, the copying process may be lost, so it is Base64-encoded.
- Return $ keyc. str_replace ('=', '', base64_encode ($ result ));
- }
- }
- Echo authcode ('20140901', 'incode', 'jobphper ', 0 );
- Echo"
";
- Echo authcode ('3575ijqlncr + R3s7Aakwy1HlvDzHw5g4oKp82qhE7q5FS88 ', 'Decode', 'jobphper', 0 );
- ?>
I did not write this explanation. I have a lot on the internet and cannot find the original author.
Reprinted address http://www.dozer.cc/2011/01/ucenter-api-in-depth-3rd/