This is a class which will encrypt the data into a numeric string. The encryption is not a straight forward one. It incorporates the ascii of the character does some mathemetical calculation over it using the key and then just returns the value (numerical value). This return value is then concatenated to give a numeric representation of the whole text. The decryption logic is a reverse application of the logic for encryption.
<?php
/** Class for encryption and decryption of the text. This is a simple class which returns the numeric encryption of the text.
**It can be further optimised to give better encryption output.
**Saji Nair
**/
class Encryption{
var $key;
var $text;
function Encryption(){
$this->key="SajIMTsENcRYptiOnKey"; /// key to encrypt with
}
/**Function to encrypt the text using the key.
** Returns numeric values for each character concatinated togather.
**Saji Nair
**/
function encrypt(){
$lenText=strlen($this->text);
$lenKey=strlen($this->key);
$str="";
$j=0;
for($i=0;$i<$lenText;$i++,$j++){
if($j==$lenKey){
$j=0;
}
$val=(ord($this->text[$i])*2)+ord($this->key[$j]);
$str.=$val;
}
return $str;
}
/**Function to encrypt the text using the key.
** Returns the text from the encrypted numeric value.
**Saji Nair
**/
function decrypt(){
// $this->text=explode("##",$this->text);
$temp=$this->text;
$temptext=array();
$templen=strlen($temp);
for($i=0,$j=0;$i<$templen;$i=$i+3,$j++){
$temptext[$j]=substr($this->text,$i,3);
}
$lenText=count($temptext);
$lenKey=strlen($this->key);
$str="";
saji nair wrote :1682
This is not a simple ascii conversion function. This piece of code converts text to numeric values irrespective of the length of data to be ciphered. If you try to convert the output you see for encrypt function into character you will get something different. Secondly this ciphered value can be deciphered only using the decrypting function. I have concatenated the numeric (ciphered) values of individual text. I had to stop at the numeric value of characters if you convert it to characters after ciphering then it gives you some characters which are not compatible with the databases like MySQL. So when I tried to use that with password encryption for MySQL I was not able to retrive the exact value from the database once the chphered value was inserted, so the comparision was failing. Thats why I have used the numeric values only and concatenated it directly.