Page 1 of 1

i cant decrypt my encrypted text

Posted: Sun Jul 12, 2009 2:12 pm
by xiaomahe
Hai, i have been struggling for 2 days for decrypting the message that has been encrypted using public key. i am doing an email application whereby the message will be encrypted and decrypted later.

This is my encryption code

Code: Select all

if (isset($_POST["encrypt"]))
{
    $public = $_SESSION['xpublic'];
    $msg = $_REQUEST['emailmsg'];
    openssl_public_encrypt($msg, $crypttext, $public);
?>
I take the msg from <textarea> and then use it with public to encrypt, and the result is $crypttext. Then i display back the new encrypted text into the text area

Code: Select all

<tr>
    <td><textarea name="emailmsg" id ="emailmsg" rows="15" cols="90"><? echo $crypttext?></textarea></td>
    </tr>
The problem is, i failed to decrypt it when i open my mail in inbox. When the message is viewed, i moved the body content of the email to the text area:

Code: Select all

<tr>
    <td><textarea name="emailmsg" id ="emailmsg" rows="15" cols="90"><?php echo $body?></textarea></td>
    </tr>
and here is my decryption code

Code: Select all

<?php
if (isset($_POST["decrypt"]))
{
    $private = $_SESSION['xprivate'];
    openssl_private_decrypt($body, $decrypted, $private);
    echo $decrypted;
}
?>
technically it should work, but when i click decrypt button this error come out
Warning: imap_body() [function.imap-body]: Bad message number in /var/www/MySecureEmail/view.php on line 20
and this is how i called the body of my email msg

Code: Select all

<?php
$user = $_SESSION['xemail'];;
$password = $_SESSION['xpassword'];
$mail = '{mail.secure.sec:143/notls}INBOX';
$num = $_GET['num'];
$inbox = imap_open($mail, $user, $password) or die ("It was not possible to establish the imap connection.");
        
        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$num,0);
        /* output the email header information */
        $output.= $overview[0]->from;
 
 
// echo (imap_fetchheader($inbox,$num));
echo ("<BR><P>");
$body = imap_body($inbox,$num);
?>

Re: i cant decrypt my encrypted text

Posted: Sun Jul 12, 2009 3:20 pm
by requinix
Warning: imap_body() [function.imap-body]: Bad message number in /var/www/MySecureEmail/view.php on line 20
Does that look like it has anything to do with your decryption routine? Not to me.

Make sure your IMAP stuff is working correctly.

Re: i cant decrypt my encrypted text

Posted: Sun Jul 12, 2009 4:02 pm
by xiaomahe
thanks for a fast reply

I tried this application is a simple way. WHich is having 2 text area, and 2 button, encrypt and decrypt. First enter the text in the first text area, click the encrypt button, the first text area will be encrypted, next click the decrypt button, the decrypted text will appear in the next text area. but when i press decrypt, nothinng happened..

Code: Select all

<?php
 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="mahe"; //Mysql password
 
$db_name="mysecureemail"; // Database name 
 
 
// Connect to server and select databse.
$conn=mysql_connect("$host", "$username", "$password")or die("Can't connect."); 
mysql_select_db("$db_name");
 
$query = mysql_query("SELECT private FROM users where email='admin@secure.sec'");
$query2 = mysql_query("SELECT public FROM users where email='admin@secure.sec'");
$row = mysql_fetch_array($query);
$row2 = mysql_fetch_array($query2);
$private = $row['private'];
$public = $row2['public'];
 
 
if(isset($_POST["encrypt"]))
{
    $cleartext = $_REQUEST['emailmsg'];
    openssl_public_encrypt($cleartext, $crypttext, $public);
 
}
 
if(isset($_POST["decrypt"]))
{
    $crypttext = $_REQUEST['emailmsg'];
    openssl_private_decrypt($crypttext, $decrypted, $private);
 
}
 
?>
 
 
<form name="view.php" action="encrypt.php" method="POST">
<table border="1" align="center">
    
    <tr>
    <td><textarea name="emailmsg" id ="emailmsg" rows="15" cols="90"><?php echo $crypttext?></textarea></td>
    <td><textarea name="decryptmsg" id="decmsg" rows="15" cols="60"><?php echo $decrypted?></textarea>  </td>
    </tr>
 
</table>
 
 
<table border="0" align="center">
    <tr>
    <td align="center">
    <input type="submit" value="encrypt" name="encrypt">
    </td>
    <td align="center"><input type="submit" value="Decrypt" name="decrypt"></td>
    </tr>
    </table></form>
 
 
i am still new to php, so maybe there is something wrong in ISSET function or something else.. please do guide me.. thanks a lot