php如通过AES加密/解密实现bin2hex和hex2bin之间的切换

零到壹度
零到壹度原创
2023-03-22 16:14:012067浏览

本文主要为大家分享一篇php如通过AES加密/解密实现bin2hex和hex2bin之间的切换的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。

<?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, '', 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;
        }
        //填充
        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 = '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;
        }

相关推荐:

bin文件和hex文件互转

hex文件转换为bin文件C语言实现

以上就是php如通过AES加密/解密实现bin2hex和hex2bin之间的切换 的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。