0

What is a way i can encode information into a binary file using BinaryWriter. Without anybody being able to see the information.

What i am creating needs to be able to load a binary file (& read it using BinaryReader) and interpret the data.

So for example i had a set of instructions like

op1
op2 variable
op3
op4 string "Hello World"

How can i put that and other files into a binary file without anybody seeing the data.

Steven
  • 109
  • 6
  • 2
    You may want to look at string encryption and decryption: http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net. – Jeremy McGee Nov 03 '11 at 21:51
  • 1
    Is your goal is to keep the data secret? If so, then you need to encrypt it. Binary is just a format that is designed for quickly and efficiently loading/saving objects, but it won't protect the data as anyone else could create a tool to read the data. If you need to keep the data secret, then you need to encrypt it as well. – AaronLS Nov 03 '11 at 21:52
  • Your question is unclear. What do you mean to achieve by 'encoding' files? What kind of information do you need to store? Do you wish to hide file or you don't want file to be readable in plain text? How do you mean interpret the data? Are you storing code you wish to execute? – Nikola Radosavljević Nov 03 '11 at 21:52

3 Answers3

2

Depends on how secure you need it to be.

I am using a zip-stream just to wave off curios onlookers....

EDIT: An example for an xml-file

public void save( String filename )
{
    XmlSerializer s = new XmlSerializer( this.GetType( ) );
    MemoryStream w = new MemoryStream( 4096 );

    TextWriter textWriter = new StreamWriter( w );

    s.Serialize( textWriter, this );

    FileStream f = new FileStream( filename, FileMode.CreateNew );
    DeflateStream zipStream = new DeflateStream( f, CompressionMode.Compress );

    byte[] buffer = w.GetBuffer( );
    zipStream.Write( buffer, 0, buffer.Length );

    zipStream.Close( );
    textWriter.Close( );
    w.Close( );
    f.Close( );
}

hth

Mario

Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
1

You will need to do a few steps. First, save the binary file, which it sounds like you've got a handle on. After the file has been written to disk, you will want to encrypt it. Check the answer to this question for instructions. When opening the file, you will decrypt it first, and then use your BinaryReader to populate your app state.

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72
0

Use serialization (BinaryFormatter class), your data won't be human-readable (although it's not strong level of protection); or use encryption (CryptoStream class).

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91