php写的AES加密解密类分享


Posted in PHP onJune 20, 2014

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

<?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 ? 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);
	}
}
PHP 相关文章推荐
IIS下配置Php+Mysql+zend的图文教程
Dec 08 PHP
php与XML、XSLT、Mysql的结合运用实现代码
Nov 19 PHP
PHP similar_text 字符串的相似性比较函数
May 26 PHP
php实现上传图片保存到数据库的方法
Feb 11 PHP
php数组键名技巧小结
Feb 17 PHP
PHP时间类完整实例(非常实用)
Dec 25 PHP
PHP获取当前文件的父目录方法汇总
Jul 21 PHP
PHP使用星号替代用户名手机和邮箱的实现代码
Feb 07 PHP
php layui实现前端多图上传实例
Jul 30 PHP
thinkPHP5.1框架使用SemanticUI实现分页功能示例
Aug 03 PHP
thinkPHP3.2使用RBAC实现权限管理的实现
Aug 27 PHP
PHP读取XML文件的方法实例总结【DOMDocument及simplexml方法】
Sep 10 PHP
PHP提交表单失败后如何保留已经填写的信息
Jun 20 #PHP
将酷狗krc歌词解析并转换为lrc歌词php源码
Jun 20 #PHP
Yii Framework框架获取分类下面的所有子类方法
Jun 20 #PHP
windows下配置apache+php+mysql时出现问题的处理方法
Jun 20 #PHP
PHP扩展CURL的用法详解
Jun 20 #PHP
教你如何解密 “ PHP 神盾解密工具 ”
Jun 20 #PHP
ThinkPHP3.1查询语言详解
Jun 19 #PHP
You might like
PHP求最大子序列和的算法实现
2011/06/24 PHP
解析关于wamp启动是80端口被占用的问题
2013/06/21 PHP
php导出csv格式数据并将数字转换成文本的思路以及代码分享
2014/06/05 PHP
PHP获取二叉树镜像的方法
2018/01/17 PHP
PHP PDOStatement::getAttribute讲解
2019/02/01 PHP
php设计模式之职责链模式定义与用法经典示例
2019/09/19 PHP
多选列表框动态添加,移动,删除,全选等操作的简单实例
2014/01/13 Javascript
浏览器环境下JavaScript脚本加载与执行探析之动态脚本与Ajax脚本注入
2016/01/19 Javascript
学习vue.js计算属性
2016/12/03 Javascript
js点击任意区域弹出层消失实现代码
2016/12/27 Javascript
详解jQuery中关于Ajax的几个常用的函数
2017/07/17 jQuery
nodejs使用http模块发送get与post请求的方法示例
2018/01/08 NodeJs
JavaScript图片旋转效果实现方法详解
2020/06/28 Javascript
[54:41]2018DOTA2亚洲邀请赛3月30日 小组赛B组 VGJ.T VS paiN
2018/03/31 DOTA
Django集成百度富文本编辑器uEditor攻略
2014/07/04 Python
python中的reduce内建函数使用方法指南
2014/08/31 Python
Python常用知识点汇总
2016/05/08 Python
git进行版本控制心得详谈
2017/12/10 Python
pyqt5 实现多窗口跳转的方法
2019/06/19 Python
基于Python安装pyecharts所遇的问题及解决方法
2019/08/12 Python
Django对models里的objects的使用详解
2019/08/17 Python
TensorFlow加载模型时出错的解决方式
2020/02/06 Python
Python解释器及PyCharm工具安装过程
2020/02/26 Python
如何配置关联Python 解释器 Anaconda的教程(图解)
2020/04/30 Python
Python爬取股票信息,并可视化数据的示例
2020/09/26 Python
德国高尔夫商店:Golfshop.de
2019/06/22 全球购物
水务局局长岗位职责
2013/11/28 职场文书
大学自主招生自荐信
2013/12/16 职场文书
开办饭店创业计划书
2013/12/28 职场文书
团购业务员岗位职责
2014/03/15 职场文书
2015年信息中心工作总结
2015/05/25 职场文书
解约证明模板
2015/06/19 职场文书
2016年安全生产先进个人事迹材料
2016/02/29 职场文书
导游词之澳门妈祖庙
2019/12/19 职场文书
OpenCV-Python实现油画效果的实例
2021/06/08 Python
Docker下安装Oracle19c
2022/04/13 Servers