php写的AES加密解密类分享
2015-01-24信息快讯网
这篇文章主要介绍了php写的AES加密解密类,实际是为YII框架写的,不在YII框架时只需替换其中的两句代码即可使用,需要的朋友可以参考下
今天写了一个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操作文件的一些基本函数使用示例
一个经典的PHP文件上传类分享
php数组查找函数总结
一个经典的PHP验证码类分享
一个经典实用的PHP图像处理类分享
php中addslashes函数与sql防注入
php中current、next与reset函数用法实例
getimagesize获取图片尺寸实例
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
ThinkPHP在新浪SAE平台的部署实例
PHP实现AES256加密算法实例
ThinkPHP3.1之D方法实例详解
PHP提交表单失败后如何保留已经填写的信息
将酷狗krc歌词解析并转换为lrc歌词php源码
windows下配置apache+php+mysql时出现问题的处理方法
PHP扩展CURL的用法详解
教你如何解密 “ PHP 神盾解密工具 ”
CI框架自动加载session出现报错的解决办法
新浪SAE云平台下使用codeigniter的数据库配置
PHP命名空间(Namespace)简明教程
PHP独立Session数据库存储操作类分享
CI框架中libraries,helpers,hooks文件夹详细说明
一个严格的PHP Session会话超时时间设置方法
php中AES加密解密的例子小结
php实现aes加密类分享
sae使用smarty模板的方法