简单聊天客户端 - 服务器加密

时间:2016-05-09 16:29:27

标签: c# encryption client client-server aes

我想制作一个聊天客户端服务器,非常简单,但同时非常安全,使用AES-256bit。我当时是这么认为的,当然,告诉我我是不是错了。 各个客户端在“安全”文本框中输入密码(在任何地方必须相同)和发送功能(客户端程序),客户端使用密码加密我放入另一个普通文本框的消息并将其发送到服务器;接收功能总是使用相同的密钥解密。服务器应该在加密级别不执行任何操作。 我试过,但我不知道它是否因为在代码级别不起作用。 以下是我在客户端上修改发送和接收功能的方法:发送功能

// send the message to the server
private void SendMessage()
{
    if (txtMessage.Lines.Length >= 1)
    {
        byte[] passwordBytes = GetPasswordBytes();
        encrypted_message = AES.Encrypt(txtMessage.Text, passwordBytes);
        swSender.WriteLine(encrypted_message);
        swSender.Flush();
        txtMessage.Lines = null;
    }
    txtMessage.Text = "";
}

接收功能:

private void ReceiveMessages()
{
    // Receive the response from the server
    srReceiver = new StreamReader(tcpServer.GetStream());
    // If the first character of the response is 1, connection was successful
    string ConResponse = srReceiver.ReadLine();
    // If the first character is a 1, connection was successful
    if (ConResponse[0] == '1')
    {
        // Update the form to tell it we are now connected
        this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "Connessione avvenuta con successo!" });
    }
    else // If the first character is not a 1 (probably a 0), the connection was unsuccessful
    {
        string Reason = "Non connesso: ";
        // Extract the reason out of the response message. The reason starts at the 3rd character
        Reason += ConResponse.Substring(2, ConResponse.Length - 2);
        // Update the form with the reason why we couldn't connect
        this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason });
        // Exit the method
        return;
    }
    // While we are successfully connected, read incoming lines from the server
    while (Connected == true)
    {
            try
            {
                // Show the messages decrypted in the log TextBox
                this.Invoke(new UpdateLogCallback(this.MessageUpdateLog), new object[] { srReceiver.ReadLine() });

            }
            catch
            {
                try
                {
                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { srReceiver.ReadLine() });
                }
                catch { }
            }
    }
}

// This method is called from a different thread in order to update the log TextBox
private void UpdateLog(string strMessage)
{
    txtLog.AppendText(strMessage + "\r\n");
}

//Decrypt the messages incoming from the other client
private void MessageUpdateLog(string strMessage_de)
{
    byte[] passwordBytes = GetPasswordBytes();
    strMessage_de = AES.Decrypt(strMessage_de, passwordBytes);
    // Append text also scrolls the TextBox to the bottom each time
    txtLog.AppendText(strMessage_de + "\r\n");
}

不要让工作变得困难,我想通过语音交换加密密码(密钥),因为它是一个供个人使用的聊天室。

0 个答案:

没有答案