PHP对称加密类
<?php
/**
* Created by PhpStorm.
* User: zongbinghuang
* Date: 2017/7/31
* Time: 15:13
*/ namespace app\common; use Exception; class BizEncrypt
{
const IV_SIZE = 16;
const CIPHER = 'AES-256-CBC'; private $key;#length:32 public function __construct($key)
{
$key = (string)$key; if (static::supported($key, self::CIPHER)) {
$this->key = $key;
} else {
throw new Exception('The only supported ciphers are AES-256-CBC with the correct key lengths.');
}
} public function encrypt($value)
{
$iv = self::random_bytes(self::IV_SIZE); $value = openssl_encrypt(serialize($value), self::CIPHER, $this->key, 0, $iv); if ($value === false) {
throw new \Exception('Could not encrypt the data.');
} $iv = base64_encode($iv);
$mac = hash_hmac('sha256', $iv . $value, $this->key);
$json = json_encode(compact('iv', 'value', 'mac')); if (!is_string($json)) {
throw new Exception('Could not encrypt the data.');
} return base64_encode($json);
} public function decrypt($payload)
{
$payload = $this->getJsonPayload($payload); $iv = base64_decode($payload['iv']); $decrypted = openssl_decrypt($payload['value'], self::CIPHER, $this->key, 0, $iv); if ($decrypted === false) {
throw new Exception('Could not decrypt the data.');
} return unserialize($decrypted);
} private static function supported($key, $cipher)
{
$length = mb_strlen($key, '8bit'); return ($cipher === 'AES-256-CBC' && $length === 32);
} private function getJsonPayload($payload)
{
$payload = json_decode(base64_decode($payload), true); if (!$payload || $this->invalidPayload($payload)) {
throw new Exception('The payload is invalid.');
} return $payload;
} private function invalidPayload($data)
{
return !is_array($data) || !isset($data['iv']) || !isset($data['value']) || !isset($data['mac']);
} private static function random_bytes($bytes)
{
try {
$bytes = self::RandomCompareInteger($bytes);
} catch (Exception $ex) {
throw new Exception('random_bytes(): $bytes must be an integer');
} if ($bytes < 1) {
throw new Exception('Length must be greater than 0');
} $secure = true;
$buf = openssl_random_pseudo_bytes($bytes, $secure);
if ($buf !== false && $secure && mb_strlen($buf, '8bit') === $bytes) {
return $buf;
} throw new Exception('Could not gather sufficient random data');
} private static function RandomCompareInteger($number, $fail_open = false)
{
if (is_numeric($number)) {
$number += 0;
} if (is_float($number) && $number > ~PHP_INT_MAX && $number < PHP_INT_MAX) {
$number = (int)$number;
} if (is_int($number) || $fail_open) {
return $number;
} throw new Exception('Expected an integer.');
}
}
PHP对称加密类的更多相关文章
- Java常用的加密解密类(对称加密类)
Java常用的加密解密类 原文转载至:http://blog.csdn.net/wyc_cs/article/details/8793198 原创 2013年04月12日 14:33:35 1704 ...
- C#加密类
var es= EncryptSugar.GetInstance(); string word = "abc"; var wordEncrypt = es.Encrypto(wor ...
- .net 对称加密DESCryptoServiceProvider
1.生成密钥以加密和解密数据 DESCryptoServiceProvider 基于一种对称加密算法.对称加密需要密钥和初始化矢量 (IV) 来加密数据.要解密该数据,您必须拥有此同一密钥和 IV.您 ...
- DotNet加密方式解析--对称加密
离过年又近了一天,回家已是近在咫尺,有人欢喜有人愁,因为过几天就得经历每年一度的装逼大戏,亲戚朋友加同学的各方显摆,所以得靠一剂年终奖来装饰一个安稳的年,在这里我想起了一个题目“论装逼的技术性和重要性 ...
- AES对称加密解密类
import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.spec.Se ...
- Asp.Net 常用工具类之加密——对称加密DES算法(2)
又到周末,下午博客园看了两篇文章,关于老跳和老赵的程序员生涯,不禁感叹漫漫程序路,何去何从兮! 转眼毕业的第三个年头,去过苏州,跑过上海,从一开始的凌云壮志,去年背起行囊默默回到了长沙准备买房,也想有 ...
- C#不对称加密
对称加密的缺点是双方使用相同的密钥和IV进行加密.解密.由于接收方必须知道密钥和IV才能解密数据,因此发送方需要先将密钥和IV传递给接收方.这就 有一个问题,如果攻击者截获了密钥和IV,也就等于知道了 ...
- 介绍对称加密的另一个算法——PBE
除了DES,我们还知道有DESede(TripleDES,就是3DES).AES.Blowfish.RC2.RC4(ARCFOUR)等多种对称加密方式,其实现方式大同小异,这里介绍对称加密的另一个算法 ...
- 对称加密DES和TripleDES
一. 对称加密 对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码).因此,通信双方都必 ...
随机推荐
- [转] Makefile 基础 (2) —— Makefile 总述
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- Linux目录结构简析
Linux目录结构简析 Linux继承了unix操作系统结构清晰的特点.在linux下的文件结构非常有条理.但是,上述的优点只有在对linux相当熟悉时,才能体会到.现在,虫虫就把linux下的目录结 ...
- Zabbix实现短信报警设置(实战)
配置环境: zabbix 2.2.15 1.配置示警媒介类型 此文件所在位置:/usr/lib/zabbix/alertscripts/ 必须拥有执行权限,并且改变所属用户和组 要修改此脚本的路径,需 ...
- tomcat Enabling JMX Remote
wiki 利用JMX做存活监控 cat /opt/wiki/work/bin/setenv.sh | grep jmxremoteCATALINA_OPTS="-Dcom.sun.manag ...
- 转 python基础学习笔记(一)
http://www.cnblogs.com/fnng/category/454439.html 下面我们创建一个文件 root@fnngj-H24X:/hzh/python# touch hell. ...
- 标准C程序设计七---60
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 快充 IC BQ25896 的 input current monitor
BQ25896 沒有顯示 input current 的 register, 但可以讀 ILIM pin 的 電壓 做計算求出 input current.
- Swoole 简单学习
Swoole 百度百科:是一个PHP扩展,扩展不是为了提升网站的性能,是为了提升网站的开发效率.最少的性能损耗,换取最大 的开发效率.利用Swoole扩展,开发一个复杂的Web功能,可以在很短的时间内 ...
- CUDA程序计时
之前写的CUDA程序,想测量一下性能,网上很多用的是CPU端计时,很不准确.翻了一下书,发现这里应该使用事件来计时. CUDA中的事件本质上是一个GPU时间戳,这个时间戳是在用户指定的时间点上记录的. ...
- js-百度地图多个标注点
以成都华誉口腔3个门诊部为例,锦江区-青羊区-高新区 html: <div class = "demo_main"> <fieldset class = &quo ...