WeinForce WeinForce - 4 years ago 87
C# Question

Does File.ReadAllBytes work on word doc and docx?

I need to Encrypt a word file and to do so i need to not just read the text of the word file beacuse it may contain also images so i need to get the Word Doc or Docx content as bytes then encrypt them and then create a new file with a new extension say DocSec and write the bytes to this file.

My qestion is: is it possible to read the content of word doc\x with File.ReadAllBytes so it will take into consideration also images?

If not what is the recommended way to do so?

Answer Source

Of course you can with any file.

All kind of file are a sequence of bytes.

General guidelines

To encrypt steps are

  1. Open file (if is really big consider read() instead of readAllBytes())
  2. Use your favorite Algorithm to encrypt bytes read.
  3. Write new bytes into new file (ex newdoc.docsec).
  4. Close both file.
  5. Delete original file.

Now MS Word can't read anymore your file.

To decrypt

  1. Open your cript file (ex newdoc.docsec)
  2. Use your decrypt algorithm on read's byte.
  3. Write new bytes into new file (ex mydoc.doc).
  4. Close both file.
  5. Delete newdoc.docsec file.

Now you can open it with MS Word.

Full example on CodeProject File Encryption and Decryption in C#

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download