Php – Encrypt uploaded pdf files with mcrypt and php

encryptionfilesPHP

I'm currently set up with a CentOS box that utilizes mcrypt to encrypt/decrypt data to/from the database. In my haste, I forgot that I also need a solution to encrypt files (primarily pdf, with a xls and txt file here and there).

Is there a way to utilize mcrypt to encrypt uploaded pdf files? I understand the possibility of file_get_contents() with txt; is a similar solution available for other formats?

Thanks!

Best Answer

I know this question has an accepted answer but the cleanest way to do this would be to use an encryption stream filter. The page on the PHP manual has full details so I won't duplicate any code here but it seems to be the simplest way of achieving this. You attach a stream filter to the file handle resource and data is transparently encrypted or decrypted as it is read from or written to the file. Best of all this uses the mcrypt library to do all of this.