A case of PHP reversible cryptographic function code

Source: Internet
Author: User
  1. Encrypt the data and write it into a cookie.

  2. $cookie _data = $this, Encrypt ("Nowamagic", $data);

  3. $cookie = Array (

  4. ' Name ' = ' $data ',
  5. ' Value ' = $cookie _data,
  6. ' Expire ' = $user _expire,
  7. ' Domain ' = ',
  8. ' Path ' = '/',
  9. ' prefix ' and ' = '
  10. );
  11. $this->input->set_cookie ($cookie);

  12. Encryption

  13. Public function Encrypt ($key, $plain _text) {
  14. $plain _text = Trim ($plain _text);
  15. $IV = substr (MD5 ($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
  16. $c _t = MCRYPT_CFB (mcrypt_cast_256, $key, $plain _text, Mcrypt_encrypt, $IV);
  17. Return Trim (Chop (Base64_encode ($c _t));
  18. }//edit bbs.it-home.org 2013/6/7

  19. Decrypt

  20. if (Isset ($_cookie[' data '))
  21. {
  22. Assign a value to a session with a cookie
  23. $_session[' data ' = Decrypt ("Nowamagic", $_cookie[' data ');
  24. }

  25. Resolving functions

  26. function Decrypt ($key, $c _t) {
  27. $c _t = Trim (Chop (Base64_decode ($c _t));
  28. $IV = substr (MD5 ($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
  29. $p _t = MCRYPT_CFB (mcrypt_cast_256, $key, $c _t, Mcrypt_decrypt, $IV);
  30. Return Trim (Chop ($p _t));
  31. }
  32. ?>

Copy Code
  • 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.