Greetings! My first post here and would just like to say hello to everybody first.
Anyway I googled for awhile and could not come up with a solid solution to my problem. I have coded a simple local LAN messenger-type application. I need to somehow encrypt the outbound message and decrypt the inbound messages. I realize something to do with a public/private key is needed. So far I have this (im not sure how post in the code tag if there is one somebody help me out lol )
Public Class Form1
Dim textbytes, encryptedtextbytes As Byte() 'encryption part
Dim rsa As New RSACryptoServiceProvider
Dim encoder As New UTF8Encoding
Dim listen As New TcpListener(44444) 'communication
Dim client As TcpClient
Dim message As String = ""
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listen.Stop()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listen.Start()
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
If listen.Pending = True Then
message = ""
client = listen.AcceptTcpClient()
Dim reader As New StreamReader(client.GetStream())
While reader.Peek > -1
message = message + (Convert.ToChar(reader.Read())).ToString
End While
Me.Focus()
'**********************TRYING TO DECRYPT THE SENT MESSAGE*************************'
Dim bytes() As Byte = ASCIIEncoding.ASCII.GetBytes(message)
textbytes = rsa.Decrypt(bytes, False)
MsgBox(encoder.GetString(textbytes))
TextBox4.Text = (TextBox4.Text + message + vbCrLf)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'SENDING BELOW!!!!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
MsgBox("DATA IS MISSING!!")
Else
client = New TcpClient(TextBox2.Text, 44444)
Dim writer As New StreamWriter(client.GetStream())
Dim TexttoEncrypt As String = TextBox3.Text
textbytes = encoder.GetBytes(TexttoEncrypt)
encryptedtextbytes = rsa.Encrypt(textbytes, True)
Dim result As String = System.Text.Encoding.ASCII.GetString(encryptedtextbytes) 'Sending correctly ?????????
writer.Write(TextBox1.Text + " says: " + result)
writer.Flush()
TextBox3.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Thank you in advance. And forgive my ignorance in RSA.
Anyway I googled for awhile and could not come up with a solid solution to my problem. I have coded a simple local LAN messenger-type application. I need to somehow encrypt the outbound message and decrypt the inbound messages. I realize something to do with a public/private key is needed. So far I have this (im not sure how post in the code tag if there is one somebody help me out lol )
Public Class Form1
Dim textbytes, encryptedtextbytes As Byte() 'encryption part
Dim rsa As New RSACryptoServiceProvider
Dim encoder As New UTF8Encoding
Dim listen As New TcpListener(44444) 'communication
Dim client As TcpClient
Dim message As String = ""
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listen.Stop()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listen.Start()
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
If listen.Pending = True Then
message = ""
client = listen.AcceptTcpClient()
Dim reader As New StreamReader(client.GetStream())
While reader.Peek > -1
message = message + (Convert.ToChar(reader.Read())).ToString
End While
Me.Focus()
'**********************TRYING TO DECRYPT THE SENT MESSAGE*************************'
Dim bytes() As Byte = ASCIIEncoding.ASCII.GetBytes(message)
textbytes = rsa.Decrypt(bytes, False)
MsgBox(encoder.GetString(textbytes))
TextBox4.Text = (TextBox4.Text + message + vbCrLf)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'SENDING BELOW!!!!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
MsgBox("DATA IS MISSING!!")
Else
client = New TcpClient(TextBox2.Text, 44444)
Dim writer As New StreamWriter(client.GetStream())
Dim TexttoEncrypt As String = TextBox3.Text
textbytes = encoder.GetBytes(TexttoEncrypt)
encryptedtextbytes = rsa.Encrypt(textbytes, True)
Dim result As String = System.Text.Encoding.ASCII.GetString(encryptedtextbytes) 'Sending correctly ?????????
writer.Write(TextBox1.Text + " says: " + result)
writer.Flush()
TextBox3.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Thank you in advance. And forgive my ignorance in RSA.