Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server :
VB
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

            '' Data reading from client
            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))
          
            '' Data writting to client
            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:
VB.NET
Public Shared Function SendRequest(ByVal InXML As String, ByRef OutXML As String) As Boolean
        Try
            Dim nwStream As NetworkStream = clientSocket.GetStream()

            '' write to network stream
            Dim outStream As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(InXML))
            ' Dim outStream As Byte() = Encoding.ASCII.GetBytes(InXML)
            nwStream.Write(outStream, 0, outStream.Length)
            nwStream.Flush()

            '' data reading from server
            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)

            'OutXML = tmpStr

            Return True

        Catch ex As Exception
            OutXML = ex.Message
            Return False

        End Try

    End Function

End Class



Encryption Class

VB
Public Class TripleDES

    '' Initilization vector
    Private Shared IV() As Byte = Encoding.UTF8.GetBytes("password")

    '' Encryption key - base 64
    Private Shared EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5")

    '' Encryption method
    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

    '' Descryption method
    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.
Posted
Updated 15-Apr-13 4:53am
v3

1 solution

I'm pretty sure it's your text encoding:

VB
Dim dataFromClient As String = Encoding.ASCII.GetString(bytesFrom)


Your encryption code is probably generating characters that can not be represented in ascII. try using:

VB
Encoding.Unicode...


on both ends instead.
 
Share this answer
 
Comments
normalsoft 16-Apr-13 2:40am    
Thanks for your reply.
I changed the code on both ends as follows (lines bolded). But i'm still getting the error while decrypting the client encrypted data in server.

"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters."

Client side:
Collapse | Copy Code
Public Shared Function SendRequest(ByVal InXML As String, ByRef OutXML As String) As Boolean
Try
Dim nwStream As NetworkStream = clientSocket.GetStream()

'' write to network stream
Dim outStream As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(InXML))
' Dim outStream As Byte() = Encoding.ASCII.GetBytes(InXML)
nwStream.Write(outStream, 0, outStream.Length)
nwStream.Flush()

'' data reading from server
Dim inStream(clientSocket.ReceiveBufferSize) As Byte
nwStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))

'Dim tmpStr As String = Encoding.ASCII.GetString(inStream)
Dim tmpStr As String = Encoding.Unicode.GetString(inStream)
OutXML = TripleDES.DecryptString(tmpStr)
Return True
Catch ex As Exception
OutXML = ex.Message
Return False
End Try
End Function

Server side:
Collapse | Copy Code
Module Module1

Sub Main()

' Dim ipAddress As IPAddress = ipAddress.Parse("192.168.8.8")
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

'' Data reading from client
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)
Dim dataFromClient As String = Encoding.Unicode.GetString(bytesFrom)

msg("Data from client - " + TripleDES.DecryptString(dataFromClient))
'msg("Data from client - " + dataFromClient)

'' Data writting to client
Dim serverResponse As String = "Server response " + Convert.ToString(requestCount)
Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(serverResponse))
'Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(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
pdoxtader 16-Apr-13 12:46pm    
Well, the first thing I see is that in the top of sendRequest() you're still encoding for AscII.

"Dim outStream As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(InXML))"

So, your sending an ascII encoded string and trying to convert it to unicode at the other end. This could be your problem...
pdoxtader 16-Apr-13 12:56pm    
If that doesn't work, what you need to to is separate and encapsulate every step of the conversation, and set stops in visual studio. Then as your program runs and stops here and there, look closely at the variable / object values and make sure everything that arrives at the other end is exactly what you expect.

I would take the encryption out of the picture again - especially since you know it works already. If you can test it in a quick test app, and encode and decode some text, then you're probably fine there. So:

I would place a stop after you place your text into a string, and then look at the string. write down how many characters long it is. then another stop after you convert it to a byte array. Write down how many bytes it is... if it's a small enough byte array, mouse over it in the ide and record the values of the bytes. When it arrives at the server, place a stop after it's added to a byte array there, and compare that byte array to the one on the client side. Are they the same length? Do they have identical values? Convert it to a string, and compare the string to the one in the client. Are they the same length? Are they identical? If you still have trouble after making the change I just suggested, I think you'll find that they won't be.
normalsoft 17-Apr-13 2:49am    
Thanks for your support.

strings are identical values. but length is different. In client side, i'm getting 12 (byte array length). But in server side, while from receiving data from network stream, it has length 8193 having rest byte values are 0. ie. 0 - 11 values are same as client side. but 12 - 8192 are zeros.
normalsoft 17-Apr-13 3:05am    
I changed the code as follows. Now its works for me. Thanks for your great support.

Dim bytesFrom(clientSocket.ReceiveBufferSize) As Byte
'networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))

Dim bytesRead As Integer = networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize)
Dim dataFromClient As String = Encoding.ASCII.GetString(bytesFrom, 0, bytesRead)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900