PHP实现的DES加密解密实例代码_php技巧

WBOY
Libérer: 2016-05-16 19:54:46
original
1109 Les gens l'ont consulté

本文实例讲述了PHP实现的DES加密解密实例。分享给大家供大家参考,具体如下:

<&#63;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>';

Copier après la connexion

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!