php写的AES加密解密类分享
今天写了一个php的AES加密类。适用于Yii的扩展。
如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey']换成你对应的默认key就可以了。
类代码:
<?php
/**
*phpAES加解密类
*如果要与java共用,则密钥长度应该为16位长度
*因为java只支持128位加密,所以php也用128位加密,可以与java互转。
*同时AES的标准也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。
*java要使用AES/CBC/NoPadding标准来加解密
*
*@authorTerry
*
*/
classPhpAes
{
/**
*ThiswasAES-128/CBC/NoPaddingencrypted.
*returnbase64_encodestring
*@authorTerry
*@paramstring$plaintext
*@paramstring$key
*/
publicstaticfunctionAesEncrypt($plaintext,$key=null)
{
$plaintext=trim($plaintext);
if($plaintext=='')return'';
if(!extension_loaded('mcrypt'))
thrownewCException(Yii::t('yii','AesEncryptrequiresPHPmcryptextensiontobeloadedinordertousedataencryptionfeature.'));
$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));
/*CreatetheIVanddeterminethekeysizelength,useMCRYPT_RAND
*onWindowsinstead*/
$iv=substr(md5($key),0,mcrypt_enc_get_iv_size($module));
/*Intializeencryption*/
mcrypt_generic_init($module,$key,$iv);
/*Encryptdata*/
$encrypted=mcrypt_generic($module,$plaintext);
/*Terminateencryptionhandler*/
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
returnbase64_encode($encrypted);
}
/**
*ThiswasAES-128/CBC/NoPaddingdecrypted.
*@authorTerry
*@paramstring$encrypted base64_encodeencryptedstring
*@paramstring$key
*@throwsCException
*@returnstring
*/
publicstaticfunctionAesDecrypt($encrypted,$key=null)
{
if($encrypted=='')return'';
if(!extension_loaded('mcrypt'))
thrownewCException(Yii::t('yii','AesDecryptrequiresPHPmcryptextensiontobeloadedinordertousedataencryptionfeature.'));
$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));
/*Initializeencryptionmodulefordecryption*/
mcrypt_generic_init($module,$key,$iv);
/*Decryptencryptedstring*/
$decrypted=mdecrypt_generic($module,$ciphertext_dec);
/*Terminatedecryptionhandleandclosemodule*/
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
returnrtrim($decrypted,"\0");
}
/**
*Returnsthelengthofthegivenstring.
*Ifavailableusesthemultibytestringfunctionmb_strlen.
*@paramstring$stringthestringbeingmeasuredforlength
*@returnintegerthelengthofthestring
*/
privatestaticfunctionstrlen($string)
{
returnextension_loaded('mbstring')?mb_strlen($string,'8bit'):strlen($string);
}
/**
*Returnstheportionofstringspecifiedbythestartandlengthparameters.
*Ifavailableusesthemultibytestringfunctionmb_substr
*@paramstring$stringtheinputstring.Mustbeonecharacterorlonger.
*@paraminteger$startthestartingposition
*@paraminteger$lengththedesiredportionlength
*@returnstringtheextractedpartofstring,orFALSEonfailureoranemptystring.
*/
privatestaticfunctionsubstr($string,$start,$length)
{
returnextension_loaded('mbstring')?mb_substr($string,$start,$length,'8bit'):substr($string,$start,$length);
}
}