Home  >  Article  >  Backend Development  >  Encryption and decryption algorithm in php

Encryption and decryption algorithm in php

墨辰丷
墨辰丷Original
2018-06-05 11:54:063035browse

This article mainly introduces the encryption and decryption algorithm in php. Interested friends can refer to it. I hope it will be helpful to everyone.

The code is as follows:


//加密
function string2secret($str)
{
 $key = "123";
 $td = mcrypt_module_open(MCRYPT_DES,'','ecb','');
 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 $ks = mcrypt_enc_get_key_size($td);
 $key = substr(md5($key), 0, $ks);
 mcrypt_generic_init($td, $key, $iv);
 $secret = mcrypt_generic($td, $str);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);
 return $secret;
}
//解密
function secret2string($sec)
{
 $key = "123";
 $td = mcrypt_module_open(MCRYPT_DES,'','ecb','');
 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 $ks = mcrypt_enc_get_key_size($td);
 $key = substr(md5($key), 0, $ks);
 mcrypt_generic_init($td, $key, $iv);
 $string = mdecrypt_generic($td, $sec);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);
 return trim($string);
}
echo secret2string(string2secret("11111111111111111")); //显示结果是11111111111111111
echo string2secret("11111111111111111"); //显示乱码


php is often used Encryption and decryption functions, base64_encode, base64_decode.


Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php uses the number_format function to intercept decimals and example analysis

php implements the method of determining the format through the file header

php time function usage and example analysis


The above is the detailed content of Encryption and decryption algorithm in php. For more information, please follow other related articles on the PHP Chinese website!

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