writing to a binary file

  • Thread starter Thread starter Bob Cummings
  • Start date Start date
B

Bob Cummings

Good Day

I would like to write a password to a binary file. I am following along
in the book and examples I have found googling. However when I open
the file in notepad it looks like a text file. Can someone tell me what
I am doing wrong?

try
{
if (sfdCommon.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
System.IO.Stream fs = File.Create(sfdCommon.FileName);
System.IO.BinaryWriter bw = new BinaryWriter(fs);
char [] b;
b=this.txtPassWord.Text.ToCharArray();
bw.Write(b);
bw.Close();
fs.Close();
}
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show (ex.StackTrace);
}

cheers

Bob
 
Bob said:
Good Day

I would like to write a password to a binary file. I am following along
in the book and examples I have found googling. However when I open
the file in notepad it looks like a text file. Can someone tell me what
I am doing wrong?

try
{
if (sfdCommon.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
System.IO.Stream fs = File.Create(sfdCommon.FileName);
System.IO.BinaryWriter bw = new BinaryWriter(fs);
char [] b;
b=this.txtPassWord.Text.ToCharArray();

[ I don't think you should convert it to the char array, u really should
use a byte array.]
bw.Write(b);
bw.Close();
fs.Close();
}
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show (ex.StackTrace);
}

cheers
[Also, make sure you close bw, fs in case some exceptions happen, which
is not the case here.

Either use a finally block , or use using statement to enclose those blocks.
]
 
Bob said:
Good Day

I would like to write a password to a binary file.

In what format? What bytes do you want to be in your file when you've
written it? That should be the first thing to decide - after that, life
gets easier :)

Note that a "text file" is just a binary file which happens to contain
sensible text if you decode it using a suitable encoding.

Jon
 
Your code works fine, it just creates a binaryfile where all the bytes
happen to be valid ascii characters. You need to encrypt each byte
somehow before you write them out to make the file unreadable.

e.g. for encryption an average 5 year old could break, try replacing
bw.write(b) with:

for (int i=0; i < b.Length; i++)
{
char c = (char)((int)b - 20);
bw.Write(c);
}
 
Anthony said:
Your code works fine, it just creates a binaryfile where all the bytes
happen to be valid ascii characters. You need to encrypt each byte
somehow before you write them out to make the file unreadable.

e.g. for encryption an average 5 year old could break, try replacing
bw.write(b) with:

for (int i=0; i < b.Length; i++)
{
char c = (char)((int)b - 20);
bw.Write(c);
}



Got it thanks so much. For some I thought when I used to do this in C++
it was unreadable when opening the file stream ios::binary. Thanks for
5 year old encryption algorithm. This is for a charity event for a
college radio station. The students have devised their own alphabet
system for storing the cds on the shelf (depending on how the stars line
up sometimes G and O are the same letter <grin>). So this will be
sufficiently secure I am sure.
 
Back
Top