Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Header of docx file

Posted on 2016-10-21
17
Medium Priority
?
192 Views
Last Modified: 2016-11-20
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" ?
0
Comment
Question by:DD 25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
  • +1
17 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41853429
"access the header of docx file" may be misleading.  This page https://support.office.com/en-us/article/Introduction-to-new-file-name-extensions-eca81dcb-5626-4e5b-8362-524d13ae4ec1 is a general description of the XML formats for Microsoft Office products.  The XML format documents are zipped and can contain more than one file including text and images.  If you view a DOCX file in a hex viewer, you will see the PK header followed often by a blank space and then one or more blocks of compressed data.

Attached is a sample of a DOCX file in hex view.
DOCX hex view
0
 
LVL 40

Expert Comment

by:evilrix
ID: 41853549
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.
0
 

Author Comment

by:DD 25
ID: 41853987
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41854149
After you encrypt the DOCX file, how do you plan to open it again?
0
 

Author Comment

by:DD 25
ID: 41854152
By decrypting it?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41854203
With what?  Are you going to distribute the decryption program so everyone has it?
0
 

Author Comment

by:DD 25
ID: 41854216
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.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41854258
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.
0
 

Author Comment

by:DD 25
ID: 41854324
That is being done, the connection is HTTPS secure.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41854525
#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;
}

Open in new window


note, there are also tools which would encrypt the docx file directly at the command line.

Sara
0
 

Author Comment

by:DD 25
ID: 41863414
@sarabande can you explain what you just did?
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 41865651
can you explain what you just did?

it is the c++ code for the first question you asked in your initial post:

 
I need to read the docx file, in char array, (c++) to perform AES encryption on it.

if you were looking for a c++ solution of your problem, you could put the code into an empty cpp file and create a new empty windows console c++ project where the code would be a starter.

Sara
0
 

Author Comment

by:DD 25
ID: 41886422
@sarabande the code you provided didn't work
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41887444
can you post your code and how you call it and what (error) messages/results you got?

Sara
0
 

Author Comment

by:DD 25
ID: 41895059
@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?
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41895117
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
0

Featured Post

Cyber Threats to Small Businesses (Part 1)

This past May, Webroot surveyed more than 600 IT decision-makers at medium-sized companies to see how these small businesses perceived new threats facing their organizations.  Read what Webroot CISO, Gary Hayslip, has to say about the survey in part 1 of this 2-part blog series.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question