468,101 Members | 1,338 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

Binary encryption

Anyone know of an example/tutorial for encrypting a binary file?

I'm able to encrypt/decrypt simple text files, but anything more complicated
craps out.

Thanks
TomB
Nov 16 '05 #1
5 10947
I think there is a class for this in the .Net frame work. I haven't used it
but look in MSDN.
"TomB" <sh*****@hotmailXXX.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Anyone know of an example/tutorial for encrypting a binary file?

I'm able to encrypt/decrypt simple text files, but anything more complicated craps out.

Thanks
TomB

Nov 16 '05 #2

"TomB" <sh*****@hotmailXXX.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Anyone know of an example/tutorial for encrypting a binary file?

I'm able to encrypt/decrypt simple text files, but anything more
complicated
craps out.


It shouldn't matter what the contents are. What encryption method are you
using? Can you post a short, but complete program that shows what your
problem is?
Nov 16 '05 #3
Encrypt and Decrypt are my button names.

RijndaelEnhanced is from

http://www.obviex.com/samples/EncryptionWithSalt.aspx

Thanks

TomB

private void Encrypt_Click(object sender, System.EventArgs e)

{

Cursor.Current=Cursors.WaitCursor;

Encrypt.Text="Encrypting...";

Encrypt.Refresh();

string IV;

IV=(Password.Text+"01234567890123456").Substring(0 ,16);
RijndaelEnhanced re=new RijndaelEnhanced(Password.Text,IV);
StreamReader _sr=new StreamReader(FileName.Text);

byte[] byteArray=re.EncryptToBytes(_sr.ReadToEnd());

string encryptedText=Convert.ToBase64String(byteArray);

//string encryptedText=re.Encrypt(_sr.ReadToEnd());

StreamWriter _sw;

try

{

_sw=new
StreamWriter(EncryptedFileLocation.Text,false,Syst em.Text.Encoding.UTF8);
_sw.Write( encryptedText);

//_sw.Write(Convert.ToBase64String(byteArray));

_sw.Flush();

_sw.Close();

MessageBox.Show("Encrypted File saved as " +
EncryptedFileLocation.Text,"File
Encrypted",MessageBoxButtons.OK,MessageBoxIcon.Inf ormation);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

finally

{

Encrypt.Text="&Encrypt";

Cursor.Current=Cursors.Default;

}

}

private void Decrypt_Click(object sender, System.EventArgs e)

{
try

{

Decrypt.Text="Decrypting...";

Cursor.Current=Cursors.WaitCursor;

string IV;

IV=(Password.Text+"01234567890123456").Substring(0 ,16);
RijndaelEnhanced re=new RijndaelEnhanced(Password.Text,IV);
StreamReader _sr=new
StreamReader(EncryptedFileLocation.Text,System.Tex t.Encoding.UTF8);

string encryptedText=_sr.ReadToEnd();

string DecryptedText=re.Decrypt(encryptedText);

StreamWriter _sw=new
StreamWriter(DecryptedFileLocation.Text,false,Syst em.Text.Encoding.UTF8);

_sw.Write(Convert.FromBase64String(DecryptedText)) ;

//_sw.Write(DecryptedText);

_sw.Flush();

_sw.Close();

MessageBox.Show("Decrypted File saved as '" + DecryptedFileLocation.Text +
"'.","File Decrypted",MessageBoxButtons.OK,MessageBoxIcon.Inf ormation);

}

catch (Exception ex)

{

// I imagine an exception would occur if the password is incorrect

//MessageBox.Show(ex.Message);

MessageBox.Show("The file '" + EncryptedFileLocation.Text + "' could not be
decrypted. Please ensure you entered the correct password and filename",

"Unable to decrypt file",

MessageBoxButtons.OK,

MessageBoxIcon.Warning );

}

finally

{

Decrypt.Text="Decrypt";

Cursor.Current=Cursors.Default;

}

}

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2***************@TK2MSFTNGP11.phx.gbl...

"TomB" <sh*****@hotmailXXX.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Anyone know of an example/tutorial for encrypting a binary file?

I'm able to encrypt/decrypt simple text files, but anything more
complicated
craps out.


It shouldn't matter what the contents are. What encryption method are you
using? Can you post a short, but complete program that shows what your
problem is?

Nov 16 '05 #4
TomB <sh*****@hotmailXXX.com> wrote:
Encrypt and Decrypt are my button names.


<snip>

The problem is that you're treating the binary files as if they're text
files. Don't use a StreamReader - just read straight from the stream.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Ah. OK.

Thanks Jon, I'll give that a shot.

Thanks for your help.

TomB

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TomB <sh*****@hotmailXXX.com> wrote:
Encrypt and Decrypt are my button names.


<snip>

The problem is that you're treating the binary files as if they're text
files. Don't use a StreamReader - just read straight from the stream.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Palmieri | last post: by
reply views Thread by chris | last post: by
1 post views Thread by Pete | last post: by
4 posts views Thread by Bob Cummings | last post: by
29 posts views Thread by Harlin Seritt | last post: by
6 posts views Thread by aagarwal8 | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.