Home  >  Article  >  Backend Development  >  PHP uses DES for encryption and decryption

PHP uses DES for encryption and decryption

WBOY
WBOYOriginal
2016-07-25 08:43:18829browse

There is an extension in php that can support the DES encryption algorithm, it is: extension=php_mcrypt.dll

Turning on this extension in the configuration file cannot be used in the Windows environment

You need to copy libmcrypt.dll in the PHP folder to the system32 directory of the system. You can check mcrypt through phpinfo to indicate that this module can be tried normally.

The following is an example of using DES encryption and decryption in PHP:

  1. //$input - stuff to decrypt
  2. //$key - the secret key to use
  3. function do_mencrypt($input, $key)
  4. {
  5. $input = str_replace(""n", "", $input);
  6. $input = str_replace(""t", "", $input);
  7. $input = str_replace(""r", "", $input);
  8. $key = substr(md5($key), 0, 24);
  9. $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  10. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size( $td), MCRYPT_RAND);
  11. mcrypt_generic_init($td, $key, $iv);
  12. $encrypted_data = mcrypt_generic($td, $input);
  13. mcrypt_generic_deinit($td);
  14. mcrypt_module_close($td);
  15. return trim (chop(base64_encode($encrypted_data)));
  16. }
  17. //$input - stuff to decrypt
  18. //$key - the secret key to use
  19. function do_mdecrypt($input, $key)
  20. {
  21. $input = str_replace(""n", "", $input);
  22. $input = str_replace(""t", "", $input);
  23. $input = str_replace(""r", "", $input) ;
  24. $input = trim(chop(base64_decode($input)));
  25. $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  26. $key = substr(md5($key), 0, 24);
  27. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  28. mcrypt_generic_init($td, $key, $iv);
  29. $decrypted_data = mdecrypt_generic($td, $input);
  30. mcrypt_generic_deinit($ td);
  31. mcrypt_module_close($td);
  32. return trim(chop($decrypted_data));
  33. }
Copy code

PHP, DES


Statement:
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