-3

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
WeinForce
  • 1,264
  • 2
  • 11
  • 17
  • 4
    I'm sorry, but if you have to ask this, you shouldn't be writing security-related software. The fact that a file, after you handle it, can't be opened in Word anymore doesn't mean your software is secure. There is no difference between "text bytes" and "image bytes" in a Word file, from a byte stream perspective. – CodeCaster Feb 05 '17 at 12:04
  • 1
    The simple direct answer here is "Yes". – H H Feb 05 '17 at 12:14
  • @CodeCaster "The fact that a file, after you handle it, can't be opened in Word anymore doesn't mean your software is secure." - Of course not, that is why i am also encrypting the content of the file. – WeinForce Feb 05 '17 at 12:15
  • @HenkHolterman you could have made it as an answer and get a V sign. – WeinForce Feb 05 '17 at 12:17
  • Too few details for a good answer. I would normally not use anything based on `byte[]` but a FileStream and an EncryptionStream instead. – H H Feb 05 '17 at 12:34

1 Answers1

1

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#

Marco Luongo
  • 397
  • 4
  • 13