473,789 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11462
I think there is a class for this in the .Net frame work. I haven't used it
but look in MSDN.
"TomB" <sh*****@hotmai lXXX.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.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*****@hotmai lXXX.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.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.

RijndaelEnhance d is from

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

Thanks

TomB

private void Encrypt_Click(o bject sender, System.EventArg s e)

{

Cursor.Current= Cursors.WaitCur sor;

Encrypt.Text="E ncrypting...";

Encrypt.Refresh ();

string IV;

IV=(Password.Te xt+"01234567890 123456").Substr ing(0,16);
RijndaelEnhance d re=new RijndaelEnhance d(Password.Text ,IV);
StreamReader _sr=new StreamReader(Fi leName.Text);

byte[] byteArray=re.En cryptToBytes(_s r.ReadToEnd());

string encryptedText=C onvert.ToBase64 String(byteArra y);

//string encryptedText=r e.Encrypt(_sr.R eadToEnd());

StreamWriter _sw;

try

{

_sw=new
StreamWriter(En cryptedFileLoca tion.Text,false ,System.Text.En coding.UTF8);
_sw.Write( encryptedText);

//_sw.Write(Conve rt.ToBase64Stri ng(byteArray));

_sw.Flush();

_sw.Close();

MessageBox.Show ("Encrypted File saved as " +
EncryptedFileLo cation.Text,"Fi le
Encrypted",Mess ageBoxButtons.O K,MessageBoxIco n.Information);

}

catch (Exception ex)

{

MessageBox.Show (ex.Message);

}

finally

{

Encrypt.Text="& Encrypt";

Cursor.Current= Cursors.Default ;

}

}

private void Decrypt_Click(o bject sender, System.EventArg s e)

{
try

{

Decrypt.Text="D ecrypting...";

Cursor.Current= Cursors.WaitCur sor;

string IV;

IV=(Password.Te xt+"01234567890 123456").Substr ing(0,16);
RijndaelEnhance d re=new RijndaelEnhance d(Password.Text ,IV);
StreamReader _sr=new
StreamReader(En cryptedFileLoca tion.Text,Syste m.Text.Encoding .UTF8);

string encryptedText=_ sr.ReadToEnd();

string DecryptedText=r e.Decrypt(encry ptedText);

StreamWriter _sw=new
StreamWriter(De cryptedFileLoca tion.Text,false ,System.Text.En coding.UTF8);

_sw.Write(Conve rt.FromBase64St ring(DecryptedT ext));

//_sw.Write(Decry ptedText);

_sw.Flush();

_sw.Close();

MessageBox.Show ("Decrypted File saved as '" + DecryptedFileLo cation.Text +
"'.","File Decrypted",Mess ageBoxButtons.O K,MessageBoxIco n.Information);

}

catch (Exception ex)

{

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

//MessageBox.Show (ex.Message);

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

"Unable to decrypt file",

MessageBoxButto ns.OK,

MessageBoxIcon. Warning );

}

finally

{

Decrypt.Text="D ecrypt";

Cursor.Current= Cursors.Default ;

}

}

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

"TomB" <sh*****@hotmai lXXX.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.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*****@hotmai lXXX.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
TomB <sh*****@hotmai lXXX.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.co m>
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
10438
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 correctly, and be able to decrypt them? in the perfect situation i would like to be able to store the encrypted binary file into a mysql blob field, or at least be able to save an encrypted version of the binary file on the server.
0
8248
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 decrypts the files in directory B, and puts them in directory C. I'm getting some exceptions when I try to decrypt a binary file though. Here's the code: ////////////////////////////////////////////////////////////////////// public class...
7
24458
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 fields. I want to save UserName and UserPassword using bit or binary data type with VB.NET. Then ofcourse I have to retrive them to compare it later and if I find match than user can enter the MAIN forum.
7
13086
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 a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
1
288
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 10. Then I'm using this as an index to copy to an output array, but offcourse the integer 10 is reading the non-existent 11th element of, could someone help with a solution to my problem, I would prefer to get the binary string from keyboard but I...
3
3768
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 database. But I don't know how to used source code to solve it. Can you help me? Urgently! <<<<<<Save Method<<<<<<
4
1647
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 file. Can someone tell me what I am doing wrong? try { if (sfdCommon.ShowDialog() ==
29
5090
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 something like ascii text editor. I'd also like to be able to read the binary formed data back into string format so that it shows the original value. Is there any way to do this in Python? Thanks!
6
5246
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 = File.Open(@"D:\test.dat", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs);
0
9665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.