PHP实现的AES加密、解密封装类与用法示例
作者:u011474028 发布时间:2023-07-23 12:56:45
标签:PHP,AES,加密
本文实例讲述了PHP实现的AES加密、解密封装类与用法。分享给大家供大家参考,具体如下:
<?php
/**
* Class AES
* 用于AES加解密数据
* time:2018-04-27
*/
class AES
{
protected $cipher = MCRYPT_RIJNDAEL_256; //AES加密算法
protected $mode = MCRYPT_MODE_CBC; //采用cbc加密模式
protected $key; //密钥
protected $iv; //cbc模式加密向量,如为空将采用密钥代替
/**
* AES constructor.
*
* @param $key 密钥
* @param null $iv 向量 可选 如为空将采用密钥代替
*
* @throws Exception
*/
public function __construct($key, $iv = NULL)
{
if (!extension_loaded("mcrypt")) {
// throw new \Exception("mcrypt extension do not exist. it was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0. use OpenSSL instead");
}
$this->key = $key;
$this->iv = $iv;
}
/**
* 加密数据
* @param $data
*
* @return string
*/
public function encrypt($data)
{
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
$key = hash("sha256", $this->key, true);
$iv = isset($this->iv) ? hash("sha256", $this->iv, true) : $key;
$data = $this->padding($data);
mcrypt_generic_init($td, $key, $iv);
$encryptedData = base64_encode(mcrypt_generic($td, $data));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encryptedData;
}
/**
* 解密数据
* @param $data
*
* @return bool|string
*/
public function decrypt($data)
{
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
$key = hash("sha256", $this->key, true);
$iv = isset($this->iv) ? hash("sha256", $this->iv, true) : $key;
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, base64_decode($data));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this->unPadding($decrypted_data);
}
/**
* 填充数据到分组大小的整数倍
* @param null $data
*
* @return string
*/
protected function padding($data = null)
{
$blockSize = 32; //MCRYPT_RIJNDAEL_256算法的分组大小是32字节
$pad = $blockSize - (strlen($data) % $blockSize);
return $data . str_repeat(chr($pad), $pad);
}
/**
* 去掉填充的数据
* @param null $data
*
* @return bool|string
*/
protected function unPadding($data = null)
{
$pad = ord($data[strlen($data) - 1]);
if ($pad > strlen($data)) {
return false;
}
if (strspn($data, chr($pad), strlen($data) - $pad) != $pad) {
return false;
}
return substr($data, 0, -1 * $pad);
}
/**
* @return mixed
*/
public function getSecretKey()
{
return $this->key;
}
/**
* @param mixed $key
*/
public function setSecretKey($key)
{
$this->key = $key;
}
/**
* @return null
*/
public function getIv()
{
return $this->iv;
}
/**
* @param null $iv
*/
public function setIv($iv)
{
$this->iv = $iv;
}
}
//使用方法:
$keyStr = 'sq8f77fwhksk';
$aes = new AES($keyStr);
$str = 'www.aspxhome.com';
$chgstr = $aes->encrypt($str);
echo $chgstr;
echo "<br/>";
$rstr = $aes->decrypt($chgstr);
echo $rstr;
?>
运行结果:
pDyiRRNaxlss2b6SgoiVPdkD2m1QWhX393lh2iFgGdY=
www.aspxhome.com
希望本文所述对大家PHP程序设计有所帮助。
来源:https://blog.csdn.net/u011474028/article/details/80108810
0
投稿
猜你喜欢
- 下面是Sql Server 和 Access 操作数据库结构的常用Sql,如如何新建一个表,如何删除表,如何删除数据,插入数据等等..希望对
- 图片轮播滤镜转换效果,只支持ie浏览器<img width="156" height="60"
- 当管理SQL Server内在的帐户和密码时,我们很容易认为这一切都相当的安全。但实际上并非如此。在这里,我们列出了一些对于SQL Serv
- 如何编写CSS代码才能更有效率?这是许多网页制作者与开发者都关心的问题。大概没有什么魔法,可以保证一下就把你的样式表缩小到百分之多少,但合理
- 1 create table test(coltest varchar(20))2 实现这一功能 的 sql 语句 s
- 当使用MySQL做站点的时候,肯定会有不知道的错误发生,怎么记录呢?以下是具体解决方法:class.method &n
- ExpiresAbsolute 属性指定缓存于浏览器中的页的到期日期和时间语法Response.ExpiresAbsolute [
- 先由exp把数据卸出到文件系统, 产生一个.dmp文件, 然后必要时再由imp将数据装入数据库. 对于一般中小型数据库来说, 全数据库的ex
- 判断某一个表的记录总数,对于一个开发者来说是最再常见不过的事,我想大家都常用的作法就是:以下为引用的内容:select count(*) f
- 一、适配器模式适配器,顾名思义是一种万能的接口,达到万能转换的效果。适配器模式,定义一个适配器类,并且在该类中定义了适配器接口,这些适配接口
- 最近在做一个程序正好需要用到此方面,在网上找到过相应的程序,但用起来都非常恶,于是乎只好自己实现一个了。 首先实现两个函数用来操作光标:
- 在pytest自动化测试中,如果只是简单的从应用的角度来说,完全可以不去了解pytest中的显示信息的部分以及原理,完全可以通过使用推荐的p
- 在设计网页之前,客户或产品经理会提出对网页视觉风格设计的期望:活跃、大气、稳重、信赖、都市化….. 设计师一听到关键词或许很自然地在心里蹦出
- 百度了一下,找了点别人的方法改进了一下。 获取天气网址:http://www.weather.com.cn/html/weather/101
- 春节休息了几天,今天上班第一天,最近混twitter混得比较多,经常要压缩URL,以前做了个书签用http://is.gd/压缩,后来发现了
- 一、为什么难 秒杀系统难做的原因:库存只有一份,所有人会在集中的时间读和写这些数据。例如小米手
- 数据类型是所有开发语言的基础,JavaScript虽然是一个弱类型的脚本语言,但是在数据类型上也有很多讲究的,看了淘宝UED玉伯的一篇文章,
- // 格式化字符串 Fmt("{0}.[{id}].{name}",{id:1,name:'
- detectres.asp<HTML><head><TITLE>asp教程之全能屏幕分辨率侦测</
- 程序一:负责从字典中随机提取数据,写入一个新文件。(1.php) <?php /* 从字典文件中提取随机值 */