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