A concise PHP reversible encryption function
Many times we need to encrypt and decrypt data. For example, some data needs to be saved in cookies, but the data cannot be easily obtained by users. At this time, we need to encrypt the data and save it in cookies until we need to use them. Decrypt again.
// 加密数据并写到cookie里 $cookie_data = $this -> encrypt("nowamagic", $data); $cookie = array( 'name' => '$data', 'value' => $cookie_data, 'expire' => $user_expire, 'domain' => '', 'path' => '/', 'prefix' => '' ); $this->input->set_cookie($cookie); // 加密 public function encrypt($key, $plain_text) { $plain_text = trim($plain_text); $iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB)); $c_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT, $iv); return trim(chop(base64_encode($c_t))); } 使用的时候再解密: if( isset($_COOKIE['data']) ) { //用cookie给session赋值 $_SESSION['data'] = decrypt("nowamagic", $_COOKIE['data']); } function decrypt($key, $c_t) { $c_t = trim(chop(base64_decode($c_t))); $iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB)); $p_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT, $iv); return trim(chop($p_t)); }
Copy after login
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1378
52

