Home >Backend Development >PHP Tutorial >An example of php DES encryption and decryption code

An example of php DES encryption and decryption code

WBOY
WBOYOriginal
2016-07-25 09:00:01911browse
  1. /**

  2. * php des encryption and decryption
  3. * by http://bbs.it-home.org
  4. */
  5. function do_mencrypt($input, $key)
  6. {
  7. $input = str_replace(""n", "", $input);
  8. $input = str_replace(""t", "", $input);
  9. $input = str_replace(""r", "", $input);
  10. $key = substr(md5($key), 0, 24);
  11. $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  12. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  13. mcrypt_generic_init($td, $key, $iv);
  14. $encrypted_data = mcrypt_generic($td, $input);
  15. mcrypt_generic_deinit($td);
  16. mcrypt_module_close($td);
  17. return trim(chop(base64_encode($encrypted_data)));
  18. }

  19. //$input - stuff to decrypt

  20. //$key - the secret key to use
  21. function do_mdecrypt($input, $key)
  22. {
  23. $input = str_replace(""n", "", $input);
  24. $input = str_replace(""t", "", $input);
  25. $input = str_replace(""r", "", $input);
  26. $input = trim(chop(base64_decode($input)));
  27. $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  28. $key = substr(md5($key), 0, 24);
  29. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  30. mcrypt_generic_init($td, $key, $iv);
  31. $decrypted_data = mdecrypt_generic($td, $input);
  32. mcrypt_generic_deinit($td);
  33. mcrypt_module_close($td);
  34. return trim(chop($decrypted_data));
  35. }
  36. ?>

复制代码

您可能感兴趣的文章: 关于des加密与解密实现方法(php net两个版本) php使用3des加密的代码(兼容.net)



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