About the encryption problem in PHP development

Source: Internet
Author: User
Tags crypt sha1
This article mainly introduces the PHP development of several methods to summarize the relevant information, the need for friends can refer to the following

1, using the Crypt () function for encryption

The crypt () function can be individually encrypted with the following syntax:

String crypt (string str[,tring Salt])

Where Str is the string to be encrypted, the salt is the interference string used for encryption, and if the second parameter is omitted, a disturbance string is randomly generated. The crypt () function supports four algorithms and lengths. Specifically, the following table:

The sample code is as follows:

<?php $str = "I m jack!!!"; echo "str before encryption:". $str. " <br> $cryptStr =crypt ($STR); echo "Encrypted str:". $cryptStr. " <br> ";? >

The results of the operation are as follows:

Run for the first time:


Second run:


Results of the third run:


You can see that the results are different after each encryption. So how to judge the encrypted string, this time you will find that salt will come in handy. Ha ha. Here's a code to illustrate the following:

<?php $str = "I m jack!!!"; echo "str before encryption:". $str. " <br> "; $cryptStr =crypt ($str," Doc "); echo" Encrypted str: ". $cryptStr." <br> ";? >

The results of the operation are as follows:


You will find that no matter how many times the encryption string is run, we can judge the encrypted string.

2, using the MD5 () function for encryption

The MD5 () function uses the MD5 algorithm. The syntax format is as follows:

String MD5 (String Str[,bool raw_ouput])

Where Str is the plaintext to be encrypted, the Raw_output parameter is set to True to return a binary ciphertext, which defaults to false.

3, using the SHA1 () function for encryption

The syntax format is as follows:

String SHA1 (String Str[,bool,raw_output])

STR is the plaintext to be encrypted, Raw_output returns a 20-bit binary number if true. The default raw_output is false.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.