473,770 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Ne tStream,TDes.Cr eateDecryptor() ,CryptoStreamMo de.Read);
return SecStream;
}
else
return NetStream;
}

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

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

BuildData(recBy te,0,intCount);

AsyncCallback GetStreamDataCa llback = new AsyncCallback(G etStreamData);
Net().BeginRead (recByte,0,1024 ,GetStreamDataC allback,null);
}
catch
{
Client.Close();

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

public void Send(string Data)
{
StreamWriter Writer;

if(Encrypted)
{
CryptoStream SecStream = new CryptoStream(Ne tStream,TDes.Cr eateEncryptor() ,CryptoStreamMo de.Write);
Writer = new StreamWriter(Se cStream);
}
else
Writer = new StreamWriter(Ne tStream);

Writer.Write(Da ta);
Writer.Flush();
}

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

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

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

Send("DesKey:" + Convert.ToBase6 4String(DesKey) );

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

Send("DesIV:" + Convert.ToBase6 4String(DesIV)) ;

Encrypted = true;

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

Code Client:
private void BuildData(byte[] Databyte, int offset, int count)
{
string Data = Encoding.UTF8.G etString(Databy te, 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.FromBas e64String(DataC md[1]);
TDes.Key = DesKey;

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

DesIV = Convert.FromBas e64String(DataC md[1]);
TDes.IV = DesIV;

Encrypted = true;

break;
}
}
}
else
{
txtDataRx.Text = Data;
}
}
Nov 15 '05 #1
1 3453
"Martin" <tr***@guess.it > wrote in message news:<#A******* *******@TK2MSFT NGP10.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
6583
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 passed back and forth. Anyone have any ideas? David
10
2357
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 your Visual Studio and compile it + run it; so you can see what the actual problem is. Thanks. code:
5
2160
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 information: Bad Data. Please help; if you can with exactly which code I should change and how. Patrick ****************************************************************
5
7698
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 Service and decrypt the results on the web app. Any example using System.Security.Cryptography namespace (VB not C#, thanks)would be greatly appreciated. Thanks in advance. -Rob
3
3211
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 the stream and writes it out to the FileStream. Now, in the D_File function, I read the encrypted file into a FileStream, then decrypt it into a MemoryStream, which I pass back to my DataSet and Read the XML into the DataSet. The problem I'm...
4
2595
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 passwords are stored in my SQL server. Now I need to migrate my application to framework 2.0. I use this same class with framework 2.0 library to decrypt the passwords from database (of course, they were encrypted in 1.0) but getting "Bad Data"
2
7151
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 French grave... Anyway when I run encryption on a plaintext word like:
8
2745
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 understand why it is doing this. Below is my code, I would be greatfull if someone can guide me through the right path or even help me solve this issue. Problem: The old tool which was written in VB6 works perfect. But I needed to convert this to C#...
3
3078
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 functions. While my functions read and write files the encryption/decryption is not working properly. My test file has an original length of 66,048 bytes. My encrypted file ends up with 66,056 bytes … 8 bytes more than my original. When I...
0
9618
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
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.