-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.php
93 lines (87 loc) · 2.67 KB
/
util.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
function generateAESKey(): string|false
{
if (!function_exists('openssl_random_pseudo_bytes')) {
error_log('请开启openssl扩展');
return false;
}
return openssl_random_pseudo_bytes(16);
}
function aesEncrypt($data, $key): string|false
{
if (!function_exists('openssl_encrypt')) {
error_log('请开启openssl扩展');
return false;
}
$method = 'aes-128-ctr';
$iv = str_repeat("\x00", 16);
if (strlen($key) !== 16) {
error_log('密钥长度必须为16字节(128位)');
return false;
}
$encryptedData = openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
if ($encryptedData === false) {
error_log("aesEncrypt error");
return false;
}
return base64_encode($encryptedData);
}
function aesDecrypt($data, $key): string|false
{
if (!function_exists('openssl_decrypt')) {
error_log('请开启openssl扩展');
return false;
}
$method = 'aes-128-ctr';
$iv = str_repeat("\x00", 16);
if (strlen($key) !== 16) {
error_log('密钥长度必须为16字节(128位)');
return false;
}
$decryptedData = openssl_decrypt($data, $method, $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
if ($decryptedData === false) {
error_log("aesDecrypt error");
return false;
}
return $decryptedData;
}
function base64ToRsaPublicKey($base64String): OpenSSLAsymmetricKey|false
{
if (!function_exists('openssl_pkey_get_public')) {
error_log('请开启openssl扩展');
return false;
}
$pem = "-----BEGIN PUBLIC KEY-----\n"
. wordwrap($base64String, 64, "\n", true)
. "\n-----END PUBLIC KEY-----";
return openssl_pkey_get_public($pem);
}
function rsaEncrypt($data, $publicKey): false|string
{
if (!function_exists('openssl_public_encrypt')) {
error_log('请开启openssl扩展');
return false;
}
$encryptedData = '';
$success = openssl_public_encrypt($data, $encryptedData, $publicKey, OPENSSL_PKCS1_PADDING);
if (!$success) {
error_log("rsaEncrypt: Encryption failed");
return false;
}
return base64_encode($encryptedData);
}
function jsonExit($code, $msg): string
{
$response = [
'code' => $code,
'msg' => $msg
];
$jsonResponse = json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
if ($jsonResponse === false) {
return json_encode([
'code' => 500,
'msg' => 'JSON encoding error.'
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
return ($jsonResponse);
}