Des encryption and decryption matching c#des encryption and decryption symmetric encryption in PHP

Source: Internet
Author: User
Tags crypt decrypt

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.