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)
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.
12 Comments
Trackbacks for this post
-
[…] 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 […]
Thanks a lot! This script help me…
No worries, glad it helped. Thanks
This code is a life saver.Thank you
No worries, hope its only for educational purposes though as its in no way secure.
cant you modify this with numbers and letters?
Can you elaborate on that? What do you mean?
cab we get the encrypted value in combination with letters special characters and numbers.
ex- 5Hfg&n0#
oh well yes you could. Though this method doesn’t do that. We could use the numbers to look-up ASCII values of and print that out.
how do I use the code after it is encrypted. Thanks in advance. kindly Post me the samples if it is possible.
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!
plzz give the html code for applying this algorithm at arjun.bansal3@gmail.com