Tuesday, May 10, 2011

PHP Encryptions Tutorial

In this tutorial I'm going to teach you how to use some of the different PHP Encryptions available to us. There are quite a few ways to encrypt or change data using PHP so that the data can be inaccessible or very hard to decipher. I'll be showing you how to set up a very simple encryption process through a form so that you'll understand how PHP encryptions work.



The first step is to set up a form that will allow the user to enter some data. So create a new HTML file and within the body we'll enter:

 
<form action="query.php" method="post">                   

Enter a string: <input type="text" name="addc" />

<input type="submit" value="enter" />

</form>

 

The <form> creates an action towards "query.php"; this is where we'll be handling our PHP encryptions. It also uses the method POST, because the information sent to a PHP file using this method becomes invisible through the process so  other users can not see the data. Then we write what we'll want the user to see, so we write "Enter a string:" followed by an <input type> of text with a name of "addc". The "addc" name is important to remember because this is what we'll be using in our PHP file later to access what the user entered.
Then we close everything off with an <input type> of submit, which basically creates an enter button for the user to click and send their information to the PHP file.



So now that the HTML form is complete, we'll move on to the PHP file that will contain the encryption process for the data.

First create a new PHP file and name is "query.php" PHP scripts are always written between <?php and ?> The first  thing we'll have to do is access what the user entered, and we'll do this by writing:






$str = strip_tags($_POST["addc"]);







In PHP, variables are written using a dollar sign and a word, so in this case we'll use $str as our variable. _POST["addc"] is how we access what the user entered in the form. Before when we created our form we had used the method POST, and this allows us to get any information from the form, so in this case we want what the user actually entered into the text box. We gave the text box a name of "addc", so $_POST["addc"] gets whatever is entered in the textbox from the form in the HTML file. Then we enclose this within strip_tags() to strip any HTML or PHP tags the user may have entered. This is usually safe practice for safety and protection from people trying to access hidden information in your forms. Now let's get on to the different encryptions.

After the user hits submit, and they're taken to the "query.php" file, we'll still want them to see what they had entered in their form. So after we declare the variable above, the next thing we'll write is: (P.S. anything between /* */ is just a comment, so you could keep it in your PHP file if you want as commentary to help you better understand)

echo "You entered: " . $str; /* echo is how we output strings in PHP. The "." is a concatenation operator and it's used to put two string values together. */

echo "md5 Hash Form: " . md5($str); /* md5() is how we change our data into an md5 hash. We declared what the user entered by the variable $str, so all we do is write
md5($str) */


echo "SHA1 Algorithm: " . sha1($str); /* This encryption is used the same way as md5(), you just write sha1($str) and it will create a SHA1 hash of the string */

echo "String Shuffle Function: " . str_shuffle($str); /* str_shuffle() just shuffles around the data so it looks completely random */

echo "uuEncode Algorithm: " . convert_uuencode($str); /* this converts the string using the uuEncode Algorithm */

echo "ROT13 Encyrption: " . str_rot13($str); /* converts the data using a ROT13 function */

echo "How Many Words Counted: " . str_word_count($str); /* this function simply just tells the user how many words were entered in the text box in the form */

 
Then when all of the different encryption functions are written out, write this piece of code between each of them to give some spacing.  echo "<br /> <br />"; 

Some explanations on each of the encryptions:

The MD5 encryption is a widely used cryptographic hash function with a 128-bit hash value. MD5 is also very useful at checking the integrity of files. A few years after the creation of MD5, it was found to be less suitable for several applications and security measures. In recent years, many flaws were found with MD5 dealing with SSL certificates and
checksums of files.

The SHA1 encryption (Secure Hash Algorithm) is a cryptographic hash function designed by the National Security Agency. SHA1 is widely used and employed in several security
applications and protocols.

The uuEncode Algorithm works by converting all the data into a text file with only printable characters. uuEncode was very useful in the earlier days because it addressed the problem of sending binary data file through email.

The ROT13 encryption works by shifting every letter 13 places in the alphabet. It's a very simple encryption method that only works on letters, numeric and non-alphabetical characters will remain as they are entered.

You can check out an example of PHP encryptions here!

No comments:

Post a Comment