Original: DES encryption and decryption matching C#des encryption decryption symmetric encryption in PHP
The online search for PHP des encryption and decryption exactly matches the previous c# string encryption decryption function can be used for C # and PHP communication to encrypt data, where $key is the encryption key, $IV is the offset, The default offset is the same as the encryption key, <?phpclass des{ var $key; var $iv;//Offset function DES ($key, $iv =0) { //key length 8 For example:1234abcd $ This->key = $key; if ($iv = = 0) { $this- >iv = $key; Default to $key as iv } else { $this->iv = $iv; Mcrypt_create_iv (Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC), mcrypt_dev_random); } } function Encrypt ($str) { // Encrypt, return uppercase hexadecimal string $size = mcrypt_get_block_size (MCRYPT_DES,MCRYPT_MODE_CBC); $STR = $this->pkcs5pad ($str, $siZe); return strtoupper (Bin2Hex (MCRYPT_CBC, $this mcrypt_des, $str,->key ENCRYPT, $this->iv)); } function Decrypt ($str) { & nbsp //decryption $strBin = $this->hex2bin (strtolower ($STR)); &N BSP; $str = MCRYPT_CBC (mcrypt_des, $this->key, $strBin, Mcrypt_decrypt, $this->iv); &NBS p; $str = $this->pkcs5unpad ($str); return $str; } & nbsp; function Hex2bin ($hexData) { $binData = ""; & nbsp for ($i = 0; $i < strlen ($hexData); $i + = 2) { $binData. = Chr ( Hexdec (substr ($hexData, $i, 2)); } return $binData ; &NBSP;}&NBSP;&NBSp; function Pkcs5pad ($text, $blocksize) { $pad = $blocksize-(strlen ($text) % $blocksize); return $text. Str_repeat (Chr ($pad), $pad); } function Pkcs5unpad ($text) {& nbsp; $pad = Ord ($text {strlen ($text)-1}); if ($pad >str Len ($text)) return false; if (strspn ($text , Chr ($pad), strlen ($text)-$pad)! = $pad) return false; &N bsp; return substr ($text, 0,-1* $pad); } }?> Test $str = "Xublog"; $key = ' 12345678 '; $crypt = new DES ($key); $mstr = $crypt->encrypt ($STR); echo "[$str] Encryption: [$mstr]<br>"; $str = $crypt-& Gt;decrypt ($MSTR); echo "[$mstr] decryption: [$str]<br>";
Des encryption decryption matching C#des encryption decryption symmetric encryption in PHP