PHP for switching between Bin2Hex and Hex2bin via AES encryption/decryption

Source: Internet
Author: User
This article mainly for you to share a PHP, such as through the AES encryption/decryption to achieve the switch between Bin2Hex and Hex2bin, has a very good reference value, I hope to help you. Follow the small series together to see it.

<?php/** * Request Data via AES Encryption * * @param array $query * @return String *                    /function Aesencryptrequest ($encryptKey, $query) {return $this->encrypt_pass ($query, $encryptKey); }//Cryptographic function Encrypt_pass ($input, $key) {$size = Mcrypt_get_block_size (MC             rypt_rijndael_128, MCRYPT_MODE_ECB);             $input = Pkcs5_pad ($input, $size);            $TD = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_ECB, ');            $iv = ' 0102030405060708 ';            Mcrypt_generic_init ($TD, $key, $IV);            $data = Mcrypt_generic ($TD, $input);            Mcrypt_generic_deinit ($TD);             Mcrypt_module_close ($TD);            $data = Bin2Hex ($data);        return $data;             }//Fill function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text.        Str_repeat (Chr ($pad), $pad);    }        /** * Decrypt request Data via AES * * @param array $query * @return String */func                    tion Aesdecryptresponse ($encryptKey, $data) {return $this->decrypt_pass ($data, $encryptKey);            }//Decryption function Decrypt_pass ($SSTR, $sKey) {$iv = ' 0102030405060708 ';                $decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $sKey, Hex2bin ($SSTR),            MCRYPT_MODE_ECB, $IV);            $dec _s = strlen ($decrypted);            $padding = Ord ($decrypted [$dec _s-1]);            $decrypted = substr ($decrypted, 0,-$padding);        return $decrypted; }

Related recommendations:

The bin file and hex file are transferred to each other

Hex file to bin file C language implementation

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.