_php technique of DES encryption and decryption instance code implemented by PHP

Source: Internet
Author: User

This example describes the DES encryption and decryption instance implemented by PHP. Share to everyone for your reference, specific as follows:

<?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 ' Encrypt: '. $jiami.
' <br> ';
File_put_contents (' Jiamiqian.txt ', $str);
File_put_contents (' Jiamihou.txt ', $jiami);
$jiemi = Jiemi ($key, $jiami); Echo ' Decrypt: '. $jiemi.

 ' <br> ';

More interested in PHP related content readers can view the site topics: "PHP Encryption Method Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.