CryptoStream does not decrypt properly

746 views Asked by At

I have a working TCP-connection between a server application and a client, wich communicate encrypted with AES. it works with an CryptoStream having an RijndaelManaged En/Decrypter. well Right now, if i want to send sth. like this: "F4FBF-B60FC-6F3ED-1E5A9" i get that as output : "Ü]Þí£ôÉò×â×$¼ÚD-1E5A9" so... what could be going wrong here? why is that still encryptet up to "D-1E5A9"?

Well here some Code: Server:

while (true)
{   
    Console.Write("Waiting for a connection... ");

    TcpClient client = server.AcceptTcpClient();
    Console.WriteLine("Connected!");

    NetworkStream stream = client.GetStream();
    stream.ReadTimeout = System.Threading.Timeout.Infinite;
    stream.WriteTimeout = System.Threading.Timeout.Infinite;

    byte[] KeyBuffer = new Byte[1510];
    stream.Read(KeyBuffer, 0, KeyBuffer.Length);
    stream.Flush();
    RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(4096);
    RSAC.FromXmlString(bla.GetString(KeyBuffer));

    RijndaelManaged RM = new RijndaelManaged();

    byte[] RMKeyBuffer = RSAC.Encrypt(RM.Key, true);
    stream.Write(RMKeyBuffer, 0, RMKeyBuffer.Length);
    stream.Flush();
    CryptoStream cs = new CryptoStream(stream, RM.CreateDecryptor(), CryptoStreamMode.Read);
    byte[] licence = new byte[23];
    cs.Read(licence, 0, licence.Length);
    cs.Dispose();

    string myConnectionString = "SERVER=Server;" +
    "DATABASE=Database;" +
    "UID=UID;" +
    "PASSWORD=Password;";

    MySqlConnection mysqlConn = new MySqlConnection(myConnectionString);
    MySqlCommand myComm = mysqlConn.CreateCommand();
    myComm.CommandText = "SELET * From lizenzen WHERE Lizenznummer='"+bla.GetString(licence)+"'";
    mysqlConn.Open();
}

Client:

private void DoTheThing(string Lizenz)
       { 
        textBox2.Text = "Passed Beginning";
                Encoding bla = Encoding.BigEndianUnicode;
                Int32 port = 14000;
                TcpClient client = new TcpClient("192.168.1.177", port);
                NetworkStream stream = client.GetStream();
                stream.ReadTimeout = System.Threading.Timeout.Infinite;
                stream.WriteTimeout = System.Threading.Timeout.Infinite;
                textBox2.Text = "Passed Socket Creation";

                RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(4096);
                byte[] Key = bla.GetBytes(RSAC.ToXmlString(false));
                stream.Write(Key, 0, Key.Length);
                textBox2.Text = "Passed sending KEy";

                byte[] encryAES = new byte[512];
                stream.Read(encryAES, 0, encryAES.Length);
                textBox2.Text = "Passed receiving Encrypted AES Key";

                byte[] decryAES = RSAC.Decrypt(encryAES, true);
                string decryAesString = string.Empty;
                textBox2.Text = "Passed Decryption";

                foreach (byte item in decryAES)
                {
                    decryAesString += ((int)item).ToString();
                }
                textBox1.Text = decryAesString;

                RijndaelManaged RM = new RijndaelManaged();
                RM.Key = decryAES;
                CryptoStream csS = new CryptoStream(stream, RM.CreateEncryptor(), CryptoStreamMode.Write);
                byte[] Licence = bla.GetBytes(Lizenz);
                csS.Write(Licence,0,Licence.Length);
                csS.Flush();
                csS.Dispose();

                CryptoStream csR = new CryptoStream(stream, RM.CreateDecryptor(), CryptoStreamMode.Read);
                byte[] Data = new byte[4096];
                csR.Read(Data, 0, Data.Length);
                csR.Flush();
                csR.Dispose();

                textBox3.Text = bla.GetString(Data);
}

sometime, i will begin to use "using" to get rid of the flushes and disposes, but right now, i just tried to get that working and after a long day working on the RSA encryption to get that working, now i got that problem. and if you wonder what encodiung 'bla' is... just imagine whatever you want, it doesn't make a difference.

0

There are 0 answers