<!--
This script encrypts and stores the message in the encrypted form in the database.
This can also be implemented as a function.
Be sure to Change the queries according to the fields in ur databse. But itz mandatory to include
the ivec field which is the ini vector for the encryption algorithm.
The backend CREATE is as follows:
CREATE TABLE `messenc` (
`uid` text NOT NULL,
`dom` text NOT NULL,
`ext` text NOT NULL,
`encmess` longtext NOT NULL,
`ekey` text NOT NULL,
`iv` varchar(100) NOT NULL default '',
) TYPE=InnoDB;
Variable Docs:
$ekey is the encryption key given by user
$to - Is the mail id ex:veevakan
Anybody having problems with this script can mail me with the correct error message.
Put the subject line as "MessEncrypt :Bug Mail "
My @ is : veevakan@email.com
The class implementation for all the databases is on itz way
<HTML>
<TITLE>
Text Encrypted
</title>
<body text="Lucida Console" size="4">
<?
$key=$ekey;
$add=$to;
$domai=$dom;
$exte=$ext;
$ciph=$cipher;
print($ciph);//DEBUG LINE
$iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB), MCRYPT_RAND);// gets the initialization vector for MCRYPT_RIJNDAEL_256 algorithm .for more algorithms look refer the PHP Manual.
$cryptext=mcrypt_encrypt (MCRYPT_RIJNDAEL_256,$key, $ta1, MCRYPT_MODE_ECB, $iv); //The message is encrypted here
$connect=mysql_connect("127.0.0.1", "uname","mysqlpassword");
mysql_select_db("user_reg"); //This is the database which contains the messenc table
if($connect!=0)
{
$query="SELECT encmess FROM messenc WHERE uid='$add' and dom='$domai' and ext='$exte' and ekey='$key'";
$res = mysql_query($query); //Checks whether the message already exists in the table
if($res)
{
$row=mysql_fetch_assoc($res);
if(mysql_num_rows($res)>0)
{
$ecmess=$row["encmess"]
if(strcmp($ecmess,$cryptext)!=0) // Checks the message from table and the encrypted message
{
$query1="INSERT INTO messenc(uid,dom,ext,encmess,ekey,ivec)"." VALUES('$add','$domai','$exte',"."'$cryptext','$key','$iv')"; // If the message is new ,then inserted into table
$quok=mysql_query($query1);
if($quok!=false)
{
print("<BR><BR> Done");
print($iv);
}
else
{
print("Error during DB operations");
}
}
else
{
print("<BR><B>Message11 already inserted</b><BR>"); //DEBUG LINE
}
}
else
{
print("<BR><B>Message12 already inserted</b><BR>");//DEBUG LINE
}
}