加入收藏 | 设为首页 | 会员中心 | 我要投稿 上饶站长网 (https://www.0793zz.com.cn/)- 数据库平台、视觉智能、智能搜索、决策智能、迁移!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP实现的DES加密解密案例代码

发布时间:2022-06-23 18:12:39 所属栏目:PHP教程 来源:互联网
导读:本文实例讲述了PHP实现的DES加密解密实例。分享给大家供大家参考,具体如下: ?php $key = very important data; function jiami($key, $str) { /* Open module, and create IV */ $td = mcrypt_module_open(des, , ecb, ); //$td = mcrypt_module_open(MCRY
  本文实例讲述了PHP实现的DES加密解密实例。分享给大家供大家参考,具体如下:
 
  <?php
  
  $key = 'very important data';
  
  function jiami($key, $str)
  
  {
  
    /* Open module, and create IV */
  
    $td = mcrypt_module_open('des', '', 'ecb', '');
  
    //$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
  
    //$td = mcrypt_module_open('des', '', 'cbc', '');
  
    $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  
    $iv_size = mcrypt_enc_get_iv_size($td);
  
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  
    /* Initialize encryption handle */
  
    if (mcrypt_generic_init($td, $key, $iv) === -1)
  
    {
  
      return FALSE;
  
    }
  
    /* Encrypt data */
  
    $c_t = mcrypt_generic($td, $str);
  
    /* Clean up */
  
    mcrypt_generic_deinit($td);
  
    mcrypt_module_close($td);
  
    return $c_t;
  
  }
  
  function jiemi($key, $str)
  
  {
  
    /* Open module, and create IV */
  
    $td = mcrypt_module_open('des', '', 'ecb', '');
  
    //$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
  
    //$td = mcrypt_module_open('des', '', 'cbc', '');
  
    $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  
    $iv_size = mcrypt_enc_get_iv_size($td);
  
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  
    /* Initialize encryption handle */
  
    if (mcrypt_generic_init($td, $key, $iv) === -1)
  
    {
  
      return FALSE;
  
    }
  
    /* Reinitialize buffers for decryption */
  
    $p_t = mdecrypt_generic($td, $str);
  
    /* Clean up */
  
    mcrypt_generic_deinit($td);
  
    mcrypt_module_close($td);
  
    return trim($p_t);
  
  }
  
  $str = 'hello world!';
  
  echo 'key:' . $key . '<br>';
  
  echo 'str:' . $str . '<br>';
  
  $jiami = jiami($key, $str);
  
  echo '加密:' . $jiami . '<br>';
  
  file_put_contents('jiamiqian.txt', $str);
  
  file_put_contents('jiamihou.txt', $jiami);
  
  $jiemi = jiemi($key, $jiami);
  
  echo '解密:' . $jiemi . '<br>';。
 

(编辑:上饶站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读