Data encryption has become more and more important in our lives, especially considering the large amount of data that has been traded and transmitted over the Internet. If you are interested in using security measures, you will be interested in learning a series of security functions provided by PHP. In this article, we will introduce these functions and provide some basic usage, so that you can become more and more important in our life for your own application of soft data encryption, especially considering the large volume of transactions and transmitted data on the network. If you are interested in using security measures, you will be interested in learning a series of security functions provided by PHP. In this article, we will introduce these features and provide some basic usage so that you can add security features to your application software. Prerequisites
Before giving a detailed introduction to PHP's security features, we need to spend some time learning about this.
Language = "JavaScript1.1" src = "/upimg/allimg/070123/2247580 .jpg"/> |
Readers of this section will introduce some basic knowledge about cryptography. if you are familiar with the basic concepts of cryptography, you can skip this section.
Cryptography can be widely described as the research and experiment on encryption/decryption. encryption is a process of converting easy-to-understand data into easy-to-understand data, decryption is the process of converting obscure data into original understandable data. An obscure document is called a password, and an easy-to-understand document is called a plaintext.
Data encryption/decryption requires certain algorithms. these algorithms can be very simple, such as the famous Caesar code. However, the current encryption algorithm is much more complex, some of these methods cannot be decrypted even by using the existing methods.
PHP encryption
Anyone who has experience using a non-Windows platform may be familiar with crypt (). This function is called one-way encryption and can encrypt some plain codes, however, the password cannot be converted to the original plaintext. Although on the surface this seems useless, it is indeed widely used to ensure the integrity of the system password. Because, once a one-way encryption password falls into the hands of a third party, it is useless because it cannot be restored to plain text. When verifying the password entered by the user, the user's input is also a one-way algorithm. if the input matches the stored encrypted password, the entered message must be correct.
PHP also provides the possibility of using its crypt () function to implement one-way encryption. Here I will briefly introduce this function:
string crypt (string input_string [, string salt])
The input_string parameter is the string to be encrypted, and the second available salt is a single-digit string. it can affect the encrypted password and further eliminate the possibility of a pre-computing attack. By default, PHP uses a two-character DES interference string. if your system uses MD5 (I will introduce the MD5 algorithm later ), it uses a 12-character interference string. By the way, you can run the following command to find the length of the interference string to be used by the system:
print "My system salt size is: ". CRYPT_SALT_LENGTH;
The system may also support other encryption algorithms. Crypt () supports the following algorithms and the length of the corresponding salt parameter:
Algorithm |
Salt length |
CRYPT_STD_DES |
2-character (Default) |
CRYPT_EXT_DES |
9-character |
CRYPT_MD5 |
12-character beginning with 102/td> |
CRYPT_BLOWFISH |
16-character beginning with 102/td> |
Use crypt () for user authentication
As an example of the crypt () function, you want to create a PHP script to restrict access to a directory, only users with the correct username and password can access this directory. I will store materials in a table in my favorite MySQL database. The following is an example of creating a table called members:
Mysql> create table members ( -> Username CHAR (14) not null, -> Password CHAR (32) not null, -> Primary key (username) -> ); |
Then, we assume that the following data has been stored in the table:
User name |
Password |
Clark |
KeloD1C377lKE |
Bruce |
Ba1T7vnz9AWgk |
Peter |
PaLUvRWsRLZ4U |