Simple implementation _php Example of PHP encryption technology

Source: Internet
Author: User
Tags base64 crypt md5 md5 encryption sha1 urlencode

First, MD5 encryption

Direct dry, here is an example of a login page:

 <?php require_once ' config/database.config.php ';
$act =$_request[' act '];
$username =$_post[' username '];

$password =md5 ($_post[' password '));
  if ($act = = ' reg ') {$sql = "INSERT into user (Username,password) VALUES (' {$username} ', ' {$password} ')";
  
  $result =mysqli_query ($link, $sql);
    if ($result) {echo "Success";
  echo "<meta http-equiv= ' refresh ' content= ' 1;url=login.html '/>";
    }else {echo "failure!";
  echo "<meta http-equiv= ' refresh ' content= ' 1;url=reg.html '/>";
  }}elseif ($act = = ' Login ') {$sql = "select * from user WHERE username= ' {$username} ' and password= ' {$password} '";
  $result =mysqli_query ($link, $sql);
  $validate =mysqli_fetch_array ($result);
  Var_dump ($validate);
    if ($validate) {echo "Success";
  echo "<meta http-equiv= ' refresh ' content= ' 1;url=http://www.baidu.com '/>";
    }else {echo "failure";
  echo "<meta http-equiv= ' refresh ' content= ' 1;url=login.html '/>"; }
}

The main thing is to remember to use MD5 when the time is right, so the password is stored in the database.

Mysql> SELECT * from user;
+----+----------+----------------------------------+
| id | username | password |
+----+----------+----------------------------------+
| 1 | 123 |   d41d8cd98f00b204e9800998ecf8427e |
| 2 | 123   | 5e12a8f9c9e959060fdcaea165393039 |
| 3 |     | | d41d8cd98f00b204e9800998ecf8427e |
| 4 | root   | 202cb962ac59075b964b07152d234b70 |
| 5 | Root   | 0c51f0ba4316a5c844397f69effe2d01 |
+----+----------+----------------------------------+

Second, crypt encryption algorithm

The same is a one-way encryption algorithm, can not be directly from the cipher text password (like MD5);

Syntax:string Crypt (String $str [, String $salt]), $str for encrypted plaintext, $salt as distractors, can be understood as salt and pepper noise;

/**
 * Crypt
/echo Crypt (' shit ');
echo " 
 

Basically nothing to say, on the one point, crypt encryption has a different encryption algorithm, the default is MD5 encryption, but if not given the "salt value", each refresh, is not the same result;

Then you can specify the salt value, see Manual, each algorithm's salt value length is different, such as the above Des and MD5, the results are as follows

1223B8C30A347321299611F873B449AD
$1$ed0.ph. $fPbfhSOMLyNdtZn 9krt8x/
im37cleo/jpaq
th12a1v7qcns.
$1$this is $Bu 9fe8y8ognibftjda4ez0

Des can only take two digits, while the MD5 takes 8 digits;

The usage is similar, remember to enter the salt value is good.

Third, SHA1

The same is one-way encryption , can not be cracked (but the online approach is similar to the database like the "pseudo-violence" cracked);

The difference with MD5 is to return a longer (40-bit) 16-binary string (MD5 is 32-bit);

/**
 * SHA1
/echo " 
 

Therefore, the general encryption to save, do not use these passwords alone, rather than this mixed use :

echo " 
 

is equivalent to the encryption outside and its own simple encryption!!

Four, URL encoding encryption

Encrypt the address bar information;

Bidirectional, UrlEncode and UrlDecode;

/**
 * URL Coded encryption * * *

$STR = "This is a test";
$result =urlencode ($STR);
echo $result;
echo " 
 

The results are as follows

This+is+a+test This is
a test
login.php%3fusername%3dshit%26action%3dact%253+hape%23123%5c
Shit Loginarray ([username] => Username&shit [gender] => male) 
shit login2array ([username] => Username& ; shit [gender] => male) 

So the function is: The Address bar is more secure, no longer clear text transmission, another solution to the special circumstances of the transmission

Five, Base64 coded encryption

In fact, Base64 is not encryption technology, but he will be the data for Base64 encoding, can also be seen as a cryptographic technology;

/**
 * Base64 * * *

$data = "I am King";
echo Base64_encode ($data);
echo " 
 

is equivalent to the content of the encryption;

Summarize

Single hash Encryption: the output of a fixed length is one-way;

Symmetric hash Encryption: Use the same key for encryption and decryption, can be calculated mutually; (Simple algorithm, high efficiency, small overhead, suitable for large amount of data encryption) DES, etc.

Asymmetric cryptography: The key is not the same, the public and private key points

The above PHP encryption technology is a simple implementation of small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.