Page 1 of 1

decrypting RSA attachments from mail

Posted: Sun Feb 28, 2010 11:32 am
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'; }
}
?>
 
 

Re: decrypting RSA attachments from mail

Posted: Sun Feb 28, 2010 4:49 pm
by klevis miho
Is base64 encoding a RSA?

Re: decrypting RSA attachments from mail

Posted: Tue Mar 02, 2010 9:05 am
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.

Re: decrypting RSA attachments from mail

Posted: Tue Mar 02, 2010 11:50 am
by Weirdan
This comment should be of some use to you: http://us2.php.net/manual/en/function.o ... .php#93849

Re: decrypting RSA attachments from mail

Posted: Tue Mar 02, 2010 9:23 pm
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.

Re: decrypting RSA attachments from mail

Posted: Wed Mar 03, 2010 12:33 am
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;
 

Re: decrypting RSA attachments from mail

Posted: Wed Mar 03, 2010 9:17 am
by acc427us
I'll try that today. Thank you very much.