In-depth study on UCenterAPI-PHP encryption and decryption

Source: Internet
Author: User
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

 

 
 
  1. // $ String: plaintext or ciphertext
  2. // $ Operation: DECODE indicates decryption, and others indicates encryption
  3. // $ Key: key
  4. // $ Expiry: ciphertext validity period
  5. // String decryption and encryption
  6. Function authcode ($ string, $ operation = 'Decode', $ key = '', $ expiry = 0 ){
  7. // The length of the dynamic key. different ciphertext values are generated for the same plaintext based on the dynamic key.
  8. $ Ckey_length = 4; // The random key length ranges from 0 to 32;
  9. // 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.
  10. // The larger the value, the larger the ciphertext change law. The ciphertext change is equal to the power of $ ckey_length of 16.
  11. // When this value is 0, no random key is generated
  12. // Key
  13. $ Key = md5 ($ key? $ Key: UC_KEY );
  14. // Key a is used for encryption and decryption.
  15. $ Keya = md5 (substr ($ key, 0, 16 ));
  16. // Key B is used for data integrity verification.
  17. $ Keyb = md5 (substr ($ key, 16, 16 ));
  18. // Key c is used to change the generated ciphertext
  19. $ Keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 ('000000'),-$ ckey_length )):'';
  20. // Key used for calculation
  21. $ Cryptkey = $ keya. md5 ($ keya. $ keyc );
  22. $ Key_length = strlen ($ cryptkey );
  23. // 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.
  24. // 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.
  25. $ String = $ operation = 'decode '? Base64_decode (substr ($ string, $ ckey_length): sprintf ('% 010d', $ expiry? $ Expiry + time (): 0). substr (md5 ($ string. $ keyb), 0, 16). $ string;
  26. $ String_length = strlen ($ string );
  27. $ Result = '';
  28. $ Box = range (1, 0,255 );
  29. $ Rndkey = array ();
  30. // Generate a key book
  31. For ($ I = 0; $ I <= 255; $ I ++ ){
  32. $ Rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);
  33. }
  34. // Use a fixed algorithm to disrupt the key book and increase randomness. it seems complicated. In fact, it does not increase the ciphertext strength.
  35. For ($ j = $ I = 0; I I <256; $ I ++ ){
  36. $ J = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256;
  37. $ Tmp = $ box [$ I];
  38. $ Box [$ I] = $ box [$ j];
  39. $ Box [$ j] = $ tmp;
  40. }
  41. // Core encryption/decryption part
  42. For ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++ ){
  43. $ A = ($ a + 1) % 256;
  44. $ J = ($ j + $ box [$ a]) % 256;
  45. $ Tmp = $ box [$ a];
  46. $ Box [$ a] = $ box [$ j];
  47. $ Box [$ j] = $ tmp;
  48. // The Keys obtained from the key book are different or converted into characters.
  49. $ Result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);
  50. }
  51. If ($ operation = 'decode '){
  52. // Verify the data validity. see the unencrypted plaintext format.
  53. If (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16 )){
  54. Return substr ($ result, 26 );
  55. } Else {
  56. Return '';
  57. }
  58. } Else {
  59. // Save the dynamic key in the ciphertext, which is why different ciphertext can be decrypted in the same plain text.
  60. // Because the encrypted ciphertext may be special characters, the copying process may be lost, so it is Base64-encoded.
  61. Return $ keyc. str_replace ('=', '', base64_encode ($ result ));
  62. }
  63. }
  64. Echo authcode ('20140901', 'incode', 'jobphper ', 0 );
  65. Echo"
    ";
  66. Echo authcode ('3575ijqlncr + R3s7Aakwy1HlvDzHw5g4oKp82qhE7q5FS88 ', 'Decode', 'jobphper', 0 );
  67. ?>

 

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.