登录  /  注册

php写的AES加密解密类分享_php实例

php中文网
发布: 2016-05-17 08:41:23
原创
907人浏览过

今天写了一个php的AES加密类。适用于Yii的扩展。
如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的默认key就可以了。
类代码:

<&#63;php
/**
 * php AES加解密类
 * 如果要与java共用,则密钥长度应该为16位长度
 * 因为java只支持128位加密,所以php也用128位加密,可以与java互转。
 * 同时AES的标准也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。
 * java 要使用AES/CBC/NoPadding标准来加解密
 * 
 * @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 &#63; 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 &#63; 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') &#63; 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') &#63; mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
	}
}
登录后复制

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号