I am using CSecurityManager to encrypt-decrypt files that i store in a MySQL database. It works fine on most files EXCEPT for docx files. For docx files, the encryption and database storage works, However, when i retrieve the file and decrypt, the file turns out corrupted. Any ideas on this?
Here’s a snippet of my code:
encryption:
public function beforeSave(){
//set the encryption key
$securityManager = new CSecurityManager;
$securityManager->setEncryptionKey(Yii::app()->params['fileEncryptKey']);
if($file=CUploadedFile::getInstance($this,'uploadedFile')){
$this->fileName = $file->name;
$this->fileType = $file->type;
$this->fileSize = $file->size;
$this->fileContent = $securityManager->encrypt(file_get_contents($file->tempName));
}
return parent::beforeSave();
}
Maybe its because the sendFile() method (used without mime type parameter) uses the mime type defined in the mimeTypes.php (in sendFile the CFileHelper::getMimeTypeByExtension() method). In the mimeTypes.php file there is no entry defined for docx so it sets the mime type to text/plain in sendFile().
I don’t be sure but one possible solution in my opinion could be setting the correct mime type as third parameter in the sendFile() call.
Maybe it should be added as feature/bug request to add this and maybe other missing mime types to the mimeTypes.php file.
Update: I submitted this as new issue 3210 on code.google.com
What if you try to base64_encode() the file contents before encrypt, then when you get the content from database, decrypt it, then base64_decode() and see if it works that way.