Home > Backend Development > PHP Tutorial > DES encryption and decryption example code implemented by PHP, phpdes encryption and decryption example_PHP tutorial

DES encryption and decryption example code implemented by PHP, phpdes encryption and decryption example_PHP tutorial

WBOY
Release: 2016-07-12 08:54:18
Original
1033 people have browsed it

DES encryption and decryption example code implemented by PHP, phpdes encryption and decryption example

The example in this article describes the DES encryption and decryption example implemented by PHP. Share it with everyone for your reference, the details are as follows:

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

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP Encryption Methods", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Encryption and decryption method implemented by PHP combined with md5
  • PHP encryption and decryption example analysis
  • SSL encryption and decryption under PHP, Verification and signature method (very simple)
  • PHP encapsulated string encryption and decryption function
  • thinkphp WeChat development (message encryption and decryption)
  • Detailed explanation of PHP encryption and decryption function
  • PHP implements enhanced encryption and decryption class examples
  • How to use PHP rsa encryption and decryption
  • PHP encryption and decryption string summary
  • PHP encryption and decryption class example analysis
  • Repair code of the classic PHP encryption and decryption function Authcode()
  • Detailed explanation of the use of php rsa encryption and decryption
  • Examples of base64_decode and base64_encode encryption and decryption functions in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1120004.htmlTechArticleDES encryption and decryption example code implemented by PHP, phpdes encryption and decryption example This article describes the DES encryption and decryption example implemented by PHP . Share it with everyone for your reference, the details are as follows: php$key...
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