FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Php - Basic Encryption site

 


luiz_a2web
i'm sick of searching for PHP tutorials and never finding a good one on encryption. Each one I find uses some one way hash (such as MD5, Sha-1, crypt, etc.). In this tutorial, I will explain how to create an extremely simple, easy to decipher encryption. You're going to have to use your creativity to create it more secure (I'll list a few examples).

Step 1
Start out by creating two PHP files. One titled encryption.php, the other titled decryption.php.

Step 2
Let's start out on the encryption page. Copy the following code, and I will give a run-down on each segment.

Encryption Page (encryption.php)
Code:
<?php

function encrypt_string($input) //1.
{
   
    //2.
    $inputlen = strlen($input);

    //3.
    $i = 1;
    while ($i <= $inputlen)
    {
        $inputchr .= chr(substr($input, $i, 1));
        if ($i < $inputlen)
        {
            $inputchr .= ".";
        }
        $i++;
    }

    //4.
    $encrypted = "$inputchr";
    return $encrypted;
}


Whew! That was pretty confusing. Let's check out what it all means.

1. We start out by creating a function.

2. This is where we define out variables. The function strlen checks the length of the string (in our case, "$input").

3. This is a while loop. This little segment goes through each letter in "$string" one by one, encrypting it, and adding it to the rest of the newly encrypted string. More specifically, it translates each character in the string to it's ASCII value, and adds each letter to a string, separated by a period.

4. Now we are setting "$encrypted" to our newly encrypted string, and returning it. We can now display our encrypted string by using the function we just made. I'll show you how:

Paste this code below the function we just made.

Code:
Encryption Page Cont. (encryption.php)
$input = $_GET["input"];
$encrypted = encrypt_string($input);
echo "<b>Encrypted:</b> $encrypted";
?>


If you entered this correctly, when you host this file and visit the page like this: encryption.php?input=hello, it should display your encrypted text. Here is an example of how it should output: Encrypted: 104.101.108.108.111

Note: Remember to save this as encryption.php!

Step 3
Let's make our decryption page. Copy the script below. I'm confident most of it is self explanatory as long as you already have a basic understanding of PHP.

Decryption Page (decryption.php)

Code:
<?php

function decrypt_string($input)
{

$dec = explode(".", $input); //Sends each ASCII value to the array $dec
$a = 0;
$b = count($dec); //Counts how many values are in the array $dec

while ($a < $b)
{
$real .= chr($dec[$a]); //The actual decryption
$a++;
}

$input = $real;
return $input; //Returns the value

}


This is our decryption function. Looks pretty similar to the encryption one, eh? This time I've commented a few lines rather than explaining everything, because most of it is similar to the encryption section.

Once again, paste the following code below the function we just made.

Code:
Decryption Page Cont. (decryption.php)
$input = $_GET["input"];
$decrypted = decrypt_string($input);
echo "<b>Decrypted:</b> $decrypted";

?>


Voila! You have learned how to create an extremely simple encryption system.

if need help....post
Naif
I tried your code. On executing it with the string "hello" or anyother string for that matter, I am getting an output of ".....". After going through the code I realised you didnt do what you were trying to do. The chr() function returns a string from an ASCII value. The value being passed is already an string. The function to be used here is ord(). Example: ord("A") would return an integer value of 65. There were some other errors too which I have corrected. Here is the modified code:

Code:

<?php

function encrypt_string($input) //1.
{
   
    //2.
    $inputlen = strlen($input);

    //3.
    $i = 0; // Changed value of $i from 1 to 0
    while ($i < $inputlen) // Changed <= to <
    {
        $inputchr .= ord($input{$i});
        if ($i < $inputlen)
        {
            $inputchr .= ".";
        }
        $i++;
    }

    //4.
    $encrypted = "$inputchr";
    return $encrypted;
}

$input = $_GET["input"];
$encrypted = encrypt_string($input);
echo "<b>Encrypted:</b> $encrypted";
?>
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.