php寫(xiě)的AES加密解密類分享
今天寫(xiě)了一個(gè)php的AES加密類。適用于Yii的擴(kuò)展。
如果不用在Yii框架中,把代碼中Yii::app()->params['encryptKey'] 換成你對(duì)應(yīng)的默認(rèn)key就可以了。
類代碼:
<?php /** * php AES加解密類 * 如果要與java共用,則密鑰長(zhǎng)度應(yīng)該為16位長(zhǎng)度 * 因?yàn)閖ava只支持128位加密,所以php也用128位加密,可以與java互轉(zhuǎn)。 * 同時(shí)AES的標(biāo)準(zhǔn)也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。 * java 要使用AES/CBC/NoPadding標(biāo)準(zhǔn)來(lái)加解密 * * @author Terry * */ class PhpAes { /** * This was AES-128 / CBC / NoPadding encrypted. * return base64_encode string * @author Terry * @param string $plaintext * @param string $key */ public static function AesEncrypt($plaintext,$key = null) { $plaintext = trim($plaintext); if ($plaintext == '') return ''; if(!extension_loaded('mcrypt')) throw new CException(Yii::t('yii','AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.')); $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $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)); /* 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 7.1中AES加解密方法mcrypt_module_open()的替換方案
- 升級(jí) PHP7.1 后 openssl 解密 mcrypt AES 數(shù)據(jù)不兼容問(wèn)題的處理方法
- PHP實(shí)現(xiàn)的AES雙向加密解密功能示例【128位】
- PHP實(shí)現(xiàn)的AES加密、解密封裝類與用法示例
- PHP7.1實(shí)現(xiàn)的AES與RSA加密操作示例
- PHP對(duì)稱加密算法(DES/AES)類的實(shí)現(xiàn)代碼
- PHP實(shí)現(xiàn)的簡(jiǎn)單AES加密解密算法實(shí)例
- PHP的AES加密算法完整實(shí)例
- php中AES加密解密的例子小結(jié)
- PHP擴(kuò)展mcrypt實(shí)現(xiàn)的AES加密功能示例
相關(guān)文章
php實(shí)現(xiàn)微信公眾號(hào)企業(yè)轉(zhuǎn)賬功能
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)微信公眾號(hào)企業(yè)轉(zhuǎn)賬功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Yii2實(shí)現(xiàn)讓關(guān)聯(lián)字段支持搜索功能的方法
這篇文章主要介紹了Yii2實(shí)現(xiàn)讓關(guān)聯(lián)字段支持搜索功能的方法,結(jié)合實(shí)例形式分析了Yii2關(guān)聯(lián)字段搜索功能的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08PHP中使用substr()截取字符串出現(xiàn)中文亂碼問(wèn)題該怎么辦
本文給大家介紹使用php substr()截取字符串出現(xiàn)亂碼問(wèn)題該怎么辦,涉及到php substr()方法的一些知識(shí)點(diǎn),感興趣的朋友一起學(xué)習(xí)下吧2015-10-10php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法
這篇文章主要介紹了php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Laravel實(shí)現(xiàn)通過(guò)blade模板引擎渲染視圖
今天小編就為大家分享一篇Laravel實(shí)現(xiàn)通過(guò)blade模板引擎渲染視圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10Laravel 自動(dòng)轉(zhuǎn)換長(zhǎng)整型雪花 ID 為字符串的實(shí)現(xiàn)
這篇文章主要介紹了Laravel 自動(dòng)轉(zhuǎn)換長(zhǎng)整型雪花 ID 為字符串的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Discuz論壇標(biāo)題和底部去掉版權(quán)信息實(shí)例講解
這篇文章主要介紹了Discuz論壇標(biāo)題和底部去掉版權(quán)信息實(shí)例講解,有正好遇到這個(gè)問(wèn)題但是不知道修改哪個(gè)文件的同學(xué)可以跟著操作下,可以節(jié)省很多不必要的時(shí)間2021-03-03yii的入口文件index.php中為什么會(huì)有這兩句
這篇文章主要介紹了yii的入口文件index.php中為什么會(huì)有這兩句 的相關(guān)資料,需要的朋友可以參考下2016-08-08php結(jié)合js實(shí)現(xiàn)點(diǎn)擊超鏈接執(zhí)行刪除確認(rèn)操作
本文是一篇超級(jí)簡(jiǎn)單的php結(jié)合js實(shí)現(xiàn)點(diǎn)擊超鏈接執(zhí)行js代碼,并確認(rèn)是否刪除數(shù)據(jù)庫(kù)數(shù)據(jù),附上全部源代碼,給需要的朋友參考下吧2014-10-10