Home > Backend Development > PHP Tutorial > PHP such as switching between bin2hex and hex2bin through AES encryption/decryption

PHP such as switching between bin2hex and hex2bin through AES encryption/decryption

零到壹度
Release: 2023-03-22 16:14:01
Original
2843 people have browsed it

This article mainly shares with you an article about PHP switching between bin2hex and hex2bin through AES encryption/decryption. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

<?php
     
        /**
         * 通过AES加密请求数据
         * 
         * @param array $query
         * @return string
         */
        function AESEncryptRequest($encryptKey, $query){
            return $this->encrypt_pass($query,$encryptKey);
            
        }
        // 加密
        function encrypt_pass($input, $key) {
 
            $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
 
            $input = pkcs5_pad($input, $size);
 
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_ECB, &#39;&#39;);
            $iv = &#39;0102030405060708&#39;;
            mcrypt_generic_init($td, $key, $iv);
            $data = mcrypt_generic($td, $input);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
 
            $data = bin2hex($data);
            return $data;
        }
        //填充
        function pkcs5_pad ($text, $blocksize) {
            $pad = $blocksize - (strlen($text) % $blocksize);
            return $text . str_repeat(chr($pad), $pad);
        }
    
        /**
         * 通过AES解密请求数据
         * 
         * @param array $query
         * @return string
         */
        function AESDecryptResponse($encryptKey,$data){
            return $this->decrypt_pass($data,$encryptKey);
            
        }
        // 解密
        function decrypt_pass($sStr, $sKey) {
            $iv = &#39;0102030405060708&#39;;
            $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;
        }
Copy after login

Related recommendations:

bin file and hex file conversion

hex file conversion Implement

for bin file C language

The above is the detailed content of PHP such as switching between bin2hex and hex2bin through AES encryption/decryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template