PHP – Simple Encryption and Decryption algorithm

php

PHP – Simple Encryption and Decryption algorithm

Once again I have found an old PHP script which I wrote when I was first getting into PHP programming a few years ago. I came across a thread with a tutorial on how to write a very simple encryption and decryption algothrim, with out using MD5 or anything like that.

I read on down through the thread and a poster there had posted a modified encryption algorithm that was some what better then the original posters.Though the user did not post a decryption function for it.I personally never had used or seen any type of custom encryption methods outside the use of things like MD5, Sha-1, crypt, etc. So I thought it would be fun to write the decryption function for this user’s encryption function. Below is the encrypt and decrypt functions.

(Disclaimer this code was written almost 3 years ago, thus bad naming of variables and practices etc)

 

Live demo

 

Encryption Function

function encrypt_string($input)
{

    $inputlen = strlen($input);// Counts number characters in string $input
    $randkey = rand(1, 9); // Gets a random number between 1 and 9

    $i = 0;
    while ($i < $inputlen)
    {

        $inputchr[$i] = (ord($input[$i]) - $randkey);//encrpytion 

        $i++; // For the loop to function
    }

//Puts the $inputchr array togtheir in a string with the $randkey add to the end of the string
    $encrypted = implode('.', $inputchr) . '.' . (ord($randkey)+50);
    return $encrypted;
}

$input = $_GET["input"];// encryption.php?input=data-I-want-to encrpyt
$encrypted = encrypt_string($input);
echo "Encrypted: $encrypted";

Decryption Function

function decrypt_string($input)
{
  $input_count = strlen($input);

  $dec = explode(".", $input);// splits up the string to any array
  $x = count($dec);
  $y = $x-1;// To get the key of the last bit in the array 

  $calc = $dec[$y]-50;
  $randkey = chr($calc);// works out the randkey number

  $i = 0;

   while ($i < $y)
  {

    $array[$i] = $dec[$i]+$randkey; // Works out the ascii characters actual numbers
    $real .= chr($array[$i]); //The actual decryption

    $i++;
  };

$input = $real;
return $input;
}

$input = $_GET["data"];
$decrypted = decrypt_string($input);
echo "Decrypted: $decrypted";

It is NOT recommend you use this code for transferring sensitive data as it can be easily decrypted. This code is only for example and educational propose.


Ciarán McCann

Flax Project Founder - Ciarán McCann is an extremely passionate and motivated programmer who has been programming for about 4 years now. Currently attending Carlow I.T studying computer games development. He has experience in many different programming languages, markup languages and general technologies. His role in the Flax Project is as a blogger/Web Designer and Flax Engine programmer. Please excuse any bad grammar/spelling, I am a bit on the Dyslexic side. Follow me on Twitter for info on what I am working on.


Related posts:

  1. PHP algebraic equation function

8 Responses to “PHP – Simple Encryption and Decryption algorithm”


Leave a Reply