I have to make the encrypted comunication between server and client ,where to encrypt the message we use RC2 and the secret key is protected by RSA.

My code for the client is :
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.IO;
namespace PjesaKLIENTI_TCP
{
public class Klienti
{
scopes(fusheveprimit)te Main-it
static Socket S_Klienti;
static byte[] bufferi = new byte[4 * 1024];
static int prn;
 
static void Main(string[] args)
{
 


 
S_Klienti = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint Ip_Klienti = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4000);
 

try
{
S_Klienti.Connect(Ip_Klienti);
}
catch (SocketException SE)
{

Console.Write("\nE pamundur per t'u lidhur me server. " + SE);
return;
}
string KPbS;
prn = S_Klienti.Receive(bufferi);
KPbS = Encoding.UTF8.GetString(bufferi, 0, prn);
Console.WriteLine("U pranua celesi publik i serverit");
 
RSACryptoServiceProvider rsaK = new RSACryptoServiceProvider();
rsaK.FromXmlString(KPbS);
Console.WriteLine("U importua celesi pubik i serverit");

}
public static void DERGO_HYRJEN_NGA_CONSOLA(string Hyrja_Tastieres)
{
try
{
S_Klienti.Send(Encoding.ASCII.GetBytes(Hyrja_Tastieres));
}
catch
{
Console.WriteLine("Nuk mund te dergohet hyrja ,Ju lutemi kontrolloni edhe nje here konektimin\n\n");
 
}
 
}
 

public static string MERRE_PERGJIGJEN_NGASERVERI()
{
byte[] E_Dhena_Nga_Serveri = new byte[1024];
int Gjatesia_Dhenes = S_Klienti.Receive(E_Dhena_Nga_Serveri);
string E_Dhena_Perfundimtare = Encoding.ASCII.GetString(E_Dhena_Nga_Serveri, 0, Gjatesia_Dhenes);
 
return E_Dhena_Perfundimtare;
}
public static string DecryptTextFromFile(string mesazhi)
{
return "";
}

}
}
==========================================================================================
And the code for the server is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.IO;
 
namespace PjesaSERVERI_TCP
{
public class Serveri
{
 
public static IPEndPoint ipKlienti;
public static TcpListener TCP_Listener;
private int port = 9999;
static Socket KLIENTI;

 
static void Main(string[] args)
{
 

Serveri s = new Serveri();
 

Socket S_FIllimiLidhjes = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
IPEndPoint IP = new IPEndPoint(IPAddress.Any, 4000);//Lidhet me cilindo klient qe shtron kerkese
 

S_FIllimiLidhjes.Bind(IP);
S_FIllimiLidhjes.Listen(20);
 
Console.WriteLine("\n\nServeri eshte duke pritur per ndonje klient!\n");

KLIENTI = S_FIllimiLidhjes.Accept();
 
IPEndPoint ipKLienti = (IPEndPoint)KLIENTI.RemoteEndPoint;
Console.WriteLine("\nServeri u lidhe me klientin{0} ne portin {1}", ipKLienti.Address, ipKLienti.Port);
 

 
RSACryptoServiceProvider rsaS = new RSACryptoServiceProvider();
string KpbS = rsaS.ToXmlString(false);
KLIENTI.Send(Encoding.UTF8.GetBytes(KpbS));
Console.WriteLine("U dergua celesi publik tek:{0}", ipKlienti.Address);
string mesahi = "jepni nje tekst";
KTHEJE_PERGJIGJEN_TEK_KLIENTI(mesahi);
string teksti = MERRE_TE_DHENEN_E_PROCESUAR();
string ekriptuar=Enkripto(teksti,"17");
 

 
}
 
public static void KTHEJE_PERGJIGJEN_TEK_KLIENTI(string Pergjigja_kthyer)
{
 
byte[] B_Finale = Encoding.ASCII.GetBytes(Pergjigja_kthyer);
//kthejme pergjgjen
KLIENTI.Send(B_Finale, B_Finale.Length, SocketFlags.None);
 
}
 
public static string MERRE_TE_DHENEN_E_PROCESUAR()
{
byte[] B_Dhena = new byte[2000];
int Gjatesia_Te_Dhenes = KLIENTI.Receive(B_Dhena);
string Server_Dhena = Encoding.ASCII.GetString(B_Dhena, 0, Gjatesia_Te_Dhenes);
return Server_Dhena;
 
}

public static string Enkripto(string message, string inkey)
{
bool file = false;
byte[] b = null;
 
//if (message == null) return;
//if (inkey == null) return;
try
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
 
RC2CryptoServiceProvider rc2 = new System.Security.Cryptography.RC2CryptoServiceProvider();
 

if (inkey == "") { inkey = "test123"; }
 
rc2.Key = StringToByte(inkey, 12); // convert to 12 characters
 
rc2.IV = StringToByte("", 8);
byte[] key = rc2.Key;
byte[] IV = rc2.IV;
 
ICryptoTransform encryptor = rc2.CreateEncryptor(key, IV);
 
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
 
// Write all data to the crypto stream and flush it.
 
if (file == false)
{
csEncrypt.Write(StringToByte(message), 0, StringToByte(message).Length);
csEncrypt.FlushFinalBlock();
}
else
{
 
csEncrypt.Write(b, 0, b.Length);
csEncrypt.FlushFinalBlock();
}
 
// Get the encrypted array of bytes.
byte[] encrypted1 = msEncrypt.ToArray();
 
string encrypted = ByteToString(encrypted1);
 
ICryptoTransform decryptor = rc2.CreateDecryptor(key, IV);
 
// Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(encrypted1);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
 
string decrypted = ByteToString(csDecrypt);
return decrypted;
}
catch (Exception ex)
{
string encrypted = ex.Message.ToString();
return encrypted;

} 
}
public static byte[] StringToByte(string StringToConvert, int length)
{
 
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static byte[] StringToByte(string StringToConvert)
{
 
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[CharArray.Length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static string ByteToString(CryptoStream buff)
{
string sbinary = "";
int b = 0;
do
{
b = buff.ReadByte();
if (b != -1) sbinary += ((char)b);
 
} while (b != -1);
return (sbinary);
}
public static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); // hex format
}
return (sbinary);
}

 

 

}
 
}
===
That was my code so far..I just can't make it work.