views:

53

answers:

2

I am inexperienced with symmetric encryption. I am encrypting a pdf file in php using the following code:

$source_filepath = RB::get('docroot') . RB::get('baseUrl') . '/submissions/' . $this->_filename;
$encrypted_filepath =  $source_filepath . '.nc';
$pdf_data = file_get_contents($source_filepath);
$encrypted_data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, '1234567812345678', $pdf_data, MCRYPT_MODE_ECB);
file_put_contents($encrypted_filepath, $encrypted_data);

Then I need to decrypt it outside of php, potentially using a desktop application/utility on another computer. I have attempted to decrypt the file with the aescrypt utility (http://www.aescrypt.com/) which tells me:

"Error: Bad file header (not aescrypt file or is corrupted? [c, fffffff0, fffffffe])"

as well as the mcrypt command which tells me: File thefile.pdf.nc was NOT decrypted successfully.

I have yet to be able to decrypt anything encrypted with encrypt outside of php. I have tried using blowfish and decrypting it with bcrypt (http://bcrypt.sourceforge.net/) as well with similar results. I suspect my ignorance of how encryption works is to blame but any help or education would be appreciated. Thanks.

+1  A: 

I am encrypting a pdf file in php using the following code:

The reason your getting an error from aescrypt is because the file you are writing has nothing to with the AEScrypt file format. Fortunately the developers of AESCrypt have provided very detailled instructions on their file format.

If after implementing that you're still having problems, then you could try the AESCrypt mailing list and failing that you could even contact the developers and offer to sponsor development of a cmpatible encryptor/decryptor in PHP.

symcbean
That could definitely be an option with a better timeline but in the mean time is there a canned utility that can understand files encrypted with php/mcrypt? I am particularly confused that the mcrypt commandline utility which allegedly utilizes the same library for the encryption algorithms cannot decrypt my files.
brandon
As pointed out by symcbean, it is not about encryption algorithms it is about file formats decided at the application level. For the most part these file formats are not standardized even though components of them may be.
GregS
A: 

I'm don't know PHP or mcrypt but just based on the calling line I suspect you could decrypt the file with the openssl command-line utility. The following example should close to what you need:

 openssl aes-128-ecb -d -K 303132333435363738303132333435363738 -iv 00000000000000000000000000000000 -P -nopad -nosalt -in cipherfile -out plainfile

NOTE: You'll have to strip off any padding yourself. mcrypt_encrypt apparently tacks on '\0' characters to pad the data to a multiple of the blocksize.

GregS