Hey guys, I am trying to send data from client to server (encypted) and the string sent from the client is not the same as recieved from the server. I've been killing my brain for hours to get this to work but I cant manage. Im converting everything to Base64 string.
Client:
Encyption Class:Code:Public Sub SendMessage(ByVal msg As String) Try SyncLock myclient.GetStream sw = New IO.StreamWriter(myclient.GetStream) sw.Write(enc.EncryptString128Bit(msg, "key")) Debug.Print(enc.EncryptString128Bit(msg, "key")) sw.Flush() End SyncLock Catch ex As Exception MessageBox.Show("Unable to connect to Server!") End Try End Sub
Code:Public Function EncryptString128Bit(ByVal vstrTextToBeEncrypted As String, _ ByVal vstrEncryptionKey As String) As String Dim bytValue() As Byte Dim bytKey() As Byte Dim bytEncoded() As Byte Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62} Dim intLength As Integer Dim intRemaining As Integer Dim objMemoryStream As New MemoryStream() Dim objCryptoStream As CryptoStream Dim objRijndaelManaged As RijndaelManaged vstrTextToBeEncrypted = StripNullCharacters(vstrTextToBeEncrypted) bytValue = Encoding.ASCII.GetBytes(vstrTextToBeEncrypted.ToCharArray) intLength = Len(vstrEncryptionKey) If intLength >= 32 Then vstrEncryptionKey = Strings.Left(vstrEncryptionKey, 32) Else intLength = Len(vstrEncryptionKey) intRemaining = 32 - intLength vstrEncryptionKey = vstrEncryptionKey & Strings.StrDup(intRemaining, "X") End If bytKey = Encoding.ASCII.GetBytes(vstrEncryptionKey.ToCharArray) objRijndaelManaged = New RijndaelManaged() Try objCryptoStream = New CryptoStream(objMemoryStream, _ objRijndaelManaged.CreateEncryptor(bytKey, bytIV), _ CryptoStreamMode.Write) objCryptoStream.Write(bytValue, 0, bytValue.Length) objCryptoStream.FlushFinalBlock() bytEncoded = objMemoryStream.ToArray objMemoryStream.Close() objCryptoStream.Close() Catch End Try Return Convert.ToBase64String(bytEncoded) End Function
Server:
Thanks in advance!Code:Private Sub doRead() Dim networkStream As NetworkStream = MyClient.GetStream() Do Dim readBuffer(Length - 1) As Byte Dim bytesRead As Integer = MyClient.GetStream.Read(readBuffer, 0, Length) Debug.Print(Convert.ToBase64String(readBuffer, Base64FormattingOptions.None, bytesRead)) Loop End Sub




Reply With Quote