Skip to content

Commit 2cfd377

Browse files
author
苏青安
committed
docs(docs): 更新README
1 parent 44a54bd commit 2cfd377

File tree

2 files changed

+40
-122
lines changed

2 files changed

+40
-122
lines changed

README.md

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
<hr width="50%"/>
66
</div>
77

8-
A PHP package for request encryption, designed for quickly implementing secure communication between frontend and backend.
8+
A PHP toolkit for handling encrypted requests, enabling fast and secure front-end to back-end communication.
99

10-
In real-world development, there are often scenarios where requests need extra security: data must be encrypted to prevent sniffing, and requests should be protected against tampering or replay attacks. Coordinating encryption methods and signature rules with frontend teams each time can be cumbersome. To simplify this workflow, I created this PHP package and a companion npm package, allowing frontend developers to generate encrypted request parameters with a single function call, making it easy to implement secure API communication.
10+
In real-world development, you often encounter scenarios where requests need to be secure: data must be encrypted to prevent sniffing, and requests must be protected from tampering or replay attacks. Coordinating encryption methods and signature rules with the front-end can be cumbersome. This PHP package simplifies the process. Paired with a dedicated npm package, the front-end can generate encrypted request parameters with a single call, enabling secure and fast data transmission.
1111

12-
Frontend companion npm package: [npm-encrypted-request](https://github.com/zxc7563598/npm-encrypted-request)
12+
Front-end companion npm package: [npm-encrypted-request](https://github.com/zxc7563598/npm-encrypted-request)
1313

14-
**This project has been parsed by Zread. If you need a quick overview of the project, you can click here to view it:[Understand this project](https://zread.ai/zxc7563598/php-encrypted-request)**
14+
**This project has been parsed by Zread. To quickly understand it, you can click here:** **[Learn More](https://zread.ai/zxc7563598/php-encrypted-request)**
1515

1616
## Features
1717

18-
- 🔐 AES-128-CBC decryption of frontend-encrypted data to prevent leaks
19-
- ✍️ Dynamic MD5 signature verification to prevent forged signatures
20-
- ⏰ Timestamp validation (in seconds) with configurable tolerance, to prevent request hijacking
21-
- ⚙️ Configurable via `.env` file or array
22-
- 🧩 Extensible with custom decryptors
18+
- ♾️ **Hybrid encryption**: AES key is randomly generated, no need for front-end to store a fixed key, improving security
19+
- 🔐 **AES-128-CBC decryption**: Securely decrypt front-end encrypted data, back-end only needs to configure the RSA private key
20+
- ✍️ **Dynamic MD5 signature verification**: Prevents forged requests
21+
-**Second-level timestamp validation**: Customizable tolerance to prevent request hijacking
22+
- ⚙️ **Flexible configuration**: Use `.env` or pass an array directly
23+
- 🧠 **Minimal code changes required**: Front-end can securely send data without worrying about the underlying logic
2324

2425
## Installation
2526

@@ -29,25 +30,19 @@ composer require hejunjie/encrypted-request
2930

3031
## Configuration Example
3132

32-
You can configure via `.env` file:
33+
You can configure via `.env`:
3334

3435
```dotenv
35-
APP_KEY=your-app-key
36+
RSA_PRIVATE_KEY=your-private-key
3637
DEFAULT_TIMESTAMP_DIFF=60
37-
38-
# Optional when using a custom decryptor
39-
AES_KEY=your-aes-key
40-
AES_IV=your-aes-iv
4138
```
4239

43-
Or pass configuration as an array:
40+
Or pass an array directly:
4441

4542
```php
4643
$config = [
47-
'APP_KEY' => 'your-app-key', // Required: key used for signature verification (32 alphanumeric characters)
48-
'DEFAULT_TIMESTAMP_DIFF' => 60, // Required: maximum allowed timestamp difference in seconds
49-
'AES_KEY' => 'your-aes-key', // Optional: AES encryption key (16 characters); not needed for custom decryptor
50-
'AES_IV' => 'your-aes-iv' // Optional: AES initialization vector (16 characters); not needed for custom decryptor
44+
'RSA_PRIVATE_KEY' => 'your-private-key', // Private key string (including -----BEGIN PRIVATE KEY-----)
45+
'DEFAULT_TIMESTAMP_DIFF' => 60, // Optional, used to validate request expiry in seconds, default is 60
5146
];
5247
```
5348

@@ -58,14 +53,17 @@ $config = [
5853
```php
5954
use Hejunjie\EncryptedRequest\EncryptedRequestHandler;
6055

61-
$param = $_POST; // Fetch frontend request parameters
56+
$params = $_POST; // Obtain front-end request parameters
57+
58+
$config = ['RSA_PRIVATE_KEY' => 'your-private-key']; // Not needed if using .env
6259

63-
$handler = new EncryptedRequestHandler();
60+
$handler = new EncryptedRequestHandler($config);
6461
try {
6562
$data = $handler->handle(
66-
$param['en_data'] ?? '',
67-
$param['timestamp'] ?? '',
68-
$param['sign'] ?? ''
63+
$params['en_data'] ?? '',
64+
$params['enc_payload'] ?? '',
65+
$params['timestamp'] ?? '',
66+
$params['sign'] ?? ''
6967
);
7068
// $data contains the decrypted array
7169
} catch (\Hejunjie\EncryptedRequest\Exceptions\SignatureException $e) {
@@ -77,67 +75,28 @@ try {
7775
}
7876
```
7977

80-
### Custom Decryptor
81-
82-
> In most cases, if your `AES_KEY`, `AES_IV`, and `APP_KEY` remain confidential, the default AES-128-CBC decryption is sufficient for secure API communication.
83-
84-
> Custom decryptors are mainly for special scenarios, such as using a completely different encryption algorithm or requiring higher security standards.
78+
## Front-end Integration
8579

86-
> If your project demands extremely high confidentiality, it is recommended to design your own encryption rules rather than relying solely on the default AES implementation.
87-
88-
```php
89-
class MyCustomDecryptor implements \Hejunjie\EncryptedRequest\Contracts\DecryptorInterface
90-
{
91-
/**
92-
* Decrypt method
93-
*
94-
* @param string $data Encrypted data
95-
*
96-
* @return array Decrypted array
97-
* @throws \Hejunjie\EncryptedRequest\Exceptions\DecryptionException
98-
*/
99-
public function decrypt(string $data): array
100-
{
101-
// Custom decryption logic
102-
}
103-
}
104-
105-
$customDecryptor = new MyCustomDecryptor();
106-
$handler = new EncryptedRequestHandler($customDecryptor);
107-
```
108-
109-
## Frontend Integration
110-
111-
Frontend can use the [hejunjie-encrypted-request](https://github.com/zxc7563598/npm-encrypted-request) npm package to generate encrypted data and send it to the PHP backend:
80+
The front-end uses the [hejunjie-encrypted-request](https://github.com/zxc7563598/npm-encrypted-request) npm package to generate encrypted data and send it to the PHP back-end:
11281

11382
```typescript
11483
import { encryptRequest } from "hejunjie-encrypted-request";
11584

11685
const encrypted = encryptRequest(
11786
{ message: "Hello" },
11887
{
119-
appKey: "your-app-key",
120-
aesKey: "your-aes-key",
121-
aesIv: "your-aes-iv",
122-
token: "your-token",
88+
rsaPubKey: "your-public-key",
12389
}
12490
);
12591
```
12692

127-
The PHP backend can directly use `EncryptedRequestHandler` to decrypt.
128-
129-
## Notes
130-
131-
1. AES key and IV must be 16 characters.
132-
2. Timestamp is in seconds; `DEFAULT_TIMESTAMP_DIFF` is the maximum allowed difference. Be mindful of time zones.
133-
3. Frontend `appKey`, `aesKey`, and `aesIv` must match backend, otherwise signature verification will fail.
134-
4.`token` is optional and will be passed together with encrypted data.
93+
The PHP back-end can directly decrypt using `EncryptedRequestHandler`.
13594

13695
## Compatibility
13796

13897
- PHP \>\= 8.1
139-
- Compatible with any PSR-4 autoloading framework or plain PHP project
98+
- Works with any PSR-4 autoloading framework or plain PHP project
14099

141100
## Development & Contribution
142101

143-
Feel free to submit issues or pull requests, contribute new decryptors, optimize functionality, or add examples.
102+
Contributions are welcome! Submit issues or pull requests to add new decoders, optimize features, or provide examples.

README.zh-CN.md

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ PHP 请求加密处理工具包,用于快速实现安全的前后端通信。
1515

1616
## 功能特性
1717

18-
- 🔐 AES-128-CBC 解密前端加密数据,防止数据泄露
18+
- ♾️ 混合加密,AES 密钥随机生成,前端不需要存储固定密钥,提高安全性
19+
- 🔐 AES-128-CBC 解密前端加密数据,防止数据泄露,后端只需配置 RSA 私钥即可
1920
- ✍️ 动态 MD5 签名校验,防止伪造签名
2021
- ⏰ 秒级时间戳验证,可自定义误差范围,防止劫持请求
2122
- ⚙️ 支持通过 `.env` 或数组传入配置
22-
- 🧩 可扩展自定义解密器
23+
- 🧠 对代码改动极小,配套 npm 无需关注原理即可安全传输数据
2324

2425
## 安装
2526

@@ -32,22 +33,16 @@ composer require hejunjie/encrypted-request
3233
可以通过 `.env` 文件配置:
3334

3435
```dotenv
35-
APP_KEY=your-app-key
36+
RSA_PRIVATE_KEY=your-private-key
3637
DEFAULT_TIMESTAMP_DIFF=60
37-
38-
# 自定义解密器时可不传
39-
AES_KEY=your-aes-key
40-
AES_IV=your-aes-iv
4138
```
4239

4340
也可以通过数组直接传入:
4441

4542
```php
4643
$config = [
47-
'APP_KEY' => 'your-app-key', // 必传,签名密钥,用于接口签名校验(32位字母或数字)
48-
'DEFAULT_TIMESTAMP_DIFF' => 60, // 必传,用于验证请求是否过期,秒级
49-
'AES_KEY' => 'your-aes-key', // 非必传,AES 加密的密钥(16位),自定义解密器时可不传
50-
'AES_IV' => 'your-aes-iv' // 非必传,AES 加密的初始化向量(16位),自定义解密器时可不传
44+
'RSA_PRIVATE_KEY' => 'your-private-key', // 私钥字符串(包含 -----BEGIN PRIVATE KEY-----)
45+
'DEFAULT_TIMESTAMP_DIFF' => 60, // 非必传,用于验证请求是否过期,秒级,默认60
5146
];
5247
```
5348

@@ -60,10 +55,13 @@ use Hejunjie\EncryptedRequest\EncryptedRequestHandler;
6055

6156
$param = $_POST; // 自行获取前端请求的参数
6257

63-
$handler = new EncryptedRequestHandler();
58+
$config = ['RSA_PRIVATE_KEY' => 'your-private-key']; // 如果通过.env配置则无需在此处传递
59+
60+
$handler = new EncryptedRequestHandler($config);
6461
try {
6562
$data = $handler->handle(
6663
$param['en_data'] ?? '',
64+
$param['enc_payload'] ?? '',
6765
$param['timestamp'] ?? '',
6866
$param['sign'] ?? ''
6967
);
@@ -77,35 +75,6 @@ try {
7775
}
7876
```
7977

80-
### 自定义解密器
81-
82-
> 实际上,对于绝大多数场景,如果你的 AES_KEY、AES_IV 和 APP_KEY 保密,默认的 AES-128-CBC 解密已经足够满足接口数据加密的需求。
83-
84-
> 自定义解密器主要适用于特殊场景,比如需要使用完全不同的加密算法或对加密规则有更高安全要求。
85-
86-
> 如果你的项目对保密性有极高要求,建议自行设计和实现接口数据的加密规则,而不是使用公开方案。
87-
88-
```php
89-
class MyCustomDecryptor implements \Hejunjie\EncryptedRequest\Contracts\DecryptorInterface
90-
{
91-
/**
92-
* 解密方法
93-
*
94-
* @param string $data 加密数据
95-
*
96-
* @return array 解密后的数组
97-
* @throws \Hejunjie\EncryptedRequest\Exceptions\DecryptionException
98-
*/
99-
public function decrypt(string $data): array
100-
{
101-
// 自定义解密逻辑
102-
}
103-
}
104-
105-
$customDecryptor = new MyCustomDecryptor();
106-
$handler = new EncryptedRequestHandler($customDecryptor);
107-
```
108-
10978
## 前端配合
11079

11180
前端使用 [hejunjie-encrypted-request](https://github.com/zxc7563598/npm-encrypted-request) npm 包生成加密数据,并发送给 PHP 后端:
@@ -116,23 +85,13 @@ import { encryptRequest } from "hejunjie-encrypted-request";
11685
const encrypted = encryptRequest(
11786
{ message: "Hello" },
11887
{
119-
appKey: "your-app-key",
120-
aesKey: "your-aes-key",
121-
aesIv: "your-aes-iv",
122-
token: "your-token",
88+
rsaPubKey: "your-public-key",
12389
}
12490
);
12591
```
12692

12793
PHP 后端直接使用 `EncryptedRequestHandler` 解密即可。
12894

129-
## 注意事项
130-
131-
1. AES 密钥和向量必须为 16 位。
132-
2. 时间戳单位为秒,`DEFAULT_TIMESTAMP_DIFF` 为允许的最大时间差;需要注意时区问题。
133-
3. 前端 `appKey` / `aesKey` / `aesIv` 与后端保持一致,否则签名验证失败。
134-
4.`token` 可选,会与密文一起传给 PHP 后端
135-
13695
## 兼容性
13796

13897
- PHP \>\= 8.1

0 commit comments

Comments
 (0)