decrypting RSA attachments from mail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acc427us
Forum Newbie
Posts: 4
Joined: Sun Feb 28, 2010 11:23 am

decrypting RSA attachments from mail

Post by acc427us »

I'm trying to decrypt email attachments that use an RSA keyy. Without the encryption, I can read the email and the attachments but when I throw in the encryption, I can't seem to decrypt them. I can read the encrypted in Exchange just not in my php scripts :( Thanks for any help!!

Acc427us

Here is the PHP function I'm using:

Code: Select all

 
<?php
function DeCrypt($dataStr) {
$decoded_source = base64_decode($dataStr);
  
  $cert = 'mycert.pem';
  $pass = 'mypasswd';
  $fp = fopen($cert,'r');
  $priv_key = fread($fp,8192);
  fclose($fp);
    $res = $privatekey = openssl_pkey_get_private ($priv_key, $pass);
  if ($res) {
     #openssl_private_decrypt(base64_decode($dataStr), $output, $privatekey);
     $resout = openssl_private_decrypt($decoded_source, $output, $privatekey);
     if ($resout) {echo "RESOUT:" . $resout . "\r\n";}
     echo "OUTPUT:" . $output . "\r\n";
     return $output;
  }
  else { return 'Key Invalid'; }
}
?>
 
 
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: decrypting RSA attachments from mail

Post by klevis miho »

Is base64 encoding a RSA?
acc427us
Forum Newbie
Posts: 4
Joined: Sun Feb 28, 2010 11:23 am

Re: decrypting RSA attachments from mail

Post by acc427us »

I've tried base64 decoding the attachment both before the openssl_private_decrypt and as part of it with no improvement. As listed her, I'm decoding the attachment soon as I get into the function. Then later I try to openssl_private_decrypt the attachment.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: decrypting RSA attachments from mail

Post by Weirdan »

This comment should be of some use to you: http://us2.php.net/manual/en/function.o ... .php#93849
acc427us
Forum Newbie
Posts: 4
Joined: Sun Feb 28, 2010 11:23 am

Re: decrypting RSA attachments from mail

Post by acc427us »

Anything specific? I've read it like a hundred times. Strrev would just reverse the string after I've decrypted it. I've tried all versions of padding.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: decrypting RSA attachments from mail

Post by Weirdan »

acc427us wrote: Strrev would just reverse the string after I've decrypted it.
It suggests to reverse the data before decryption. Also it says you cannot directly decrypt a string if its size is greater than the key size - you need to feed the data in chunks to this function. So as far as I can tell it should be something like this:

Code: Select all

 
$details = openssl_pkey_get_details($pkey);
$keysize = $details['bits'] / 8;
$ret = '';
foreach (chunk_split($data, $keysize) as $chunk) {
   $out = '';
   openssl_private_decrypt(strrev($chunk), $out, $pkey);
   $ret .= $out;
}
return $ret;
 
acc427us
Forum Newbie
Posts: 4
Joined: Sun Feb 28, 2010 11:23 am

Re: decrypting RSA attachments from mail

Post by acc427us »

I'll try that today. Thank you very much.
Post Reply