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.

[ad#Adsense_inLine_336*280]

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.

About the Author

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.

Visit Website

12 Comments

  1. Andrés Reyes Galgani November 4, 2011 Reply

    Thanks a lot! This script help me…

  2. This code is a life saver.Thank you

  3. cant you modify this with numbers and letters?

  4. thilip kumar June 26, 2012 Reply

    how do I use the code after it is encrypted. Thanks in advance. kindly Post me the samples if it is possible.

  5. Arie Putranto July 24, 2012 Reply

    WordPress doesn’t allow me to use base64_encode and was looking for alternative way to encrypt something. Your code works like a charm. Kudos!

  6. plzz give the html code for applying this algorithm at arjun.bansal3@gmail.com

Trackbacks for this post

  1. […] This post was mentioned on Twitter by Flax.ie, Ciarán McCann. Ciarán McCann said: I just posted an article on a simple PHP Encryption and Decryption algorithm, with source and live demo http://t.co/oFr2IUf […]

Leave a Comment

Your email address will not be published. Required fields are marked *

*