Analysis on the principles of the PHP encryption function in the discuz program

Source: Internet
Author: User
Analysis on the principles of the PHP encryption function in the discuz program

  1. // Parameter description

  2. // $ String: plaintext or ciphertext
  3. // $ Operation: DECODE indicates decryption, and others indicates encryption
  4. // $ Key: key
  5. // $ Expiry: ciphertext validity period
  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;

  9. // Key

  10. $ Key = md5 ($ key? $ Key: $ GLOBALS ['discuz _ auth_key ']);

  11. // Key a is used for encryption and decryption.

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

If you are interested, it is very useful for learning php encryption and decryption to think about the implementation ideas of others. Note: the ownership of this function belongs to the concept of Kang Sheng and cannot be used freely.

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.