DD 25
asked on
Header of docx file
I need to read the docx file, in char array, (c++) to perform AES encryption on it. One idea I got from somebody else, was to actually access the header of docx file, and encrypt that header, instead of encrypting the complete content of the file. Because by doing so, the header will get encrypted and the file will become unreadable.
But I don't know, how to access the header of docx file. I know docx file is a zip format and first 2 bytes are PK, but how to specifically "access the header of docx file" ?
But I don't know, how to access the header of docx file. I know docx file is a zip format and first 2 bytes are PK, but how to specifically "access the header of docx file" ?
In terms of dealing with the compression format, I recommend using the libarchive. It's a general purpose archive and compression toolkit, which has support for all major formats, include zip. It's very easy to use.
http://www.libarchive.org/
>> ncrypt that header, instead of encrypting the complete content of the file. Because by doing so, the header will get encrypted and the file will become unreadable.
Unreadable with Word, maybe, but not unreadable with a hex editor or text editor. If your use case is to encrypt the content you need to encrypt all the content. That said, doesn't Work natively support encryption? Knowing your use case would really help.
http://www.libarchive.org/
>> ncrypt that header, instead of encrypting the complete content of the file. Because by doing so, the header will get encrypted and the file will become unreadable.
Unreadable with Word, maybe, but not unreadable with a hex editor or text editor. If your use case is to encrypt the content you need to encrypt all the content. That said, doesn't Work natively support encryption? Knowing your use case would really help.
ASKER
Then I should aim toward encrypting the whole docx file instead of just its header? But for that I need to read the complete file content in a the char array first?
After you encrypt the DOCX file, how do you plan to open it again?
ASKER
By decrypting it?
With what? Are you going to distribute the decryption program so everyone has it?
ASKER
See my scenario is, user can upload file on the server , where the AES is working, it can encrypt it and place it at third party location. Then if user wants it, it is downloaded from it onto server where it decrypts it and gives it to user.
Normally you would encrypt something to prevent others from seeing the data. I would never upload something if I wanted to keep it secret. Or encrypt it before uploading it. At the very minimum, make sure it is an HTTPS secure connection. The more valuable these files are, the more effort others will put into getting the info.
ASKER
That is being done, the connection is HTTPS secure.
#include <sys/stat.h>
#include <fstream>
#include <vector>
#include <errno.h>
int main(int nargs, char * szargs[])
{
if (nargs != 2) return -1;
struct stat fs;
if (stat(szargs[1], &fs)) != 0) return errno;
std::ifstream ifs(szargs[1], std::ios::binary | std::in);
std::vector<unsigned char> buf(fs.st_size);
if (!ifs.read((char*)&buf[0], fs.st_size) return errno;
ifs.close();
// coming here all bytes of file are read into buf
// you now may encrypt the buffer using a strong encryption tool
...
return 0;
}
note, there are also tools which would encrypt the docx file directly at the command line.
Sara
ASKER
@sarabande can you explain what you just did?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@sarabande the code you provided didn't work
can you post your code and how you call it and what (error) messages/results you got?
Sara
Sara
ASKER
@sarabande
The error I was receiving was from other cpp file I had in same project plus some typo in your code that was my mistake
But anyway, isn't your code just another way of doing this
ifstream myfile ("abc.docx", ios_base::binary);
If not, how is your code different?
The error I was receiving was from other cpp file I had in same project plus some typo in your code that was my mistake
But anyway, isn't your code just another way of doing this
ifstream myfile ("abc.docx", ios_base::binary);
If not, how is your code different?
ifstream myfile ("abc.docx", ios_base::binary);this will open the file if it existed but not read it.
using stat function allows to check existence and size of a file without opening it.
I need to read the docx file, in char array, (c++) to perform AES encryption on it.
actually my code was an answer to your question not more not less. if you would have asked further questions i surely would have answered.
note, you don't have to accept answers which are not a solution.
Sara
Attached is a sample of a DOCX file in hex view.