找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 803|回复: 0
打印 上一主题 下一主题

php写的AES加密解密类分享

[复制链接]

2617

主题

2617

帖子

7789

积分

论坛元老

Rank: 8Rank: 8

积分
7789
跳转到指定楼层
楼主
发表于 2018-2-14 05:57:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            今天写了一个php的AES加密类。适用于Yii的扩展。
如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的默认key就可以了。
类代码:
params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
                /* Create the IV and determine the keysize length, use MCRYPT_RAND
                 * on Windows instead */
                $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
                /* Intialize encryption */
                mcrypt_generic_init($module, $key, $iv);
                /* Encrypt data */
                $encrypted = mcrypt_generic($module, $plaintext);
                /* Terminate encryption handler */
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
                return base64_encode($encrypted);
        }
        /**
         * This was AES-128 / CBC / NoPadding decrypted.
         * @author Terry
         * @param string $encrypted                base64_encode encrypted string
         * @param string $key
         * @throws CException
         * @return string
         */
        public static function AesDecrypt($encrypted, $key = null)
        {
                if ($encrypted == '') return '';
                if(!extension_loaded('mcrypt'))
                        throw new CException(Yii::t('yii','AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
                $ciphertext_dec = base64_decode($encrypted);
                $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
                $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
                $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
                /* Initialize encryption module for decryption */
                mcrypt_generic_init($module, $key, $iv);
                /* Decrypt encrypted string */
                $decrypted = mdecrypt_generic($module, $ciphertext_dec);
                /* Terminate decryption handle and close module */
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
                return rtrim($decrypted,"\0");
        }
        /**
         * Returns the length of the given string.
         * If available uses the multibyte string function mb_strlen.
         * @param string $string the string being measured for length
         * @return integer the length of the string
         */
        private static function strlen($string)
        {
                return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
        }
        /**
         * Returns the portion of string specified by the start and length parameters.
         * If available uses the multibyte string function mb_substr
         * @param string $string the input string. Must be one character or longer.
         * @param integer $start the starting position
         * @param integer $length the desired portion length
         * @return string the extracted part of string, or FALSE on failure or an empty string.
         */
        private static function substr($string,$start,$length)
        {
                return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
        }
}
            
            
您可能感兴趣的文章:
  • php实现aes加密类分享
  • php中AES加密解密的例子小结
  • PHP实现AES256加密算法实例
  • PHP aes (ecb)解密后乱码问题
  • PHP的AES加密算法完整实例
  • PHP 7.1中AES加解密方法mcrypt_module_open()的替换方案
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端