Home  >  Article  >  Backend Development  >  PHP custom function to implement string encryption and decryption

PHP custom function to implement string encryption and decryption

墨辰丷
墨辰丷Original
2018-06-05 15:31:561853browse

This article mainly introduces PHP custom functions to implement string encryption and decryption. 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 &#39;<br>&#39;.string2secret("11111111111111111");
//显示乱码
echo &#39;<br>&#39;;
//$string:要加密的字符串 $isEncrypt=true:加密 $isEncrypt=false:解密
function dencrypt($string, $isEncrypt = true, $key = "KEY_SPACE") {
 if (!isset($string{0}) || !isset($key{0})) {
  return false;
 }
 $dynKey = $isEncrypt ? hash(&#39;sha1&#39;, microtime(true)) : substr($string, 0, 40);
 $fixedKey = hash(&#39;sha1&#39;, $key);
 $dynKeyPart1 = substr($dynKey, 0, 20);
 $dynKeyPart2 = substr($dynKey, 20);
 $fixedKeyPart1 = substr($fixedKey, 0, 20);
 $fixedKeyPart2 = substr($fixedKey, 20);
 $key = hash(&#39;sha1&#39;, $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);
 $string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));
 $n = 0;
 $result = &#39;&#39;;
 $len = strlen($string);
 for ($n = 0; $n < $len; $n++) {
  $result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));
 }
 return $isEncrypt ? $dynKey . str_replace(&#39;=&#39;, &#39;&#39;, base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);
}
echo strlen(dencrypt("12345678912345"));

Summary: The above is the entire content of this article, I hope it can be helpful to everyone Learning helps.

Related recommendations:

PHP Ajax implements the real-time verification function of verification code

Detailed explanation of PHP encryption and decryption class examples

Detailed explanation of PHP's AES encryption algorithm examples

The above is the detailed content of PHP custom function to implement string encryption and decryption. 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