最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php寫的AES加密解密類分享

 更新時(shí)間:2014年06月20日 11:05:36   投稿:junjie  
這篇文章主要介紹了php寫的AES加密解密類,實(shí)際是為YII框架寫的,不在YII框架時(shí)只需替換其中的兩句代碼即可使用,需要的朋友可以參考下

今天寫了一個(gè)php的AES加密類。適用于Yii的擴(kuò)展。
如果不用在Yii框架中,把代碼中Yii::app()->params['encryptKey'] 換成你對應(yīng)的默認(rèn)key就可以了。
類代碼:

<?php
/**
 * php AES加解密類
 * 如果要與java共用,則密鑰長度應(yīng)該為16位長度
 * 因?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)來加解密
 * 
 * @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);
	}
}

相關(guān)文章

最新評論

三穗县| 陇川县| 托克托县| 临邑县| 木兰县| 田东县| 贡山| 青铜峡市| 三门县| 竹溪县| 新兴县| 嘉黎县| 长宁区| 固镇县| 固镇县| 永州市| 普兰店市| 喀喇沁旗| 十堰市| 南通市| 孝感市| 桐乡市| 城固县| 白山市| 定南县| 双流县| 上蔡县| 厦门市| 右玉县| 栾城县| 哈巴河县| 嘉兴市| 江安县| 万载县| 霍山县| 涟水县| 陕西省| 上林县| 左云县| 蒲城县| 申扎县|