PHP des encryption and decryption

Source: Internet
Author: User
Tags crypt
This is a piece of DES decrypted PHP code.

Refer to routines from http://php.net/manual/zh/function.mcrypt-module-open.php. There's nothing difficult about it.

But after I decrypted the test repeatedly, are the following such invisible garbled.

? ?] Y)? aw#? Y?????] M?/m?2??] C?? F?? V (? I????? ~????? ? e=i "C??????


The majority of the genius found, is because the other side after the encryption, the binary cipher to convert to 16 into the system for me to pass over, such as 13bd5122f55e706d13fb7d0f349a787d19e9d2334e268a15

You see clearly, this can not be used as ciphertext itself, I just because the direct decryption of the 16 binary string so that can not be decrypted. This 16-binary string needs to be passed back to the binary, and then decrypted with the decryption method, you can see the clear text.

The hexadecimal-to-binary method is Hex2bin

Cryptare This function is encrypted decryption, the first parameter is clear text or ciphertext, the second parameter is key, the third parameter 0 means decryption, 1 is encryption.

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


    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.