AesEncrypt.php
992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace App\Helper;
use phpseclib3\Crypt\AES;
/**
* Class Aes
* @package App\Helper
*/
class AesEncrypt
{
private static $length = 256;
/**
* 加密
* @param $str
* @return string
*/
public static function encrypt($str, $mode = 'ecb')
{
try {
$cipher = new AES($mode);
$cipher->setKeyLength(self::$length);
$cipher->setKey(env('secrect_key'));
return base64_encode($cipher->encrypt($str));
} catch (\Exception $e){
return null;
}
}
/**
* 解密
* @param $str
* @return string
*/
public static function decrypt($str, $mode = 'ecb')
{
try {
$cipher = new AES($mode);
$cipher->setKeyLength(self::$length);
$cipher->setKey(env('secrect_key'));
return $cipher->decrypt(base64_decode($str));
} catch (\Exception $e) {
return null;
}
}
}