Analysis on the use of the PHP encryption and decryption function authcode _ PHP Tutorial

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

  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): substr (md5
    (Microtime (),-$ ckey_length )):'';
  17. // PHP encryption and decryption function authcode involved in the calculation key
  18. $ Cryptkey = $ keya. md5 ($ keya. $ keyc );
  19. $ Key_length = strlen ($ cryptkey );
  20. // 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.
  21. // If it is decoded, it starts from the $ ckey_length bit, because $ ckey _
    The length bit stores the dynamic key to ensure correct decryption.
  22. $ String = $ operation = 'decode '? Base64_decode (substr
    ($ String, $ ckey_length): sprintf ('% 010d', $ expiry?
    $ Expiry + time (): 0). substr (md5 ($ string. $ keyb), 0, 16). $ string;
  23. $ String_length = strlen ($ string );
  24. $ Result = '';
  25. $ Box = range (1, 0,255 );
  26. $ Rndkey = array ();
  27. // The authcode function of the PHP encryption and decryption function generates the key book
  28. For ($ I = 0; $ I <= 255; $ I ++ ){
  29. $ Rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);
  30. }
  31. // Use a fixed algorithm to disrupt the key book and increase randomness. it seems complicated,
    In fact, it does not increase the ciphertext strength.
  32. For ($ j = $ I = 0; I I <256; $ I ++ ){
  33. $ J = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256;
  34. $ Tmp = $ box [$ I];
  35. $ Box [$ I] = $ box [$ j];
  36. $ Box [$ j] = $ tmp;
  37. }
  38. // PHP encryption and decryption function authcode core encryption and decryption part
  39. For ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++ ){
  40. $ A = ($ a + 1) % 256;
  41. $ J = ($ j + $ box [$ a]) % 256;
  42. $ Tmp = $ box [$ a];
  43. $ Box [$ a] = $ box [$ j];
  44. $ Box [$ j] = $ tmp;
  45. // 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.
  46. $ Result. = chr (ord ($ string [$ I]) ^ (
    $ Box [($ box [$ a] + $ box [$ j]) % 256]);
  47. }
  48. If ($ operation = 'decode '){
  49. // Substr ($ result, 0, 10) = 0 verify data validity
  50. // Substr ($ result, 0, 10)-time ()> 0 to verify data validity
  51. // Substr ($ result, 10, 16) = substr (md5 (substr
    ($ Result, 26). $ keyb), 0, 16) verify data integrity
  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. // 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
  60. // Because the encrypted ciphertext may be special characters,
    The replication process may be lost, so base64 encoding is used.
  61. Return $ keyc. str_replace ('= ','',
    Base64_encode ($ result ));
  62. }
  63. }

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...

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.