兼容php5和php7openssl_encryptAES-128-CBC
直接上个类吧
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/7/25
* Time: 17:53
*/
/**
* @desc:php aes加密解密类
* @author [Lee] <[<complet@163>]>
*/
class aes{
// 加密⽅式:1、mcrypt;2、openssl 默认1
private $type;
// cast-128 gost rijndael-128 twofish cast-256 loki97 rijndael-192 saferplus wake blowfish-compat des rijndael-256 serpent xtea blowfish enigma rc2 // AES-128-CBC AES-128-CFB AES-128-CFB1 AES-128-CFB8 AES-128-CTR AES-128-ECB AES-128-OFB AES-128-XTS AES-192-CBC AES-192-CFB // private $cipher = 'AES-128-ECB';
//?iv
private $cipher = 'AES-128-CBC';
// cbc cfb ctr ecb ncfb nofb ofb stream
private $mode = 'stream';
private $iv = '2398DHY433UGFKL1X';
// MCRYPT_RAND MCRYPT_DEV_RANDOM MCRYPT_DEV_URANDOM
private $source = MCRYPT_RAND;
private $key;
private $data;
/*
构造函数
@param key 密钥
@param type 加密类型:1、mcrypt;2、openssl
*/
public function __construct($key,$type = 2){
// 关闭所有PHP错误报告
error_reporting(0);
$this->key = $key;
$this->type = $type;
php8兼容php7吗}
private function getiv(){
$cipher = $this->cipher;
$mode = $this->mode;
$source = $this->source;
$size = mcrypt_get_iv_size($cipher,$mode);
$iv = mcrypt_create_iv($size,$source);
return $iv;
}
public function encrypt($data){
$type = $this->type;
$cipher = $this->cipher;
$mode = $this->mode;
$key = $this->key;
if($type == 1){
$iv = $this->getiv();
$td = mcrypt_module_open($cipher, "", $mode, "");
mcrypt_generic_init($td, $key, $iv);
$encrypted = mcrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$ret = base64_encode($encrypted);
}else{
$encryption_key = base64_decode($key);
$encryption_key = base64_decode($key);
$len = openssl_cipher_iv_length($cipher);
//⽣成⼀串伪随机字节,其中字节数由length参数确定。
// $iv = openssl_random_pseudo_bytes($len);
$iv = $this->iv;
$encrypted = openssl_encrypt($data, $cipher, $encryption_key, 0, $iv);
$ret = base64_encode($encrypted . '::' . $iv);
}
return $ret;
}
public function decrypt($data){
$type = $this->type;
$cipher = $this->cipher;
$mode = $this->mode;
$key = $this->key;
if($type == 1){
$iv = $this->getiv();
$td = mcrypt_module_open($cipher,"",$mode,"");
mcrypt_generic_init($td, $key, $iv);
$decode = base64_decode($data);
$dencrypted = mdecrypt_generic($td, $decode);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$ret = $dencrypted;
}else{
$encryption_key = base64_decode($key);
$arr = explode('::', base64_decode($data));
$encrypted_data = $arr[0];
$iv = $arr[1];
var_dump($encrypted_data, $cipher, $encryption_key, 0, $iv);
$ret = openssl_decrypt($encrypted_data, $cipher, $encryption_key, 0, $iv);
var_dump($ret);
}
return $ret;
}
}
$key = 'FW2VN#N8DAL147L*';
$data = '{"messageid":15640501253114,"timestamp":1564050125,"deviceid":"C192101101","cmd":"CMD-01","desired":{"allget":1}}';
$aes = new aes($key,2);
$encode = $aes->encrypt($data);
echo $encode.PHP_EOL;
//$encode = 'Z1ZaeVZwaEd6dXNwMjgxSXF0MzcyeWN4aHd4dUhMRmR6RVA0bDlnblhrMWI1NmtuRG1PdW5CeWdsb01qOGZsVTNWbEdZdzBFQnltT0lwU3dVR $decode = $aes->decrypt($encode);
echo $decode.PHP_EOL;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论