Hướng dẫn php encrypt decrypt - php mã hóa giải mã

Trong PHP, có thể sử dụng một chuỗi mã hóa và giải mã một chuỗi bằng cách sử dụng một trong các tiện ích mở rộng mật mã được gọi là hàm openSSL để mã hóa và giải mã.

hàm openSSL_encrypt (): hàm openSSL_encrypt () được sử dụng để mã hóa dữ liệu. The openssl_encrypt() function is used to encrypt the data.

Syntax:

string openssl_encrypt( string $data, string $method, string $key,
                        $options = 0, string $iv, string $tag= NULL,
                        string $aad, int $tag_length = 16  )

Parameters:

  • $ Dữ liệu: Nó giữ chuỗi hoặc dữ liệu cần được mã hóa. It holds the string or data which need to be encrypted.
  • $ Phương thức: Phương thức mật mã được áp dụng bằng hàm openSSL_GET_CIPHER_METHODS (). The cipher method is adopted using openssl_get_cipher_methods() function.
  • $ Key: Nó giữ khóa mã hóa. It holds the encryption key.
  • Tùy chọn $: Nó giữ sự phân tách bitwise của các cờ OpenSSL_RAW_DATA và OPENSSL_ZERO_PADDING. It holds the bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.
  • $ IV: Nó giữ vectơ khởi tạo không phải là null. It holds the initialization vector which is not NULL.
  • $ TAG: Nó giữ thẻ xác thực được truyền bằng tham chiếu khi sử dụng chế độ mật mã AEAD (GCM hoặc CCM). It holds the authentication tag which is passed by reference when using AEAD cipher mode (GCM or CCM).
  • $ AAD: Nó giữ dữ liệu xác thực bổ sung. It holds the additional authentication data.
  • $ tag_length: Nó giữ độ dài của thẻ xác thực. Độ dài của thẻ xác thực nằm trong khoảng từ 4 đến 16 cho chế độ GCM. It holds the length of the authentication tag. The length of authentication tag lies between 4 to 16 for GCM mode.

Giá trị trả về: Nó trả về chuỗi được mã hóa thành công hoặc sai khi thất bại. It returns the encrypted string on success or FALSE on failure.

hàm openSSL_decrypt () Hàm openSSL_decrypt () được sử dụng để giải mã dữ liệu. The openssl_decrypt() function is used to decrypt the data.

Syntax:

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)

Parameters:

  • $ Dữ liệu: Nó giữ chuỗi hoặc dữ liệu cần được mã hóa. It holds the string or data which need to be encrypted.
  • $ Phương thức: Phương thức mật mã được áp dụng bằng hàm openSSL_GET_CIPHER_METHODS (). The cipher method is adopted using openssl_get_cipher_methods() function.
  • $ Key: Nó giữ khóa mã hóa. It holds the encryption key.
  • Tùy chọn $: Nó giữ sự phân tách bitwise của các cờ OpenSSL_RAW_DATA và OPENSSL_ZERO_PADDING. It holds the bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.
  • $ IV: Nó giữ vectơ khởi tạo không phải là null. It holds the initialization vector which is not NULL.
  • $ TAG: Nó giữ thẻ xác thực được truyền bằng tham chiếu khi sử dụng chế độ mật mã AEAD (GCM hoặc CCM). It holds the authentication tag using AEAD cipher mode (GCM or CCM). When authentication fails openssl_decrypt() returns FALSE.
  • $ AAD: Nó giữ dữ liệu xác thực bổ sung. It holds the additional authentication data.

$ tag_length: Nó giữ độ dài của thẻ xác thực. Độ dài của thẻ xác thực nằm trong khoảng từ 4 đến 16 cho chế độ GCM. It returns the decrypted string on success or FALSE on failure.

Giá trị trả về: Nó trả về chuỗi được mã hóa thành công hoặc sai khi thất bại. First declare a string and store it into variable and use openssl_encrypt() function to encrypt the given string and use openssl_decrypt() function to descrypt the given string.

hàm openSSL_decrypt () Hàm openSSL_decrypt () được sử dụng để giải mã dữ liệu. This example illustrates the encryption and decryption of string.

<?php

$ TAG: Nó giữ thẻ xác thực bằng chế độ mật mã AEAD (GCM hoặc CCM). Khi xác thực không thành công openSSL_decrypt () trả về sai.

Giá trị trả về: Nó trả về chuỗi được giải mã thành công hoặc sai khi thất bại.

Cách tiếp cận: Đầu tiên khai báo một chuỗi và lưu trữ nó vào biến và sử dụng hàm openSSL_encrypt () để mã hóa chuỗi đã cho và sử dụng hàm openSSL_decrypt () để giải thích chuỗi đã cho.

Ví dụ 1: Ví dụ này minh họa mã hóa và giải mã chuỗi.

$simple_string = ______7

echo ____10

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
1$simple_string;

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4 =
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
6;

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
8
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
9
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
1

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
8
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
8
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
1

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
3

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
4 =
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
6;

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
8 =
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
030

= 0= 1

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4= 5

= 6$simple_string6

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5$simple_string2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
1

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
3$simple_string
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5____
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
7

"Welcome to GeeksforGeeks\n"8

Output:

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks

echo <?php6

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
1
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
1$simple_string0;
Below example illustrate the encryption and decryption of string. Here string to be encrypted and decrypted string will be same but the encrypted string is randomly changed respectively.

<?php

$simple_string2 =

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
6;

$simple_string6 =

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
030

echo "Welcome to GeeksforGeeks\n"4

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
1= 0;

Ví dụ 1: Ví dụ này minh họa mã hóa và giải mã chuỗi.

$simple_string = ______7

echo ____10

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
1$simple_string;

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4 =
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
6;

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
8
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
9
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
1

= 6

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
8
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
1

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
3

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
4 =
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
6;

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
8 =
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
030

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
3$simple_string
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5____
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
7

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
8$simple_string6
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
1

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
2
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
3$simple_string
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
5____
string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)
4
Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
7

"Welcome to GeeksforGeeks\n"8

Output:

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks

References:

  • https://www.php.net/manual/en/function.openssl-encrypt.php
  • echo <?php6
    string openssl_decrypt( string $data, string $method, string $key,
                 int $options = 0, string $iv, string $tag, string $aad)
    
    1
    Original String: Welcome to GeeksforGeeks
    Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
    Decrypted String: Welcome to GeeksforGeeks
    
    2
    string openssl_decrypt( string $data, string $method, string $key,
                 int $options = 0, string $iv, string $tag, string $aad)
    
    1$simple_string0;

$simple_string2 =

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks
6;