Hướng dẫn php crypt decrypt - giải mã mật mã php

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.

Bài Viết Liên Quan

Chủ Đề