Method and example analysis of implementing DES encryption and decryption in PHP

墨辰丷
Release: 2023-03-29 16:46:01
Original
1536 people have browsed it

This article mainly introduces the DES encryption and decryption method implemented by PHP, and analyzes the related techniques of PHP implementation of DES encryption and decryption in the form of a complete example. Friends in need can refer to it

The details are as follows:

<?php
$key = &#39;very important data&#39;;
function jiami($key, $str)
{
  /* Open module, and create IV */
  $td = mcrypt_module_open(&#39;des&#39;, &#39;&#39;, &#39;ecb&#39;, &#39;&#39;);
  //$td = mcrypt_module_open(MCRYPT_DES, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
  //$td = mcrypt_module_open(&#39;des&#39;, &#39;&#39;, &#39;cbc&#39;, &#39;&#39;);
  $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(&#39;des&#39;, &#39;&#39;, &#39;ecb&#39;, &#39;&#39;);
  //$td = mcrypt_module_open(MCRYPT_DES, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
  //$td = mcrypt_module_open(&#39;des&#39;, &#39;&#39;, &#39;cbc&#39;, &#39;&#39;);
  $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 = &#39;hello world!&#39;;
echo &#39;key:&#39; . $key . &#39;<br>&#39;;
echo &#39;str:&#39; . $str . &#39;<br>&#39;;
$jiami = jiami($key, $str);
echo &#39;加密:&#39; . $jiami . &#39;<br>&#39;;
file_put_contents(&#39;jiamiqian.txt&#39;, $str);
file_put_contents(&#39;jiamihou.txt&#39;, $jiami);
$jiemi = jiemi($key, $jiami);
echo &#39;解密:&#39; . $jiemi . &#39;<br>&#39;;
Copy after login

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

Related recommendations:

php Methods and simple examples of random array values

Installation Configure php-fpm to build Nginx PHP production environment

phpString replacement, splitting and connection methods graphic and text Detailed explanation

The above is the detailed content of Method and example analysis of implementing DES encryption and decryption in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template