设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP运用DES进行加密与解密的方法

发布时间:2022-07-20 12:27 所属栏目:121 来源:互联网
导读:代码如下: //$input - stuff to decrypt //$key - the secret key to use function do_mencrypt($input, $key) { $input = str_replace(n, , $input); $input = str_replace(t, , $input); $input = str_replace(r, , $input); $key = substr(md5($key), 0,
       代码如下:
 
  //$input - stuff to decrypt
 
  //$key - the secret key to use
 
  function do_mencrypt($input, $key)
 
  {
 
  $input = str_replace(""n", "", $input);
 
  $input = str_replace(""t", "", $input);
 
  $input = str_replace(""r", "", $input);
 
  $key = substr(md5($key), 0, 24);
 
  $td = mcrypt_module_open('tripledes', '', 'ecb', '');
 
  $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 
  mcrypt_generic_init($td, $key, $iv);
 
  $encrypted_data = mcrypt_generic($td, $input);
 
  mcrypt_generic_deinit($td);
 
  mcrypt_module_close($td);
 
  return trim(chop(base64_encode($encrypted_data)));
 
  }
 
  //$input - stuff to decrypt
 
  //$key - the secret key to use
 
  function do_mdecrypt($input, $key)
 
  {
 
  $input = str_replace(""n", "", $input);
 
  $input = str_replace(""t", "", $input);
 
  $input = str_replace(""r", "", $input);
 
  $input = trim(chop(base64_decode($input)));
 
  $td = mcrypt_module_open('tripledes', '', 'ecb', '');
 
  $key = substr(md5($key), 0, 24);
 
  $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 
  mcrypt_generic_init($td, $key, $iv);
 
  $decrypted_data = mdecrypt_generic($td, $input);
 
  mcrypt_generic_deinit($td);
 
  mcrypt_module_close($td);
 
  return trim(chop($decrypted_data));
 
  }。
 

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读