Reading AES encrypted message from TCP socket in C#?

105 Views Asked by At

I am using TCP socket in C# sending and receiving encrypted messages using AES encryption algorithm. The problem is when I receive the encrypted message using stream reader, it came as System.byte[] which I do not know how to read it to pass it to the decryption method.

this is to send the encrypted message

private void backgroundWorker2_DoWork_1(object sender, DoWorkEventArgs e)
       {
           if (client.Connected)
           {
               STW.WriteLine(EncryptStringToBytes(TextToSend,key));
               this.chat_box.Invoke(new MethodInvoker(delegate ()
               {
                   chat_box.AppendText("Me : " + TextToSend + " \n");
               }
               ));
           }
           else
           {
               MessageBox.Show("Failed to send a messege");
           }
           backgroundWorker2.CancelAsync();
       } 

this is to receive the encrypted message

 private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
        {
            while (client.Connected)
            {
                try
                {
                    recieve = STR.ReadLine();  // this message is string
                    // I need to read it as byte format to pass it to Decrytion function                
                    this.chat_box.Invoke(new MethodInvoker(delegate ()
                    {
                        chat_box.AppendText("You : " + recieve + "\n");
                    }
                    ));
                    recieve = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

My problem is here, this message is string I need to read it as byte format to pass it to Decrytion function

  recieve = STR.ReadLine();  // this message is string
                    // I need to read it as byte format to pass it to Decrytion function   
0

There are 0 best solutions below