Page 1 of 1

File encryption with MCRYPT and MS Office 2007

Posted: Wed Jun 17, 2009 3:00 pm
by Jean-Louis
I've been using the following code for quite some time with no issue at all, until I had to use it for encrypting MS Office 2007 files (docx, xlsx, pptx...). The code creates an encrypted file, but once decrypted it can not be read by the Office application. Same issue with 128 or 256 bits.

Do you know how can that be? is there something peculiar in how to ...x files are built?

Thank so much for you help.

Jean-Louis




function file_encrypt($content, $key)
{
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$cryptedpass = mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_ECB, $iv);
return $content;
}


function file_decrypt($content, $key)
{
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decryptedpass = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_ECB, $iv);
return $content;
}

Re: File encryption with MCRYPT and MS Office 2007

Posted: Sat Jun 20, 2009 1:01 pm
by Jean-Louis
Thanks McInfo for your posting.

I added the encryption with base64 prior to the encryption with mcrypt and that did the trick.
Too bad that base64 add 30% to the file size but that's ok.

Thanks again for your suggestion.

Jean-Louis