Server :
Module Module1
Sub Main()
Dim serverSocket As New TcpListener(8888)
Dim requestCount As Integer
Dim clientSocket As TcpClient
serverSocket.Start()
msg("Server Started")
clientSocket = serverSocket.AcceptTcpClient()
msg("Accept connection from client")
requestCount = 0
While (True)
requestCount = requestCount + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
Dim bytesFrom(clientSocket.ReceiveBufferSize) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = Encoding.ASCII.GetString(bytesFrom)
msg("Data from client - " + TripleDES.DecryptString(dataFromClient))
Dim serverResponse As String = "Server response " + Convert.ToString(requestCount)
Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(serverResponse))
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
End While
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
End Module
Client:
Public Shared Function SendRequest(ByVal InXML As String, ByRef OutXML As String) As Boolean
Try
Dim nwStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(InXML))
nwStream.Write(outStream, 0, outStream.Length)
nwStream.Flush()
Dim inStream(clientSocket.ReceiveBufferSize) As Byte
nwStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim tmpStr As String = Encoding.ASCII.GetString(inStream)
OutXML = TripleDES.DecryptString(tmpStr)
Return True
Catch ex As Exception
OutXML = ex.Message
Return False
End Try
End Function
End Class
Encryption Class
Public Class TripleDES
Private Shared IV() As Byte = Encoding.UTF8.GetBytes("password")
Private Shared EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5")
Public Shared Function EncryptString(ByVal Input As String) As String
Dim buffer() As Byte = Encoding.UTF8.GetBytes(Input)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
des.Mode = CipherMode.ECB
des.Key = EncryptionKey
des.IV = IV
Dim output() As Byte = des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length())
Return Convert.ToBase64String(output)
End Function
Public Shared Function DecryptString(ByVal Input As String) As String
Dim buffer() As Byte = Convert.FromBase64String(Input)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
des.Mode = CipherMode.ECB
des.Key = EncryptionKey
des.IV = IV
Dim output() As Byte = des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length())
Return Encoding.UTF8.GetString(output)
End Function
End Class
The encryption part is done successfully. But the transferring and decryption are not happening. I checked the code for transferring the file without encryption, it is working. The encryption and decryption classes are also working when used separately without file transfer. Can someone please point out where am I going wrong here. I am sorry if the code is too long but had to present what I tried.