0

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!

stormdrain
  • 1,439
  • 7
  • 28
  • 52

3 Answers3

1

EDIT

Probably the cleanest (and more versatile/supported) option would be to use the Zend_File_Transfer library (you can use the Zend Framework's individual libraries on their own or in combination, if you're not familiar with it).

Specifically, have a look at the Filters for the Zend_File_Transfer lib. It uses MCrypt for it's encryption/decryption filters (and has alot of other must-have/nice-to-have features like renaming, progress bar, etc.).

I've used it for basic uploading before and it works great.

gravyface
  • 13,957
  • 19
  • 68
  • 100
  • I'm sorry I wasn't clear. I need to be able to encrypt files being uploaded, not generated. After looking at TCPDF documentation, I'm not clear on whether or not it can do this (it looks more like it is for generating pdf rather than working with existing pdf files)? It'd also be nice if I could integrate with mcrypt as I could use the current key management system and it would hopefully be portable to other formats (xls, doc, etc). Much thanks for the reply! – stormdrain May 20 '10 at 02:22
1

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.

Jeremy
  • 126
  • 3
  • I actually ended up using mcrypt and the file library included in the framework I'm using to do essentially the same thing the filter you linked to does. However, next time I need something similar, I will follow your suggestion--thanks! I've changed the accepted answer to yours because as of today, it is the most correct. – stormdrain Mar 17 '11 at 13:14
0

If chain file_get_contents -> mcrypt_encrypt -> mcrypt_decrypt -> file_put_contents doesn't work on PDF files (why shouldn't be), you can still treat it like a binary data, not text.

Or, in more systematic way, use EncFS and create encrypted mount point and store documents there.

mkudlacek
  • 1,677
  • 1
  • 11
  • 15