Tôi đang làm việc với mật mã trong một dự án và tôi cần một chút trợ giúp về cách làm việc openssl_encrypt
và openssl_decrypt
, tôi chỉ muốn biết cách cơ bản và chính xác nhất để thực hiện nó. Đây là những gì tôi nhận được cho đến nay:
// To encrypt a string
$dataToEncrypt = 'Hello World';
$cypherMethod = 'AES-256-CBC';
$key = random_bytes[32];
$iv = openssl_random_pseudo_bytes[openssl_cipher_iv_length[$cypherMethod]];
$encryptedData = openssl_encrypt[$dataToEncrypt, $cypherMethod, $key, $options=0, $iv];
Sau đó $cypherMethod
, tôi lưu trữ $key
và $iv
để sử dụng khi giải mã $encryptedData
. [Hãy không nói rõ về cách tôi lưu trữ các giá trị, cảm ơn!]
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
Trước hết, đoạn mã ví dụ trên có phải là một ví dụ chính xác về cách sử dụng php openssl_encrypt
không?
Thứ hai, phương pháp của tôi để tạo $key
và có $iv
chính xác và an toàn không? Bởi vì tôi tiếp tục đọc, các khóa phải được bảo mật bằng mật mã.
Cuối cùng,
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
1giá trị không bắt buộc phải có // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
2? Nếu có, thì tại sao nó // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
3chỉ trả về // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
4độ dài? Có nên không // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
5?17 hữu ích 1 bình luận 34k xem chia sẻ 1 bình luận 34k xem chia sẻ
[PHP 5 >= 5.3.0, PHP 7, PHP 8]
openssl_encrypt — Encrypts data — Encrypts data
Description
openssl_encrypt[ string
6, string // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
7, string // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
8, int // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
9 = 0, string // To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
$iv
= "", string openssl_encrypt
1 = openssl_encrypt
2, string openssl_encrypt
3 = "", int openssl_encrypt
4 = 16]: string|false[
string
6,// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
string
7,// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
string
8,// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
int
9 = 0,// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
string $iv
=
"",
string openssl_encrypt
1 = openssl_encrypt
2,
string openssl_encrypt
3 = "",
int openssl_encrypt
4 = 16
]: string|false
Parameters
openssl_encrypt
5The plaintext message data to be encrypted.
openssl_encrypt
6The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods[]. openssl_get_cipher_methods[].
openssl_encrypt
7 The passphrase. If the passphrase is shorter than expected, it is silently padded with openssl_encrypt
8 characters; if the passphrase is longer than expected, it is silently truncated.
openssl_encrypt
9 openssl_encrypt
9 is a bitwise disjunction of the flags openssl_decrypt
1 and openssl_decrypt
2. openssl_decrypt
1 and openssl_decrypt
2.
openssl_decrypt
3A non-NULL Initialization Vector.
openssl_decrypt
4The authentication tag passed by reference when using AEAD cipher mode [GCM or CCM].
openssl_decrypt
5Additional authenticated data.
openssl_decrypt
6 The length of the authentication openssl_decrypt
4. Its value can be between 4 and 16 for GCM mode.
Return Values
Returns the encrypted string on success or openssl_decrypt
8 on failure. openssl_decrypt
8 on failure.
Errors/Exceptions
Emits an openssl_decrypt
9 level error if an unknown cipher algorithm is passed in via the openssl_encrypt
6 parameter. openssl_decrypt
9 level error if an unknown cipher algorithm is passed in via the openssl_encrypt
6
parameter.
Emits an openssl_decrypt
9 level error if an empty value is passed in via the openssl_decrypt
3 parameter. openssl_decrypt
9 level error if an empty value is passed in via the openssl_decrypt
3 parameter.
Changelog
7.1.0 | openssl_encrypt[ string 6, string 7, string 8, int 9 = 0, string $iv = "", string openssl_encrypt 1 = openssl_encrypt 2, string openssl_encrypt 3 = "", int openssl_encrypt 4 = 16]: string|false |
Parameters
openssl_encrypt
5
$cypherMethod
6
The plaintext message data to be encrypted.
$cypherMethod
7
openssl_encrypt
6 ¶
The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods[].
$cypherMethod
8
$cypherMethod
9
$key
0
$key
1
$key
2
$key
3
$key
4
$key
5
$key
6
$key
7 openssl_encrypt
7 ¶
The passphrase. If the passphrase is shorter than expected, it is silently padded with openssl_encrypt
8 characters; if the passphrase is longer than expected, it is silently truncated.
$key
8
$key
9
$iv
0
$iv
1
$iv
2 openssl_encrypt
9 ¶
openssl_encrypt
9 is a bitwise disjunction of the flags openssl_decrypt
1 and openssl_decrypt
2.
$iv
3
$iv
4
$iv
5
$iv
6
$iv
7
$iv
8
$iv
9
$encryptedData
0
$key
7 openssl_decrypt
3 ¶
A non-NULL Initialization Vector.
$encryptedData
2
$encryptedData
3
$encryptedData
4 openssl_decrypt
4 ¶
The authentication tag passed by reference when using AEAD cipher mode [GCM or CCM].
$encryptedData
5
$encryptedData
6
$encryptedData
7
$encryptedData
8
$key
7 openssl_decrypt
5 ¶
Additional authenticated data.
php openssl_encrypt
0
php openssl_encrypt
1
php openssl_encrypt
2
php openssl_encrypt
3
php openssl_encrypt
4
php openssl_encrypt
5 openssl_decrypt
6 ¶
The length of the authentication openssl_decrypt
4. Its value can be between 4 and 16 for GCM mode.
php openssl_encrypt
6
php openssl_encrypt
7
$encryptedData
3
php openssl_encrypt
9Return Values ¶
openssl_encrypt
9 is a bitwise disjunction of the flags openssl_decrypt
1 and openssl_decrypt
2.
$key
0
$key
1
$key
2
$key
3 openssl_decrypt
3 ¶
openssl_encrypt
9 is a bitwise disjunction of the flags openssl_decrypt
1 and openssl_decrypt
2.
$key
4
$key
5
$key
6
$key
7
$key
8
$key
7 openssl_decrypt
3 ¶
A non-NULL Initialization Vector.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
00 openssl_decrypt
4 ¶
Additional authenticated data.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
01// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
02// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
03// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
04// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
05// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
06// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
07// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
08
openssl_decrypt
6 ¶
The length of the authentication openssl_decrypt
4. Its value can be between 4 and 16 for GCM mode.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
09// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
10// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
11// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
12// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
13// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
14// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
15// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
16// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
17$key
7Return Values ¶
The length of the authentication openssl_decrypt
4. Its value can be between 4 and 16 for GCM mode.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
19// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
20// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
21// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
22// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
23// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
24$key
7Return Values ¶
Returns the encrypted string on success or openssl_decrypt
8 on failure.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
26// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
27// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
28 Emits an openssl_decrypt
9 level error if an unknown cipher algorithm is passed in via the openssl_encrypt
6 parameter. Emits an openssl_decrypt
9 level error if an empty value is passed in via the openssl_decrypt
3 parameter. ¶
Changelog
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
30// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
31// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
32// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
33Version ¶
Additional authenticated data.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
34 openssl_decrypt
6 ¶
Additional authenticated data.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
35// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
36// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
37// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
38$key
7 openssl_decrypt
6 ¶
The length of the authentication openssl_decrypt
4. Its value can be between 4 and 16 for GCM mode.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
40Return Values ¶
The authentication tag passed by reference when using AEAD cipher mode [GCM or CCM].
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
41// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
42// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
43// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
44// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
45// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
46// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
47// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
48 openssl_decrypt
5 ¶
The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods[].
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
49 openssl_encrypt
7 ¶
The length of the authentication openssl_decrypt
4. Its value can be between 4 and 16 for GCM mode.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
50// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
51$key
7Return Values ¶
Returns the encrypted string on success or openssl_decrypt
8 on failure.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
53// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
54 Emits an openssl_decrypt
9 level error if an unknown cipher algorithm is passed in via the openssl_encrypt
6 parameter.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
32// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
57 Emits an openssl_decrypt
9 level error if an empty value is passed in via the openssl_decrypt
3 parameter. ¶
A non-NULL Initialization Vector.
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
58 openssl_decrypt
4 ¶
The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods[].
// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
59// To decrypt an encrypted string
$decryptedData = openssl_decrypt[$encryptedData, $cypherMethod, $key, $options=0, $iv];
60$key
7