欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php可逆加密函数代码一例

程序员文章站 2022-05-09 12:35:02
...
  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. ?>
复制代码