Example of encryption usage of PHP encryption function-crypt () function

Source: Internet
Author: User
Before introducing the encryption function, let's first introduce the principle of data encryption: it is to process the original plaintext file or data according to a certain algorithm to make it a piece of unreadable code, it is usually called "ciphertext". In this way, we can protect data from illegal data theft and reading! PHP encryption function-crypt () function encryption

Before introducing the encryption function, let's first introduce the principle of data encryption: it is to process the original plaintext file or data according to a certain algorithm to make it a piece of unreadable code, it is usually called "ciphertext". In this way, we can protect data from illegal data theft and reading!

Data encryption functions in PHP include crypt (), md5 (), sha1 (), and Mcrpyt and Mash. In this article, IWe will first introduce how to use the crpyt () function for encryption!

Crypt () functionOne-way encryption can be completed, which is a one-way String hash!

The syntax format of the crypt () function is as follows:

string crypt ( string $str [, string $salt ] )
Algorithm Salt length
CRYPT_STD_DES 2-character (default)
CRYPT_EXT_DES 9-character
CRYPT_MD5 12-character (starting with $1 $)
CRYPT_BLOWFISH 16-character (starting with $2 $)

Here we want to explain:

By default, PHP uses one or two character DES interference strings. if the system uses MD5, it uses 12 characters, you can use the CRYPT_SALT_LENGTH variable to view the length of the currently used interference string!

Crypt () function instance usage:

Here we use an example to show you at a glance. the specific code is as follows:

 The encrypted str value is: ". $ atr1; // output the encrypted variable?>

The output result is as follows:

After the above instance is executed, refresh the browser and you will find that the encryption results generated each time are different. therefore, how to judge the encrypted data becomes a problem. The crypt () function is one-way encrypted, and the ciphertext cannot be restored to plain text, and the data after each encryption is different. this is the problem to be solved by the salt parameter.

The crypt () function uses the salt parameter to encrypt plaintext. when determining the plaintext, the same salt parameter is used to encrypt the output information again, and the two encrypted results are compared for determination!

The following example checks the user name. The code is as follows:

     
 

The output result is as follows:

In the next article, we will introduce how to use the MD5 () function for encryption. For more information, see PHP encryption function-md5 () function encryption.

The above is the details of the encryption usage instance of the PHP encryption function-crypt () function. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.