今天沒事與了一個Php Aes加密類程序,適用于Yii的擴展如果不用在Yii框架中,把代碼中Yii::app()->params[/'encryptKey/'] 換成你對應的默認key就可以了.
AES加密算法 – 算法原理
AES 算法基于排列和置換運算,排列是對數據重新進行安排,置換是將一個數據單元替換為另一個,AES 使用幾種不同的方法來執行排列和置換運算.
AES 是一個迭代的、對稱密鑰分組的密碼,它可以使用128、192 和 256 位密鑰,并且用 128 位(16字節)分組加密和解密數據,與公共密鑰密碼使用密鑰對不同,對稱密鑰密碼使用相同的密鑰加密和解密數據,通過分組密碼返回的加密數據的位數與輸入數據相同,迭代加密使用一個循環結構,在該循環中重復置換和替換輸入數據,代碼如下:
- <?php
- /**
- * php AES加解密類
- * 因為java只支持128位加密,所以php也用128位加密,可以與java互轉。
- * 同時AES的標準也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。
- *
- * @author Terry
- *
- */
- class PhpAes
- {
- /**
- * This was AES-128 / CBC / ZeroBytePadding encrypted.
- * return base64_encode string
- * @author Terry
- * @param string $plaintext
- * @param string $key
- */
- public static function AesEncrypt($plaintext,$key = null)
- {
- 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);
- $plaintext = self::PKCS5Padding($plaintext, $size);
- $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(trim($encrypted));
- }
- /**
- * This was AES-128 / CBC / ZeroBytePadding 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 self::UnPKCS5Padding($decrypted);
- }
- private static function strlen($string)
- {
- return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
- }
- private static function substr($string,$start,$length)
- {
- return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
- }
- private static function PKCS5Padding ($text, $blocksize) {
- $pad = $blocksize - (self::strlen($text) % $blocksize);
- return $text . str_repeat(chr($pad), $pad);
- }
- private static function UnPKCS5Padding($text)
- {
- $pad = ord($text{self::strlen($text)-1});
- if ($pad > self::strlen($text)) return false;
- if (strspn($text, chr($pad), self::strlen($text) - $pad) != $pad) return false;
- return substr($text, 0, -1 * $pad);
- }
- }
- ?>
使用方法,代碼如下:
- <?php
- require_once('./AES.php');
- //$aes = new AES();
- $aes = new AES(true);// 把加密后的字符串按十六進制進行存儲
- //$aes = new AES(true,true);// 帶有調試信息且加密字符串按十六進制存儲
- $key = "this is a 32 byte key";// 密鑰
- $keys = $aes->makeKey($key);
- $encode = "123456";// 被加密的字符串
- $ct = $aes->encryptString($encode, $keys);
- echo "encode = ".$ct."<br>";//開源代碼Vevb.com
- $cpt = $aes->decryptString($ct, $keys);
- echo "decode = ".$cpt;
- ?>
新聞熱點
疑難解答