Home > Backend Development > PHP Problem > What is the php encryption and decryption method?

What is the php encryption and decryption method?

藏色散人
Release: 2023-03-11 14:48:01
Original
6791 people have browsed it

How to implement php encryption and decryption: 1. You can use the "openssl_encrypt" method to implement encryption; 2. Use the "openssl_decrypt" method to implement decryption.

What is the php encryption and decryption method?

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

Recommended related articles:How does PHP encrypt and decrypt? Methods of PHP encryption and decryption》《What are the PHP encryption and decryption tools》《Encryption and decryption processing classes implemented by PHP

php encryption and decryption What is the method?

php implements AES encryption and decryption

In php we use openssl_encrypt to implement encryption and openssl_decrypt to implement decryption

1: openssl_encrypt Detailed explanation of the method:

openssl_encrypt($data, $method, $key, $options = 0, $iv = "", &$tag = NULL, $aad = "", $tag_length = 16)
Copy after login

Parameters:

1.$data: Encrypted plain text

2.$method: Encryption method: What encryption methods can be obtained through openssl_get_cipher_methods()

3.$passwd: Encryption key [password]

4.$options: Data format options (optional) [options are:]: 0, OPENSSL_RAW_DATA=1, OPENSSL_ZERO_PADDING=2, OPENSSL_NO_PADDING=3

5.$iv: secret initialization vector (optional), please note: if the method is DES−ECB, iv does not need to be filled in

6.$tag: use AEAD password The authentication tag referenced when passing the mode (GCM or CCM) (optional)

7.$aad: Additional authentication data. (Optional)

8.$tag_length: Verify the length of the tag. In GCM mode, its range is 4 to 16 (optional)

2: Detailed explanation of openssl_decrypt method

openssl_decrypt($data, $method, $password, $options = 1, $iv = "", $tag = "",  $aad = "")
Copy after login

Parameters:

1.$data: encryption to be decrypted information.

2.$method: Decryption method: What decryption methods can be obtained through openssl_get_cipher_methods()

3.$passwd: Decryption key [password]

4.$ options: Data format options (optional) [options are:]: 0, OPENSSL_RAW_DATA=1, OPENSSL_ZERO_PADDING=2, OPENSSL_NO_PADDING=3

5.$iv: secret initialization vector (optional), please note: If the method is DES−ECB, iv does not need to be filled in

6.$tag: Authentication tag in AEAD password mode (optional)

7.$aad: Additional authentication data. (Optional)

Three: Implement AES encryption and decryption

1: AES encryption

// 要加密的字符串  
$data = 'test';  
// 密钥  
$key = '123456';  
// 加密数据 'AES-128-ECB' 可以通过openssl_get_cipher_methods()获取  
$encrypt = openssl_encrypt($data, 'AES-128-ECB', $key, 0);  
echo (($encrypt));
Copy after login

2: AES decryption

//解密字符串  
$encrypt = '***';  
//密钥  
$key = '123456';  
// 解密数据  
$decrypt = openssl_decrypt($encrypt, 'AES-128-ECB', $key, 0);  
echo $decrypt;
Copy after login

This can be achieved according to the above AES encryption and decryption function

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the php encryption and decryption method?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template