0

我正在尝试在我的客户端 - 服务器应用程序上实现加密,但由于我实现了它,我无法在应用程序之间进行通信。

服务器:

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();
    }

客户:

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);
    }
}

我的实施有什么问题吗?特别是在StreamWriterStreamReader零件上。还是其他地方的问题?

4

1 回答 1

2

using 块总是在块的末尾调用 IDisposable.Dispose()。

所以如果你说:

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

一旦您结束区块,您的加密提供者就会关闭。我想这是你问题的一部分。

于 2013-02-06T19:37:27.923 回答