<aa*******@gmail.comwrote in message
news:bf**********************************@e25g2000 prg.googlegroups.com...
I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...
[...]
But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?
You are terribly wrong.
*All* files are always binary, meaning that they contain a sequence of
ones and zeroes. That is the only thing that a computer can store in a file.
When a program opens a file, it interprets those ones and zeroes, and does
with them whatever the program knows how to do. The BinaryWriter is dumping
into the file the same sequence of ones and zeroes that the String had in
memory. When you say that "you see the contents of the file", I assume that
you are not looking at the ones and zeroes yourself, but rather you are
using a program to open the file, such as Notepad. Notepad happens to
understand the same sequences of ones and zeroes that .Net uses to store the
strings in memory, so that is why you "see" clear text. But the file IS
binary.
Please suggest how can i write strings in binary format? (or in a
format that is not human understandable.....please note i dont want to
use database or encryption for this purpose)
You don't want a binary format. You want a format that is not
understandable to humans, which is a different thing. In general, no format
is directly understandable to most humans (although some of us would be able
to read a hex dump of an ASCII file with a little bit of effort). However,
humans don't look at the ones and zeroes of the file; they always use a
program to look at the contents of the file. So, basically, you want a
format that can't be understood by a program. This means that you want to
use encryption, or if you don't need security, at least a non-standard
encoding.
If you want to use a non-standard encoding, you can do it quite easily
in .Net by storing the string in a byte array (use
System.Text.Encoding.GetBytes) and then performing some operation with those
bytes, such as XORing a constant value to all of them. You then write the
bytes to the file using your BinaryWriter. This file will not be "readable"
in any obvious way, but someone who wants to devote some effort to the task
will be able to figure out how to decode it and see the contents. If you
want to be safe against such efforts, you will have to resort to
cryptoghraphy, which is available to your .Net program through the classes
in the System.Security.Cryptography namespace.