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?