472,953 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,953 software developers and data experts.

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 11389
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Palmieri | last post by:
Im using md5 to encrypt and decrypt plain text, this works fine... When i try to run the same function on a binary file, it does not decrypt correctly. Is there a way to encrypt binary files...
0
by: chris | last post by:
I'm writing a small app to help me learn more about cryptography. All it does is encrypt all of the files in directory A, and put the encrypted versions of the files in directory B. It then...
7
by: Niyazi | last post by:
Hi, I developed an application and I am using SQL Server 2000 developer edition. I create my database and I have also created tbl_USER table. I have an ID, RealName, UserName, and UserPassword...
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
1
by: Pete | last post by:
I'm trying to a very simple encryption key generator. I have hard coded a 10 binary string into an array, I then want to permute that string using another array with element of type int for 1 to...
3
by: Benny Ng | last post by:
Dear all, The following is the source. The password is encrypted and saved into the Binary in SQL2K. Now I want to create a new page to compare the existed password and the password that in the...
4
by: Bob Cummings | last post by:
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...
29
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in...
6
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs =...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.