A concise PHP reversible cryptographic function (share) _php Tutorial

Source: Internet
Author: User
Many times we need to encrypt and decrypt data, such as some data that needs to be saved to a cookie, but not easily accessible by the user, and we need to encrypt the data to be stored in a cookie, and then decrypt it when we need to use it.

The encryption process is as follows:
Copy the Code code as follows:
Encrypt the data and write it into a cookie.
$cookie _data = $this, Encrypt ("Nowamagic", $data);

$cookie = Array (
' Name ' = ' $data ',
' Value ' = $cookie _data,
' Expire ' = $user _expire,
' Domain ' = ',
' Path ' = '/',
' prefix ' and ' = '
);
$this->input->set_cookie ($cookie);

Encryption
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));
}

Re-decrypt when used:
if (Isset ($_cookie[' data '))
{
//Use a cookie to assign a value to the 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));
}

The use of this reversible cryptographic function is recorded here.

http://www.bkjia.com/PHPjc/327440.html www.bkjia.com true http://www.bkjia.com/PHPjc/327440.html techarticle many times we need to encrypt the data, for example, some data need to be saved to the cookie, but can not be easily obtained by the user, then we need to encrypt these numbers ...

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