Home  >  Article  >  Backend Development  >  Encryption for php development

Encryption for php development

WBOY
WBOYOriginal
2016-08-08 09:26:32804browse

1, use crypt() function for encryption
The crypt() function can perform single encryption. The specific syntax is as follows:
string crypt(string str[,string salt])
Among them, str is the string to be encrypted, and 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:

The sample code is as follows:

<code><span><span><span><?php</span><span>$str</span> =<span>"I'm jack!!!"</span>;
<span>echo</span><span>"加密前的str为:"</span>.<span>$str</span>.<span>"<br>"</span>;
<span>$cryptStr</span> =crypt(<span>$str</span>);
<span>echo</span><span>"加密后的str为:"</span>.<span>$cryptStr</span>.<span>"<br>"</span>;

<span>?></span></span></span></code>

The running results are as follows:
First run:

Second run:

Results of the third run:

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:

<code><span><span><?php</span><span>$str</span> =<span>"I'm jack!!!"</span>;
<span>echo</span><span>"加密前的str为:"</span>.<span>$str</span>.<span>"<br>"</span>;
<span>$cryptStr</span> =crypt(<span>$str</span>,<span>"doc"</span>);
<span>echo</span><span>"加密后的str为:"</span>.<span>$cryptStr</span>.<span>"<br>"</span>;

<span>?></span></span></code>

The running results are as follows:

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 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. 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])
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 has introduced encryption in PHP development, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:Write Session to MemcacheNext article:Write Session to Memcache