PHP encryption and decryption string function _ PHP Tutorial

Source: Internet
Author: User
PHP string encryption and decryption function. Sometimes we don't want to see a webpage address. the simple method is to encrypt it into a string of characters. the function is as follows: functionencrypt ($ key, $ plain_text) {$ plain_textt sometimes we don't want it to be seen by a webpage address. the simple method is to encrypt it into a string of characters. the function is as follows:

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

$ Key can be set as needed, and $ plain_text is the string to be encrypted. Corresponding decryption function:

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

$ Plain_text is the string to be decrypted;

Note: If you use a url to pass encrypted strings as parameters, use the URLEncode () function to encrypt them. Otherwise, use decrypt ($ key, $ c_t) the parameters obtained by function decryption cannot obtain the original string.

Encryption function encrypt ($ key, $ plain_text) {$ plain_text = t...

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.