Definition and Usage Example of DES Encryption and Decryption Class Implemented by PHP

  • 2021-10-27 06:40:09
  • OfStack

This paper describes the definition and usage of DES encryption and decryption class implemented by PHP. Share it for your reference, as follows:

Today, when writing App interface, we need to transfer encrypted data to APP, so we wrote the following DES encryption class. The correct code is as follows


class CryptDes {
 function __construct(){
  $this->key = 'codelovers'; // Key 
  $this->iv = '15548632'; // Offset 
 }
 /*
  *  Encryption 
  */
 function encrypt($input){
  $size = mcrypt_get_block_size(MCRYPT_DES,MCRYPT_MODE_CBC); //3DES Encryption will MCRYPT_DES Replace with MCRYPT_3DES
  $input = $this->pkcs5_pad($input, $size); // If adopted PaddingPKCS7 Please replace it with PaddingPKCS7 Method. 
  $key = str_pad($this->key,8,'0'); //3DES Encryption will 8 Replace with 24
  $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
  if( $this->iv == '' )
  {
   $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  }
  else
  {
   $iv = $this->iv;
  }
  @mcrypt_generic_init($td, $key, $iv);
  $data = mcrypt_generic($td, $input);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  $data = base64_encode($data);// If you need to convert 2 The binary system can be changed to  bin2hex  Conversion 
  return $data;
 }
 /*
  *  Decryption 
  */
 function decrypt($encrypted){
  $encrypted = base64_decode($encrypted); // If you need to convert 2 The binary system can be changed to  bin2hex  Conversion 
  $key = str_pad($this->key,8,'0'); //3DES Encryption will 8 Replace with 24
  $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,'');//3DES Encryption will MCRYPT_DES Replace with MCRYPT_3DES
  if( $this->iv == '' )
  {
   $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  }
  else
  {
   $iv = $this->iv;
  }
  $ks = mcrypt_enc_get_key_size($td);
  @mcrypt_generic_init($td, $key, $iv);
  $decrypted = mdecrypt_generic($td, $encrypted);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  $y=$this->pkcs5_unpad($decrypted);
  return $y;
 }
 function pkcs5_pad ($text, $blocksize) {
  $pad = $blocksize - (strlen($text) % $blocksize);
  return $text . str_repeat(chr($pad), $pad);
 }
 function pkcs5_unpad($text){
  $pad = ord($text{strlen($text)-1});
  if ($pad > strlen($text)) {
   return false;
  }
  if (strspn($text, chr($pad), strlen($text) - $pad) != $pad){
   return false;
  }
  return substr($text, 0, -1 * $pad);
 }
 function PaddingPKCS7($data) {
  $block_size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);//3DES Encryption will MCRYPT_DES Replace with MCRYPT_3DES
  $padding_char = $block_size - (strlen($data) % $block_size);
  $data .= str_repeat(chr($padding_char),$padding_char);
  return $data;
 }
}

The usage is as follows:


$str = 'www.ofstack.com';
$des = new CryptDes();
$mres = $des->encrypt($str); // Encryption 
echo $mres."<br/>";
$jres = $des->decrypt($mres); // Decryption 
echo $jres."<br/>";

Run results:

TxqbmhK86YWdTtTAQoDOqA==
www.ofstack.com

Appendix: php7 des Encryption and Decryption

The following contents are forwarded from: https://blog.csdn.net/qq_35979073/article/details/81449590

1. If encrypt extension is not installed in linux environment or php version greater than 7.1 does not support encrypt encryption extension

You can use the openssl encryption extension

The following encapsulated encryption and decryption methods can be called directly


/**
 * des-ecb Encryption 
 * @param string $data  Data to be encrypted 
 * @param string $key  Encryption key (64 A string of bits )
 */
function des_ecb_encrypt($data, $key){
  return openssl_encrypt ($data, 'des-ecb', $key);
}
 
/**
 * des-ecb Decryption 
 * @param string $data  Encrypted data 
 * @param string $key  Encryption key 
 */
function des_ecb_decrypt ($data, $key){
  return openssl_decrypt ($data, 'des-ecb', $key);
}

Explanation: The $key encryption key (64-bit string) is self-defined

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Online DES Encryption/Decryption Tool
http://tools.ofstack.com/password/des_encode

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

More readers interested in PHP can check the topics of this site: "Summary of php Encryption Methods", "Summary of PHP Encoding and Transcoding Operation Skills", "Summary of PHP Mathematical Operation Skills", "Complete Collection of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial of PHP Data Structure and Algorithm", "Summary of php Programming Algorithm" and "Summary of php Regular Expression Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: