加密流使用不正确

本文关键字:不正确 密流 加密 | 更新日期: 2023-09-27 17:58:07

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

服务器:

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

我的实现有什么问题吗?特别是在CCD_ 1和CCD_。还是其他地方的问题?

加密流使用不正确

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

所以如果你说:

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

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