• php的AES加密、解密类


    <?php
    /**
     * php、ios、Android 通用的AES加密、解密方法
     */
    namespace CommonBusiness;
    
    
    class AESCrypt
    {
        /**
         * 初始矢量
         * @var string
         */
        # converted JAVA byte code in to HEX and placed it here
        //private $hex_iv = '00000000000000000000000000000000';
        private $key = '';
    
        function __construct($key) {
            $this->key = $key;
            $this->key = hash('sha256', $this->key, true);
        }
    
        /**
         * 设置密钥
         * @param $new_key
         */
        function set_key($new_key)
        {
            $this->key = $new_key;
        }
    
        /**
         * 加密
         * @param $str
         * @return string
         */
        function encrypt($str) {
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
            mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
            $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
            $pad = $block - (strlen($str) % $block);
            $str .= str_repeat(chr($pad), $pad);
            $encrypted = mcrypt_generic($td, $str);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return base64_encode($encrypted);
        }
    
        /**
         * 解密
         * @param $code
         * @return bool|string
         */
        function decrypt($code) {
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
            mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
            $str = mdecrypt_generic($td, base64_decode($code));
            $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return $this->strippadding($str);
        }
    
        /*
          For PKCS7 padding
         */
        private function addpadding($string, $blocksize = 16) {
            $len = strlen($string);
            $pad = $blocksize - ($len % $blocksize);
            $string .= str_repeat(chr($pad), $pad);
            return $string;
        }
    
        private function strippadding($string) {
            $slast = ord(substr($string, -1));
            $slastc = chr($slast);
            $pcheck = substr($string, -$slast);
            if (preg_match("/$slastc{" . $slast . "}/", $string)) {
                $string = substr($string, 0, strlen($string) - $slast);
                return $string;
            } else {
                return false;
            }
        }
    
        function hexToStr($hex)
        {
            $string='';
            for ($i=0; $i < strlen($hex)-1; $i+=2)
            {
                $string .= chr(hexdec($hex[$i].$hex[$i+1]));
            }
            return $string;
        }
    }
    
     
    ?>
    

      

    /**
     * AES
     */
    class AES
    {
    
        /**
         * 注:不要修改这几个值,有不同需求请以参数形式传入!
         */
        const METHOD = 'AES-128-CBC';
        const KEY = 'zbtcyuc5msxidyam';
        const IV = 'bthuiwldicoayzbt';
    
        /**
         * 加密
         *
         * @param string $data
         * @param string $method
         * @param string $key
         * @param int $options
         * @param string $iv
         * @return string
         */
        public static function encrypt($data = '', $method = self::METHOD, $key = self::KEY, $options = 0, $iv = self::IV)
        {
            $encrypted = openssl_encrypt($data, $method, $key, $options, $iv);
            return $encrypted;
        }
    
        /**
         * 解密
         *
         * @param string $data
         * @param string $method
         * @param string $key
         * @param int $options
         * @param string $iv
         * @return string
         */
        public static function decrypt($data = '', $method = self::METHOD, $key = self::KEY, $options = 0, $iv = self::IV)
        {
            $decrypted = openssl_decrypt($data, $method, $key, $options, $iv);
            return $decrypted;
        }
    
    }
    

      RSA2加、解密

    //加密
    $signContent = $this->getSignContent($params);
                    openssl_private_encrypt($signContent, $encrypted, $this->getPrivateKey());//私钥加密
                    $sign = base64_encode($encrypted);  //base64只是为了避免特殊字符
    
    
    //解密
    $signContent = $this->getSignContent($params);
                    $publicKey = openssl_get_publickey($this->getPublicKey()); //转换为openssl格式密钥
                    $result = (bool)openssl_verify($params, base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256); //调用openssl内置方法验签,返回bool值
    

      

  • 相关阅读:
    有价值的终会被人发现和承认
    Sql中存在斜杠“/”怎么办?
    JSONObject和JSONArray(json-lib-2.4)的基本用法
    如何设定linux系统时间
    动态创建的文本框想要加上jQuery的datepicker功能变成日期选择控件该怎么办?
    如果$.ajax函数迟迟得不到响应,那么最有可能出错的地方是请求参数写错了
    Notepad2替代系统自带的记事本Notepad
    css属性与js中style对象的属性对应表
    day02_js学习笔记_01_js的简介、js的基本语法
    Eclipse/MyEclipse的快捷键以及文档注释、多行注释的快捷键
  • 原文地址:https://www.cnblogs.com/diguaer/p/7209728.html
Copyright © 2020-2023  润新知