0

I'm trying to implement encryption on my client - server application, but since I implemented it I can't communicate between the applications.

Server:

private void CommsHandler(object client)
    {

        TcpClient tcpClient = (TcpClient)client;

        StreamWriter writer;
        StreamReader reader;
        using (RijndaelManaged myRijindel = new RijndaelManaged())
        {
            myRijindel.GenerateKey();
            myRijindel.GenerateIV();
            ICryptoTransform encryptor = myRijindel.CreateEncryptor(myRijindel.Key, myRijindel.IV);

            byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  
                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
            byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  
               0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};  

            CryptoStream secureStreamRead = new CryptoStream(tcpClient.GetStream(), myRijindel.CreateDecryptor(Key,IV), CryptoStreamMode.Read);
            CryptoStream secureStreamWrite = new CryptoStream(tcpClient.GetStream(), myRijindel.CreateEncryptor(Key, IV), CryptoStreamMode.Write);

            reader = new StreamReader(secureStreamRead);
            writer = new StreamWriter(secureStreamWrite);
        }

        NetworkStream network = tcpClient.GetStream();


        char[] buffer = new char[4096];

        while (true)
        {
            try
            {
                reader.Read(buffer, 0, buffer.Length);
            }
            catch (Exception)
            {
                InsertLog(2, "Communication with client closed");
                break;
            }
            string message = new string(buffer);
            Debug.Write(message);

            doJob(message, writer, network);

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (char)0;
            }
        }

        tcpClient.Close();
    }

Client:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        client.Connect(textBox1.Text, Convert.ToInt16(textBox2.Text));


        using (RijndaelManaged myRijindel = new RijndaelManaged())
        {
            myRijindel.GenerateKey();
            myRijindel.GenerateIV();
            ICryptoTransform encryptor = myRijindel.CreateEncryptor(myRijindel.Key, myRijindel.IV);

            byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  
                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
            byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  
                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

            CryptoStream secureStreamRead = new CryptoStream(client.GetStream(), myRijindel.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
            CryptoStream secureStreamWrite = new CryptoStream(client.GetStream(), myRijindel.CreateEncryptor(Key, IV), CryptoStreamMode.Write);

            reader = new StreamReader(secureStreamRead);
            writer = new StreamWriter(secureStreamWrite);
        }

        network = client.GetStream();
        connected = true;
        MessageBox.Show("Connected successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Could not connect to server:" + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Is there anything wrong with my implementation? Especially on the StreamWriter and StreamReader parts. Or is the problem elsewhere?

Javier Brooklyn
  • 624
  • 3
  • 9
  • 25
Bruno Klein
  • 3,217
  • 5
  • 29
  • 39
  • What's the error? What's the symptom? You cannot debug something by just staring at the code. – usr Feb 06 '13 at 19:43

1 Answers1

2

A using block always calls IDisposable.Dispose() at the end of the block.

So if you say:

using (RijndaelManaged myRijindel = new RijndaelManaged())
{
    ...
}

Your crypto-provider is closed once you end the block. I would imagine that is part of your problem.

sircodesalot
  • 11,231
  • 8
  • 50
  • 83