PHP cryptographic function-crypt () function Encryption usage instance

Source: Internet
Author: User
Tags crypt sha1
PHP Cryptographic function-crypt () function Encryption

Before introducing cryptographic functions, let us first introduce the principle of data encryption: that is, the original Ming file or data in accordance with an algorithm for processing, making it an unreadable piece of code, often referred to as "ciphertext", through such a way to achieve the purpose of protecting data from illegal stealing and reading!

The main functions that can encrypt data in PHP are: Crypt (), MD5 (), and SHA1 (), as well as the encryption extension mcrpyt and mash. In this article, we first introduce the use of the CRPYT () function for encryption!

crypt () function can complete one-way encryption function, is a one-way string hash!

The syntax format for 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 /td> 16-character (starting with $2$)

Here to illustrate:

By default, PHP uses one or two-character DES interference strings, and if the system is using MD5, then 12 characters are used, and the length of the interfering string currently used can be viewed by crypt_salt_length variable!

Crypt () Function instance usage:

Below we use an example, let everyone at a glance, the specific code is as follows:

<?phpheader ("content-type:text/html; "Charset=utf-8"); $atr = "php Chinese network www.php.cn";  Declare the string variable $atrecho "before encrypting the value of ATR:". $atr; $atr 1 = crypt ($ATR);              The value of STR after encrypting echo "<br> encryption for variable $STR is:". $atr 1; Output encrypted variable?>

The results of the output are as follows:

After the above instance has been executed, the browser has been refreshed, and you will find that each generation of encryption results are different, then how to judge the encrypted data becomes a problem. Crypt () function is one-way encryption, ciphertext can not be reduced to clear text, and every time after the encryption of the data are different, this is the salt parameter to solve the problem.

The crypt () function uses the salt parameter to encrypt the plaintext, and when it is judged, the output information is encrypted again using the same salt parameter, which is compared with the result of two times encryption to judge!

The following example checks the input user name with the following code:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $link =mysqli_connect (" localhost "," root "," "), $db _selected = mysqli_select_db ($link," my_db "); >    <form name= "Form1" action= "" method= "post" >    <input type= "text" name= "username" id= "username" Size= ">    <input type=" Submit "name=" submit "value=" detect ">    </form><?phpif (isset" Username "]) =" ") {     $usr =crypt (isset ($_post[" username "])," TM ");             Encrypt the user name    $sql = "SELECT * from tb_user where user = '". $usr. "'"; /Generate query statement    $rst = Mysqli_query ($link, $sql);                          Executes the statement, returning the result set    if ($rst) {        echo "user name exists";    } else{        echo "User name can be used";    }}? >

The output results are as follows:

Next we will show you how to use the MD5 () function for encryption, please read the PHP cryptographic function-md5 () function Encryption!

"Recommended"

1. Related topics: "PHP cryptographic Functions"

2.PHP cryptographic function-md5 () function Encryption instance usage

3.PHP cryptographic function-sha1 () function Encryption instance usage

4.PHP encryption Technology Video tutorial

Related Article

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.