Php Aes加密类程序代码分享
今天没事与了一个Php Aes加密类程序,适用于Yii的扩展如果不用在Yii框架中,把代码中Yii::app()->params[\'encryptKey\'] 换成你对应的默认key就可以了.
AES加密算法 – 算法原理
AES 算法基于排列和置换运算,排列是对数据重新进行安排,置换是将一个数据单元替换为另一个,AES 使用几种不同的方法来执行排列和置换运算.
AES 是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据,与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据,通过分组密码返回的加密数据的位数与输入数据相同,迭代加密使用一个循环结构,在该循环中重复置换和替换输入数据,代码如下:
- <?php
-
-
-
-
-
-
-
-
- class PhpAes
- {
-
-
-
-
-
-
-
- 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));
-
-
- $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
-
- mcrypt_generic_init($module, $key, $iv);
-
-
- $encrypted = mcrypt_generic($module, $plaintext);
-
-
- mcrypt_generic_deinit($module);
- mcrypt_module_close($module);
- return base64_encode(trim($encrypted));
- }
-
-
-
-
-
-
-
-
-
- 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));
-
-
- mcrypt_generic_init($module, $key, $iv);
-
-
- $decrypted = mdecrypt_generic($module, $ciphertext_dec);
-
-
- 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(true);
-
- $key = "this is a 32 byte key";
- $keys = $aes->makeKey($key);
- $encode = "123456";
- $ct = $aes->encryptString($encode, $keys);
- echo "encode = ".$ct."<br>";
- $cpt = $aes->decryptString($ct, $keys);
- echo "decode = ".$cpt;
- ?>