473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Encryption/Decryption of stream.

Hi
Im trying to make a Client/Server where im going to encrypt the stream but i cant get it to work. I can recieve and send as long as im not trying to send/recieve encrypted. but when i am i cant send, recieve or both.

The Send/Recieve Code is the same on both Server and client.
Send/Recieve Code:

private Stream Net()
{
if(Encrypted)
{
CryptoStream SecStream = new CryptoStream(NetStream,TDes.CreateDecryptor(),Cryp toStreamMode.Read);
return SecStream;
}
else
return NetStream;
}

private void GetStreamData(IAsyncResult ar)
{
int intCount;
try
{
lock(Net())
{
intCount = Net().EndRead(ar);
}
if(intCount<1)
{
Client.Close();

if(Disconnected!=null)
{
EventArgs e = new EventArgs();
Disconnected(this, e);
}
}

BuildData(recByte,0,intCount);

AsyncCallback GetStreamDataCallback = new AsyncCallback(GetStreamData);
Net().BeginRead(recByte,0,1024,GetStreamDataCallba ck,null);
}
catch
{
Client.Close();

if(Disconnected!=null)
{
EventArgs e = new EventArgs();
Disconnected(this, e);
}
}
}

public void Send(string Data)
{
StreamWriter Writer;

if(Encrypted)
{
CryptoStream SecStream = new CryptoStream(NetStream,TDes.CreateEncryptor(),Cryp toStreamMode.Write);
Writer = new StreamWriter(SecStream);
}
else
Writer = new StreamWriter(NetStream);

Writer.Write(Data);
Writer.Flush();
}

Code Server:
private void BuildData(byte[] Databyte, int offset, int count)
{
string Data = Encoding.UTF8.GetString(Databyte, offset, count);
string[] DataCmd = Data.Split(':');

if(!Encrypted)
{
switch(DataCmd[0])
{
case "HELO":
{
Send("Welcome");

break;
}
case "DesKey":
{
TDes.GenerateKey();
DesKey = TDes.Key;

Send("DesKey:" + Convert.ToBase64String(DesKey));

break;
}
case "DesIV":
{
TDes.GenerateIV();
DesIV = TDes.IV;

Send("DesIV:" + Convert.ToBase64String(DesIV));

Encrypted = true;

break;
}
}
}
else
{
Send(Data);
}
}

Code Client:
private void BuildData(byte[] Databyte, int offset, int count)
{
string Data = Encoding.UTF8.GetString(Databyte, offset, count);
string[] DataCmd = Data.Split(':');

if(!Encrypted)
{
switch(DataCmd[0])
{
case "Welcome":
{
txtDataRx.Text = "Connected";

break;
}
case "DesKey":
{
//txtDataRx.Text = DataCmd[1];

DesKey = Convert.FromBase64String(DataCmd[1]);
TDes.Key = DesKey;

break;
}
case "DesIV":
{
//txtDataRx.Text = DataCmd[1];

DesIV = Convert.FromBase64String(DataCmd[1]);
TDes.IV = DesIV;

Encrypted = true;

break;
}
}
}
else
{
txtDataRx.Text = Data;
}
}
Nov 15 '05 #1
1 3404
"Martin" <tr***@guess.it> wrote in message news:<#A**************@TK2MSFTNGP10.phx.gbl>...
Hi
Im trying to make a Client/Server where im going to encrypt the stream
but i cant get it to work. I can recieve and send as long as im not
trying to send/recieve encrypted. but when i am i cant send, recieve or
both.

Martin -

The problem with feeding a TCP stream directly to a CryptoStream
object is that the CryptoStream has no idea when all of the data is
present for decryption. The CryptoStream will not wait for all of the
data to arrive, it just assumes all the data will be there when it
starts, and throws an Exception when it needs more data that is not
there. Unfortunately, most TCP connections can not deliver the data
quick enough to keep up with the CryptoStream processing, no matter
how fast your network is.

To solve this problem you need a middleman. I use a MemoryStream
to accept the incoming encrypted data from the network. When you know
you have the entire encrpyted data stream in the MemoryStream (send
the data size before sending the data), you can then feed the
MemoryStream into the CryptoStream for decryption.

I demonstrate this in Chapter 17 of my "C# Network Programming"
book. You can download the code for free from the Sybex web site and
check out the examples. Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David | last post by:
Hope someone can help or have a free control. I am going to be passing data from a client computer to a server via a winsock control. I want to encrypt/decript the contents of the string that is...
10
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
5
by: Patrick De Ridder | last post by:
The following code produces an error on the very last line, namely: An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional...
5
by: Robert Bull | last post by:
I am looking for an example of encryption/decryption in asp.net. I want to encrypt a string before I send it to a Web Service, decrypt it on the Web Service then encrypt the results on the Web...
3
by: Anon | last post by:
I made this class to encrypt my DataSet before saving it to disk. So, first in the main program I write the DataSet to XML in a MemoryStream. I pass this stream to the E_File sub, which encrypts...
4
by: hohans | last post by:
Hi all, I have an encryption class that encrypts and decrypts password using TripleDESCryptoServiceProvider. It was written originally in framework 1.0 and been working fine. And those...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: áéíóú/ÁÉÍÓÚ. It's kind of like the...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.