Re: a problem with encryption
From: Tonci Jukic (nytrogen_at_email.htnet.hr)
Date: 08/10/04
- Previous message: Bob Powell [MVP]: "Re: REPOST: Displaying and Editing a Time value in a PropertyGrid"
- In reply to: Jon Skeet [C# MVP]: "Re: a problem with encryption"
- Next in thread: Jon Skeet [C# MVP]: "Re: a problem with encryption"
- Reply: Jon Skeet [C# MVP]: "Re: a problem with encryption"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 10 Aug 2004 13:32:19 -0700
>The problem is that you're assuming the decrypted size will be the same
as the encrypted size - it's not. You're only actually reading 15 bytes
(and moreover, you're assuming they'll all be read in one go, which is a
bad idea) but passing the encoding a buffer 16 bytes long.
>*Always* use the return value of Stream.Read.
Well, the problem was not in network operations and data send. It was in
decrypted data.
The problem was I did always get n to 16 bytes filled with zeros.
So I've just swapped line:
roundtrip = textConverter.GetString(fromEncrypt);
with:
roundtrip =
textConverter.GetString(fromEncrypt).TrimEnd(Convert.ToChar(0));
That way I always get the original data I've encrypted.
Thanks btw.
I have another question:
When I'm sending this data trough network stream from client to server,
I always create byte type array big enough to accept possible data from
the client application.
Do I have to always create it big enough to support any possible data
size, or I can read everything in blocks and then merge it to a single
byte array for example.
This is how it is done by now:
client code:
(this is a connection thread code cut from the main code:)
try
{
this.hostName = this.textBox2.Text;
TcpClient client = new TcpClient(hostName, portNum);
NetworkStream ns = client.GetStream();
//size of response buffer
byte[] bytes = new byte[1024];
//using custom encryption class to encrypt given data
bit256_RijndaelEnCryptorC enkripted = new bit256_RijndaelEnCryptorC();
//encrypt string from the textbox
encrypted.EnCrypt(this.textBox1.Text);
//create data-to-be-sent buffer
byte[] byteTime = new byte[encrypted.ReleaseEnCrypted).Length];
//fill it
byteTime = encrypted.ReleaseEnCrypted();
//write it trough stream
ns.Write(byteTime, 0, byteTime.Length);
//receive a response
int bytesRead = ns.Read(bytes, 0, bytes.Length);
client.Close();
}
server code:
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
//buffer for incoming data
byte[] bytes = new byte[4096];
//read data from the ns
int bytesRead = ns.Read(bytes, 0, bytes.Length);
//input data in a work buffer
byte[] returned = new byte[bytesRead];
for (int u=0; u<bytesRead; u++)
{
returned[u]=bytes[u];
}
//create data variables to be used in encryption process
byte[] key = new byte[32];
byte[] IV = new byte[16];
byte[] encrypted = new byte[returned.Length-48];
//strip usable data from the incoming stream
for (int i=0; i<32; i++)
{
key[i]=returned[i];
}
for (int i=32; i<48; i++)
{
IV[i-32]=returned[i];
}
for (int i=48; i<returned.Length; i++)
{
encrypted[i-48]=returned[i];
}
//create a custom encryption (this time decryption) class
bit256_RijndaelEnCryptorC dekripted = new
bit256_RijndaelEnCryptorC(key,IV,encrypted);
encrypted.DeCrypt();
string result = encrypted.ReleaseDeCrypted();
byte[] byteTime = Encoding.ASCII.GetBytes("server performed
operations!");
try
{
ns.Write(byteTime, 0, byteTime.Length);
ns.Close();
}
client.Close();
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
- Previous message: Bob Powell [MVP]: "Re: REPOST: Displaying and Editing a Time value in a PropertyGrid"
- In reply to: Jon Skeet [C# MVP]: "Re: a problem with encryption"
- Next in thread: Jon Skeet [C# MVP]: "Re: a problem with encryption"
- Reply: Jon Skeet [C# MVP]: "Re: a problem with encryption"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|