PHPDES encryption and decryption

Source: Internet
Author: User
Tags crypt string back
PHPDES encryption and decryption this is a piece of PHP code for DES decryption.

Refer to routines in the self-http://php.net/manual/zh/function.mcrypt-module-open.php. There is no difficulty.

However, after I decrypt the data, I try again. The following are invisible garbled characters.

? ?] Y )? Aw #? Y ???] M? /M? 2 ???] C ?? F ?? V (? I ?????~????? ? E = I "C ??????


It took most of the time to find out that it was because after the other party encrypted the binary ciphertext and sent it to me in hexadecimal format, for example, 13BD5122F55E706D13FB7D0F349A787D19E9D2334E268A15.

As you can see, this cannot be used as the ciphertext itself. I cannot decrypt this hexadecimal string only because it is directly decrypted. First, you need to pass the hexadecimal string back to the binary format, and then decrypt it with the decryption method to see the plaintext.

The hexadecimal to binary method is hex2bin.

Cryptare is used for encryption and decryption. The first parameter is plaintext or ciphertext, the second parameter is key, the third parameter is 0, and the third parameter is encryption.

Note: $ text = $ this-> hex2bin ($ text); may not be used during encryption.


    function hex2bin($hexData)     {        $binData = "";        for($i = 0; $i < strlen ( $hexData ); $i += 2) {         $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );        }               return $binData;    }               function cryptare($text, $key, $crypt)     {         $text = $this->hex2bin($text);        $encrypted_data="";         $td = mcrypt_module_open('des', '', 'ecb', '');                         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);         mcrypt_generic_init($td, $key, $iv);         if($crypt)         {               $encrypted_data = mcrypt_generic($td, $text);         print $encrypted_data ;        }               else            {               $encrypted_data = mdecrypt_generic($td, $text);         }               mcrypt_generic_deinit($td);         mcrypt_module_close($td);         return $encrypted_data;     }

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.