Home > Backend Development > PHP Tutorial > Detailed introduction to several methods of encryption in PHP development (pictures and texts)

Detailed introduction to several methods of encryption in PHP development (pictures and texts)

黄舟
Release: 2023-03-06 20:18:02
Original
1660 people have browsed it

This article mainly introduces relevant information summarizing several encryption methods in PHP development. Friends in need can refer to

1, using the crypt() function for encryption

crypt() function can perform single encryption. The specific syntax is as follows:

string crypt(string str[,tring salt])
Copy after login

where str is the string to be encrypted, and salt is the interference used in encryption. String, if the second parameter is omitted, an interference string will be randomly generated. The crypt() function supports four algorithms and lengths. The details are as follows:
Detailed introduction to several methods of encryption in PHP development (pictures and texts)

The sample code is as follows:

<?php 

$str ="I&#39;m jack!!!";
echo "加密前的str为:".$str."<br>";
$cryptStr =crypt($str);
echo "加密后的str为:".$cryptStr."<br>";

?>
Copy after login

The running results are as follows:

First run:


Detailed introduction to several methods of encryption in PHP development (pictures and texts)

The second run:


Detailed introduction to several methods of encryption in PHP development (pictures and texts)

The result of the third run:


Detailed introduction to several methods of encryption in PHP development (pictures and texts)

You can see that the results after each encryption are different. So how to judge the encrypted string? At this time, you will find that salt comes in handy. Ha ha. Let's demonstrate it through a piece of code:

<?php 

$str ="I&#39;m jack!!!";
echo "加密前的str为:".$str."<br>";
$cryptStr =crypt($str,"doc");
echo "加密后的str为:".$cryptStr."<br>";

?>
Copy after login

The running results are as follows:


Detailed introduction to several methods of encryption in PHP development (pictures and texts)

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

2, use the md5() function for encryption

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

string md5(string str[,bool raw_ouput])
Copy after login

where str is the plaintext to be encrypted. If the raw_output parameter is set to true, a binary ciphertext will be returned. The default is false.

3, use the sha1() function to encrypt

The syntax format is as follows:

string sha1(string str[,bool,raw_output])
Copy after login

str is required Encrypted plaintext, if raw_output is true, then a 20-bit binary number is returned. The default raw_output is false.

The above is the detailed content of Detailed introduction to several methods of encryption in PHP development (pictures and texts). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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