Home > Backend Development > PHP Tutorial > Encryption methods in php development

Encryption methods in php development

墨辰丷
Release: 2023-03-27 20:04:01
Original
1372 people have browsed it

This article mainly introduces relevant information on the summary of encryption methods in PHP development. Friends who need it 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, Salt is the interference string used in encryption. 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:
Encryption methods in php development

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:


Encryption methods in php development

Second run:


Encryption methods in php development

The result of the third run:


Encryption methods in php development

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:


Encryption methods in php development

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

2, use 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])
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 sha1() function for encryption

The syntax format is as follows:

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

str is the plaintext to be encrypted. If raw_output is true, then a 20-bit binary number is returned. The default raw_output is false.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

DES implemented by PHPEncryptionComplete method of decrypting the encapsulated class

Simple AES implemented in PHPEncryptionDecryption algorithm method

JS implementation 3des base64EncryptionDetailed explanation of decryption algorithm steps

The above is the detailed content of Encryption methods in php development. 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