n.r.
n.r.

Reputation: 929

How to open / decrypt a password protected word-file (docx) with PHP

I am trying to open a word-file (.docx) programmatically with PHP. This word-file is protected and encrypted with a password so it's content cannot be display without providing the password. Of course I do know the password.

But how can I manage to open this word-file with PHP by providing the password?

The only PHP-library that at least mentions password protection is the great PhpOffice/PhpWord - but it does not support opening password protected files: https://github.com/PHPOffice/PHPWord/issues/357

Please note that there is a slight difference between password protection against unwanted changes and - this is my case - to protect it against unauthorized opening.

TIA

Upvotes: 2

Views: 1889

Answers (1)

Jay Dadhania
Jay Dadhania

Reputation: 404

I welcome you to check out my PHPDecryptXLSXWithPassword repo.

First decrypt the file with password, and then use the decrypted file with PHPWord.

Here is an example:

require_once('PHPDecryptXLSXWithPassword.php');

$encryptedFilePath = '../path/to/encrypted/file.docx';
$password = 'mypassword'; // password to "open" the file
$decryptedFilePath = '../path/to/decrypted/file.docx';

decrypt($encryptedFilePath, $password, $decryptedFilePath);

// now you can use PHPWord with $decryptedFilePath

Note: This is an experimental code, so use with caution. DO NOT use in production!

Upvotes: 1

Related Questions