php des encryption and decryption code

怪我咯
Release: 2023-03-14 11:38:02
Original
3350 people have browsed it

DES stands for Data Encryption Standard, which is a block algorithm that uses key encryption. In 1977, it was determined as a Federal Information Processing Standard (FIPS) by the National Bureau of Standards of the U.S. federal government and authorized to be used in Africa. Used in classified government communications, the algorithm subsequently became widely circulated internationally. It should be noted that in some literature, DES as an algorithm is called Data Encryption Algorithm (DEA), which has been distinguished from DES as a standard.

This article mainly introduces the DES encryption and decryption method implemented by PHP, and analyzes the related techniques of DES encryption and decryption by PHP in the form of a complete example. Friends in need can refer to , 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

The above is the detailed content of php des encryption and decryption code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!