Results 1 to 5 of 5

Thread: [RESOLVED] String properties (base64)

  1. #1

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Resolved [RESOLVED] String properties (base64)

    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:

    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
    Encyption Class:

    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:

    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
    Thanks in advance!
    Life runs on code

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: String properties (base64)

    You should be using FromBase64String() on the receiving end. You'll also want to decrypt the bytes after doing that.

  3. #3

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: String properties (base64)

    Thanks! I changed as u told me to but still i get nothing :/

    Here:

    Server:

    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)
                
    
                Dim b As Byte() = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesRead))
    
                Debug.Print(System.Text.Encoding.UTF8.GetString(b))
    
    
            Loop
        End Sub
    EDIT: Also I noticed that Client is sending 88 bytes of data, Server reading 88 bytes of data but b is returning 64 bytes of data Oo
    Last edited by Skatebone; Jun 27th, 2011 at 10:41 AM.
    Life runs on code

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: String properties (base64)

    Decoding data needs to work in reverse order of how you encoded it. You aren't paying any attention to detail.

    This is how the server encodes the string, ignoring the "strip null chars" step that can't be redone on the other end:
    Code:
    * Convert the string to ASCII bytes.
    * Encrypt the bytes using Rijndael.
    * Convert the encrypted data to a Base 64 string.
    So whatever receives the data has a Base 64 string that represents encrypted bytes that represent an ASCII string. Here's how you decode it:
    Code:
    * Convert the bytes to a string using UTF-8. (Likely correct.)
    * Decode the Base 64 string to an array of bytes. (Correct.)
    * Convert the bytes to a string using UTF-8. (Incorrect.)
    Once you decode from Base 64, what you've got is encrypted bytes. You have to decrypt them, then use ASCII to go from bytes to strings if you want to get the data that was sent. You should be doing this:
    Code:
    * Convert the bytes to a string.
    * Decode the Base 64 string to the array of encrypted bytes.
    * Decrypt the bytes using Rijndael with the same key and IV used for encryption.
    * Convert the bytes to a string using ASCII.

  5. #5

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: String properties (base64)

    Solved! Thanks alot )
    Last edited by Skatebone; Jun 27th, 2011 at 01:18 PM.
    Life runs on code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width