Php reversible encryption function code example

Source: Internet
Author: User
Tags chop
Php reversible encryption function code example

  1. // Encrypt the data and write it to the 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' =>''
  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. // Use cookies to assign values to Sessions
  23. $ _ SESSION ['data'] = decrypt ("nowamagic", $ _ COOKIE ['data']);
  24. }

  25. // Solves the function

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

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.