Home  >  Article  >  Backend Development  >  Summary of encryption usage of php function crypt()

Summary of encryption usage of php function crypt()

伊谢尔伦
伊谢尔伦Original
2017-06-09 11:49:343285browse

In php functions, there are many encryption functions, one of which can implement data encryption function is called crypt() function. The crypt() function uses a one-way algorithm and has no corresponding decryption function. It returns a string encrypted using DES, Blowfish, or MD5 algorithms. On the surface, the crypt() function seems to be of little use, but this function is indeed widely used to ensure the integrity of system passwords. Because even if the one-way encrypted password falls into the hands of a third party, it will not be of much use since it cannot be restored to plain text. Below we will make some summary of the use of crypt() function, hoping to be helpful to everyone's PHP learning.

crypt() function related content

1. PHP encryption function—crypt() function encryption usage example

Summary of encryption usage of php function crypt()

Before introducing the encryption function, let us first introduce the principle of data encryption: it is to process the original plain file or data according to a certain algorithm to make it unreadable A piece of code, usually called "ciphertext", uses this method to achieve the purpose of protecting data from illegal theft and reading! The functions that can encrypt data in PHP mainly include: crypt(), md5() and sha1(), as well as the encryption extension libraries Mcrpyt and Mash. In this article, we first introduce the use of crpyt() function for encryption!

2. Use the crypt() function for encryption-php encryption technology

Summary of encryption usage of php function crypt()

The crypt() function is one-way Encrypted, the ciphertext cannot be restored to plaintext, and the data after encryption is not the same every time. How to judge?

——This is the problem that the salt parameter is to solve. The crypt() function uses the salt parameter to encrypt the plain text. When judging, the output information is encrypted again using the same salt parameter, and the judgment is made by comparing the results after the two encryptions.

3. The code for using crypt() to implement user authentication in PHP

The crypt() function is defined as follows. string crypt (string input_string [, string salt])
Among them, the input_string parameter is the plaintext string that needs to be encrypted. The second optional salt is a bit string, which can affect the encrypted password and further eliminate the possibility of being cracked. possibility. By default, PHP uses a 2-character DES interference string. If the system uses MD5 (see the next section), PHP will use a 12-character interference string. The length of the interference string that the system will use can be found by executing the following command.
print "My system salt size is: ". CRYPT_SALT_LENGTH;

4. Use crypt() in PHP to implement user authentication

crypt() function returns String encrypted using DES, Blowfish, or MD5. This function behaves differently on different operating systems, and some operating systems support more than one algorithm type. At installation time, PHP checks what algorithms are available and what algorithms are used. Readers who have a little experience using non-Windows platforms may be quite familiar with crypt(). This function completes a function called one-way encryption. It can encrypt some plain codes, but it cannot in turn convert the password into The original plain code.

5. Function introduction of PHP function crypt()

The plain codes corresponding to these encrypted passwords in PHP function crypt() are kent and banner respectively. and parker. Pay attention to the first two letters of each password. This is because I used the following code to create the interference string based on the first two letters of the password. I will use Apache's password-response authentication configuration to prompt the user for their username and Passwords, a little-known fact about PHP is that it can recognize the username and password entered by Apache's password-reply system as $PHP_AUTH_USER and $PHP_AUTH_PW, which will be used in the authentication script.

Related Q&A

1. Questions about laravel5.2 openssl_encrypt()?

2. md5 - PHP: crypt() function. Is it possible that the encrypted password value is equal to the salt value?

3. How to implement the PHP crypt() function in Nodejs

[Related recommendations]

1. 【php encryption and decryption class library】10 php encryption and decryption class libraries download

2. php Chinese website free video tutorial: 《php.cn Dugujiu Cheap (4)-php video tutorial》

The above is the detailed content of Summary of encryption usage of php function crypt(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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