PHP encryption and decryption function Authcode specific use of methods Analysis _php Tutorial

Source: Internet
Author: User
The specific sample code for the PHP cryptographic decryption function Authcode is as follows:

  1. Parameter interpretation
  2. $string: Clear Text or redaction
  3. $operation: Decode means decryption, other means encryption
  4. $key: Key
  5. $expiry: Ciphertext validity
  6. function Authcode ($string, $operation =
    ' DECODE ', $key = ' ', $expiry = 0) {
  7. Dynamic key length, the same plaintext will generate different ciphertext is dependent on the dynamic key
  8. $ Ckey_length = 4 ;
  9. Secret key
  10. $ Key = MD5 ($key $key: $GLOBALS [' Discuz_auth_key ']);
  11. Key A will participate in encryption and decryption
  12. $ Keya = MD5 (substr ($key, 0,));
  13. Key B will be used for data integrity verification.
  14. $ keyb = MD5 (substr ($key, +));
  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 Cryptographic decryption function Authcode key to participate in the operation
  18. $ Cryptkey = $keya. MD5 ($keya. $KEYC);
  19. $ Key_length = strlen ($cryptkey);
  20. PlainText, the first 10 bits are used to hold the timestamp, to verify the validity of the data when decrypting,
    10 to 26 bits are used to hold the $KEYB (key B), and the decryption will verify the integrity of the data through this key.
  21. If it is decoded, it will start from the $ckey_length bit, because the $ckey_ before the ciphertext
    Length bit holds dynamic key to ensure decryption is correct
  22. $ string = $ Operation = = ' DECODE '? Base64_decode (substr
    ($string, $ckey _length)) : sprintf ('%010d ', $expiry?
    $expiry + Time (): 0). substr (MD5 ($string. $keyb), 0, +). $string;
  23. $ String_length = strlen ($string);
  24. $ result = '' ;
  25. $ Box = Range (0, 255);
  26. $ Rndkey = Array ();
  27. PHP Cryptographic decryption function Authcode generate key book
  28. For ($i = 0; $i <= 255; $i + +) {
  29. $rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
  30. }
  31. Using a fixed algorithm to disrupt the key book, to increase randomness, seems very complex,
    Actually, it doesn't increase the intensity of the ciphertext.
  32. For ($J = $i = 0; $i < $i + +) {
  33. $ J = ($j + $box [$i] + $rndkey [$i])%;
  34. $ tmp = $box [$i];
  35. $box [$i] = $box [$j];
  36. $box [$j] = $tmp;
  37. }
  38. PHP Cryptographic decryption function Authcode core plus decryption part
  39. For ($a = $J = $i = 0; $i < $string _length; $i + +) {
  40. $ a = ($a + 1)%;
  41. $ J = ($j + $box [$a])%;
  42. $ tmp = $box [$a];
  43. $box [$a] = $box [$j];
  44. $box [$j] = $tmp;
  45. PHP Cryptographic decryption function Authcode The key from the key book to the XOR, and then into the character
  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, Ten)-time () > 0 Verifying data validity
  51. SUBSTR ($result, ten, +) = = substr (MD5 (substr
    ($result, +). $keyb), 0, 16) Verifying data integrity
  52. To verify data validation, see the format of unencrypted plaintext
  53. if ((substr ($result, 0, 10) = = 0 | |
    substr ($result, 0, Ten)-time () > 0) &&
    SUBSTR ($result, ten, +) = = SUBSTR (MD5
    (Substr ($result, +). $keyb), 0, 16)) {
  54. Return substr ($result, 26);
  55. } else {
  56. Return ';
  57. }
  58. } else {
  59. PHP Cryptographic decryption function authcode The dynamic key in the ciphertext, which is why the same plaintext,
    Reasons for decrypting after producing different ciphertext
  60. Because encrypted ciphertext can be some special characters,
    The copy process may be lost, so it is encoded with base64
  61. Return $KEYC. Str_replace (' = ', ',
    Base64_encode ($result));
  62. }
  63. }

The above code is the PHP cryptographic decryption function Authcode the specific use of the method, I hope you can through the content of this article to master the meaning of this function.


http://www.bkjia.com/PHPjc/446263.html www.bkjia.com true http://www.bkjia.com/PHPjc/446263.html techarticle the specific sample code for PHP Cryptographic decryption function Authcode is as follows://Parameter Interpretation//$string: PlainText or ciphertext//$operation: Decode means decryption, other means encryption//$key: Key//$expir ...

  • Related Article

    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.