FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free 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";
?>
korndevil666
how could you use this in a form? rather than typing input=blahblah in the title bar?


thanks,
Gushe
Just a random question, but why would anyone use a simple to decipher encryption like this?
If you want to add some security, you should use something that at least is a little harder to decrypt. Add some rotating, something involving a keyword or Caesar Ciphering to it. Wink

This way it's quite obvious that you're jst taking the numeric value of each character.
Code:

<?php

function encrypt_string($input) //1.
{
   
    //2.
    $inputlen = strlen($input);
    $randchar = rand(1, 15); // Works only for aplhanumeric input.

    //3.
    $i = 0;
    while ($i < $inputlen)
    {

        $inputchr[$i] = (ord($input[$i]) - $randchar);
        $i++;
    }
 

    //4.
    $encrypted = implode('.', $inputchr) . '.' . (chr($randchar) + 50);
    return $encrypted;
}

$input = $_GET["input"];
$encrypted = encrypt_string($input);
echo "<b>Encrypted:</b> $encrypted";
?>


This will every time generate a random encryption rotation. To be able to easily decrypt it this random value is sticked onto the end of the encrypted string.
It's up to you to create the decryption alogythm for this one. Wink Who'll be the first?

[PS: Not tested; might have a little bug in it. Please point me to that. Smile]

Cheers,
~ GuShe.
flatliner
Gushe wrote:

It's up to you to create the decryption alogythm for this one. Wink Who'll be the first?

[PS: Not tested; might have a little bug in it. Please point me to that. Smile]

Cheers,
~ GuShe.



Ha that was fun Laughing Laughing I dont know I think its bit messy the way I did it, but it does the job, Oh and there was an error in your encryption alogthrim in one place you used chr() instead of ord() and the randkey was always 50. I change it and it worked fine.

Modifted Encryption Algothrim
Code:
<?php

function encrypt_string($input) //1.
{
   
    //2.
    $inputlen = strlen($input);
    $randchar = rand(1, 15); // Works only for aplhanumeric input.

    //3.
    $i = 0;
    while ($i < $inputlen)
    {

        $inputchr[$i] = (ord($input[$i]) - $randchar);

        $i++;
    }
 

    //4.
    $encrypted = implode('.', $inputchr) . '.' . (ord($randchar)+50);
    return $encrypted;
}

$input = $_GET["input"];
$encrypted = encrypt_string($input);
echo "<b>Encrypted:</b> $encrypted";
?>




Decryption Algothrim
Code:

<?php

function decrypt_string($input)
{
   
    $input_count = strlen($input);
    $x = $input_count-2;
    $randchar = substr($input, $x, $input_count);// finds the randkey

   $dec = explode(".", $input);// splits up the string to any array
       
   $x = count($dec)-2;// -2 so it doesnt count the randkey
   $i = 0;
   
    while ($i <= $x)
   {
         
      $array[$i] = $dec[$i]+$randchar+2; // messy
      $real .= chr($array[$i]); //The actual decryption
   
      $i++;
   };
   

$input = $real;
return $input;
}

$input = $_GET["input"];
$decrypted = decrypt_string($input);
echo "<b>Decrypted:</b> $decrypted";
?>
TomS
I use this code to protect the messages of my pm-system.

Code:
function encode($key, $text)
{
    $l_k = strlen($key);
    $l_t = strlen($text);
   
    if($l_k == 0) return $text; // No encryption without key.
   
    $encoded = "";
    $k = 0; // Position im Key
    for($i=0; $i<$l_t; $i++)
    {
        if($k > $l_k) $k = 0;
        $encoded .= chr(ord($text[$i]) ^ ord($key[$k])); // encryption
        $k++;
    }
    return $encoded;
}

function decode($key, $chiffre)
{
    $l_k = strlen($key);
    $l_t = strlen($chiffre);
   
    if($l_k == 0) return $text; // No decryption withouz key.
   
    $decoded = "";
   
    $k = 0; // Position im Key
    for($i=0; $i<$l_t; $i++)
    {
        if($k > $l_k) $k = 0;
        $decoded .= chr(ord($chiffre[$i]) ^ ord($key[$k])); // decryption
        $k++;
    }
   
    return $decoded;
}

$encoded = encode("key", "message");
$decoded = decode("key", $encoded);
Related topics
MD5 Hashing
for your comments
php mysql and site management tools
learning php
simple site layout
INSTALL PHP
how do i make a login page
Prices for Websites
PHP Help entering values from a text form into a table
PhP install error
.: User login systems for your site :. CHEAP AND EASY!
Web Design XHTML/CSS
Waiting to begin
[close please]Turn on register_globals for PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

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