Home  >  Article  >  Backend Development  >  PHP encryption and decryption string functions_PHP tutorial

PHP encryption and decryption string functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:591052browse

Sometimes we don’t want people to see a web page address. The simple way 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 to a string, and $plain_text is the string you want to encrypt. The 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 you want to decrypt;

Note: If you use a URL to pass an encrypted string as a parameter, you must use the URLEncode() function to encrypt it. Otherwise, the parameters obtained by decrypting with the decrypt($key, $c_t) function will not get the original characters. string.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364698.htmlTechArticleSometimes we don’t want people to see a web address. The simple way is to encrypt it into a string of characters , the function is as follows: function encrypt($key, $plain_text) { $plain_text = t...
Statement:
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