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.

3
  • 2
    You may want to look at string encryption and decryption: stackoverflow.com/questions/202011/…. Commented Nov 3, 2011 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. Commented Nov 3, 2011 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? Commented Nov 3, 2011 at 21:52

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.