First, Introduction
DES is a common type of symmetric encryption, all called Data Encryption Standard, which is the encryption standards, is a key encryption using the block algorithm. The key length is 64 bits (bit), and the key that exceeds the number of digits is ignored. Symmetric encryption is the same as encryption and decryption key, symmetric encryption will generally follow a fixed length, the string to be encrypted into chunks, less than a whole block or just the last special padding characters.
Cross-language do DES encryption and decryption often problems, often is the wrong fill, encoding inconsistent or encryption and decryption mode does not correspond to the cause. Common fill modes are: PKCS5, PKCS7, iso10126, ansix923, zero. Encryption modes are: DES-ECB, DES-CBC, des-ctr, DES-OFB, DES-CFB.
As a software developer, a tool can be used to test DES encryption and decryption, and an online tool is recommended: Http://tool.chacuo.net/cryptdes
Second, the realization
PHP provides Mcrypt series functions to implement DES encryption and decryption, but the functions in the extension are discarded, since PHP 7.2.0, will be moved to PECL.
So this code uses a more general OPENSSL way to implement DES encryption and decryption, the implementation and use of the code as follows:
<?php/*** OpenSSL implemented DES encryption class, support various PHP versions */class DES{/** * @var string $method encryption and decryption methods, which can be openssl_get_cipher_methods () Get */ protected $method; /** * @var string $key encryption key for decryption */ protected $key; /** * @var string $output output Format None, base64, Hex */ protected $output; /** * @var string $iv vector of added decryption */ protected $iv; /** * @var string $options */ protected $options; //type of output Const Output_null="'; Const output_base64=' base64 '; Const Output_hex=' Hex '; /*** DES constructor. * @param string$key * @param string$method* ECB DES-ECB, DES-EDE3 (for ECB mode, $iv is empty)* CBC DES-CBC, DES-EDE3-CBC, DESX-CBC* CFB des-cfb8, Des-ede3-cfb8* CTR* OFB * * @param string$output* Base64, Hex * * @param string$iv * @param int$options */ Public function __construct($key, $method=' DES-ECB ', $output="', $iv="', $options=Openssl_raw_data|openssl_no_padding){$this-Key=$key; $this->method =$method; $this->output =$output; $this->iv =$iv; $this->options =$options;}/*** Encryption * * @param $str * @returnstring */ Public functionEncrypt($str){$str=$this->pkcspadding($str, 8); $sign= Openssl_encrypt($str, $this->method, $this-Key, $this->options, $this->iv); if ($this->output = = Self::output_base64){$sign=Base64_encode($sign);}Else if ($this->output = = Self::Output_hex){$sign=Bin2Hex($sign);}return $sign;}/*** Decryption * * @param $encrypted * @returnstring */ Public functionDecrypt($encrypted){if ($this->output = = Self::output_base64){$encrypted=Base64_decode($encrypted);}Else if ($this->output = = Self::Output_hex){$encrypted= Hex2bin($encrypted);}$sign=@Openssl_decrypt($encrypted, $this->method, $this-Key, $this->options, $this->iv); $sign=$this->unpkcspadding($sign); $sign=RTrim($sign); return $sign;}/*** Fill * * @param $str * @param $blocksize * @returnstring */ Private functionPkcspadding($str, $blocksize){$pad=$blocksize-(strlen($str)%$blocksize); return $str.str_repeat(CHR($pad), $pad);}/*** to fill * * @param $str * @returnstring */ Private functionUnpkcspadding($str){$pad=Ord($str{strlen($str)-1}); if ($pad>strlen($str)){return false;}return substr($str, 0,-1*$pad);}}$key=' key123456 ';$iv=' iv123456 ';//DES CBC plus decryption$des=New DES($key, ' DES-CBC ', DES::output_base64, $iv);Echo $base 64Sign=$des->encrypt(' Hello DES CBC ');Echo "\ n";Echo $des->decrypt($base 64Sign);Echo "\ n";//DES ECB plus decryption$des=New DES($key, ' DES-ECB ', DES::Output_hex);Echo $base 64Sign=$des->encrypt(' Hello DES ECB ');Echo "\ n";Echo $des->decrypt($base 64Sign);
Third, RELATED LINKS
- DES Plus decryption tool: Http://tool.chacuo.net/cryptdes
- Mcrypt Official Document: http://php.net/manual/zh/book.mcrypt.php
- The official documentation for the OPENSSL plus decryption function:
- openssl_encrypt:http://php.net/manual/zh/function.openssl-encrypt.php
- openssl_decrypt:http://php.net/manual/zh/function.openssl-decrypt.php
This article started in Mayanlong personal blog, Welcome to share, reproduced please indicate the source.
Mayanlong Personal Blog: http://www.mayanlong.com
Mayanlong personal Weibo: Http://weibo.com/imayanlong
Mayanlong GitHub Home: Https://github.com/yanlongma
PHP Basics-DES plus decryption in PHP