Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Question updated below. –  gravyface May 20 '10 at 15:44
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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 2:22
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.