An example of PHP reversible encryption function code

WBOY
Release: 2016-07-25 08:59:02
Original
973 people have browsed it
  1. // 加密数据并写到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. // 加密

  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. //解密

  20. if( isset($_COOKIE['data']) )
  21. {
  22. //用cookie给session赋值
  23. $_SESSION['data'] = decrypt("nowamagic", $_COOKIE['data']);
  24. }

  25. //解决函数

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

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!