how to send encrypted data?

  • Thread starter Thread starter Franz
  • Start date Start date
F

Franz

I use the RC2EncrytoServiceProvider, after encrypted the data, what things should I send in order to let the client who received the
encrypted data to decrypt it?
Thanks.

The following is the code that I encrypt the data.

// Read Data to Byte Array
BinaryReader aByteReader = new BinaryReader(new FileStream(strOriFilePath, FileMode.Open));
int iByteArrayLength = (int)aByteReader.BaseStream.Length;
byte[] byteContent = aByteReader.ReadBytes(iByteArrayLength);
aByteReader.Close();

FileStream aStream = new FileStream(strModFilePath, FileMode.Create);

RC2CryptoServiceProvider aRC2CSP = new RC2CryptoServiceProvider();

aRC2CSP.GenerateKey();
aRC2CSP.GenerateIV();

// Get the Key and IV.
byte[] aKey = aRC2CSP.Key;
byte[] aIV = aRC2CSP.IV;

ICryptoTransform aEncryptor = aRC2CSP.CreateEncryptor(aKey, aIV);
CryptoStream aCryptoStream = new CryptoStream(aStream, aEncryptor, CryptoStreamMode.Write);
aCryptoStream.Write(byteContent, 0, iByteArrayLength);
aCryptoStream.Close();
aStream.Close();
 
I am just a newbie for cryptography. Do you have any good sites for me?
Thanks.

--
My C++ and C# ( Traditional Chinese ) Web Site : www.franzwong.com/Home.php


Rob Teixeira said:
Symmetric encryption (RC2 is a symmetric cypher) uses the same key (and IV
if you salt the stream, which all .NET symmetric crypto classes do by
default) for encryption as decryption. However, DO NOT put the key (and IV)
in a place that everyone can get at it. That is like locking all your doors
and taping the key onto the outside of the door.

Both processes that use the data (the system that encrypts it, and the
system that decrypts it) must have access to the same key.

If you need to send a symmetric key, then I suggest following a good Key
Exchange protocol, which usually involves an exchange of public/private keys
for another (asymmetric) crypto algorithm, that is in turn used to send the
symmetric key securly.

-Rob [MVP]

Franz said:
I use the RC2EncrytoServiceProvider, after encrypted the data, what things
should I send in order to let the client who received the
encrypted data to decrypt it?
Thanks.

The following is the code that I encrypt the data.

// Read Data to Byte Array
BinaryReader aByteReader = new BinaryReader(new FileStream(strOriFilePath, FileMode.Open));
int iByteArrayLength = (int)aByteReader.BaseStream.Length;
byte[] byteContent = aByteReader.ReadBytes(iByteArrayLength);
aByteReader.Close();

FileStream aStream = new FileStream(strModFilePath, FileMode.Create);

RC2CryptoServiceProvider aRC2CSP = new RC2CryptoServiceProvider();

aRC2CSP.GenerateKey();
aRC2CSP.GenerateIV();

// Get the Key and IV.
byte[] aKey = aRC2CSP.Key;
byte[] aIV = aRC2CSP.IV;

ICryptoTransform aEncryptor = aRC2CSP.CreateEncryptor(aKey, aIV);
CryptoStream aCryptoStream = new CryptoStream(aStream, aEncryptor, CryptoStreamMode.Write);
aCryptoStream.Write(byteContent, 0, iByteArrayLength);
aCryptoStream.Close();
aStream.Close();
 
I learned most of it from my prior knowledge of cryptography and the .NET
msdn help.
However, I did find this book out on the web:

http://www.amazon.com/exec/obidos/tg/detail/-/067232184X/ref=pd_sim_books_1/
002-9192962-4937643?v=glance&s=books

I haven't read it, but it does seem to cover what you're looking for.

-Rob [MVP]

Franz said:
I am just a newbie for cryptography. Do you have any good sites for me?
Thanks.

--
My C++ and C# ( Traditional Chinese ) Web Site : www.franzwong.com/Home.php


"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> ¼¶¼g©ó¶l¥ó·s»D:#[email protected]...
Symmetric encryption (RC2 is a symmetric cypher) uses the same key (and IV
if you salt the stream, which all .NET symmetric crypto classes do by
default) for encryption as decryption. However, DO NOT put the key (and IV)
in a place that everyone can get at it. That is like locking all your doors
and taping the key onto the outside of the door.

Both processes that use the data (the system that encrypts it, and the
system that decrypts it) must have access to the same key.

If you need to send a symmetric key, then I suggest following a good Key
Exchange protocol, which usually involves an exchange of public/private keys
for another (asymmetric) crypto algorithm, that is in turn used to send the
symmetric key securly.

-Rob [MVP]

Franz said:
I use the RC2EncrytoServiceProvider, after encrypted the data, what
things
should I send in order to let the client who received the
encrypted data to decrypt it?
Thanks.

The following is the code that I encrypt the data.

// Read Data to Byte Array
BinaryReader aByteReader = new BinaryReader(new
FileStream(strOriFilePath,
FileMode.Open));
int iByteArrayLength = (int)aByteReader.BaseStream.Length;
byte[] byteContent = aByteReader.ReadBytes(iByteArrayLength);
aByteReader.Close();

FileStream aStream = new FileStream(strModFilePath, FileMode.Create);

RC2CryptoServiceProvider aRC2CSP = new RC2CryptoServiceProvider();

aRC2CSP.GenerateKey();
aRC2CSP.GenerateIV();

// Get the Key and IV.
byte[] aKey = aRC2CSP.Key;
byte[] aIV = aRC2CSP.IV;

ICryptoTransform aEncryptor = aRC2CSP.CreateEncryptor(aKey, aIV);
CryptoStream aCryptoStream = new CryptoStream(aStream, aEncryptor, CryptoStreamMode.Write);
aCryptoStream.Write(byteContent, 0, iByteArrayLength);
aCryptoStream.Close();
aStream.Close();
 
Back
Top