Home >Backend Development >PHP Tutorial >PHP simple method to implement DES encryption and decryption

PHP simple method to implement DES encryption and decryption

高洛峰
高洛峰Original
2016-12-30 14:05:051937browse

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

des encryption:

function des_encrypt($str, $key) {
  $block = mcrypt_get_block_size('des', 'ecb');
  $pad = $block - (strlen($str) % $block);
  $str .= str_repeat(chr($pad), $pad);
  return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
}

des decryption:

function des_decrypt($str, $key) {
  $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
  $len = strlen($str);
  $block = mcrypt_get_block_size('des', 'ecb');
  $pad = ord($str[$len - 1]);
  return substr($str, 0, $len - $pad);
}

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

For more articles on how to simply implement DES encryption and decryption in PHP, please pay attention to the PHP Chinese website!

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