Question Mysql base64 Encoded string

John Florinus

Member
Joined
Jun 11, 2016
Messages
16
Location
Sweden
Programming Experience
1-3
So i have this chat application that i want to encode the chat using base64. I got it working with the encoding but it fails on the decoding. First, it encodes the string of a textbox and sends it to the database, then i have a timer that gets the content of the db to a listbox. Now i have a for each loop to loop trough all items and decode the string of the item, remove the current item, then add the decoded string. And here it fails.

Here is the en
coding and decoding code:
Code:
encode function[COLOR=#000000][FONT=Roboto]
[/FONT][/COLOR]    Public Function encode(ByVal str As String) As String
        Dim bytesToEncode As Byte()
        bytesToEncode = Encoding.UTF8.GetBytes(str)


        Dim encodedText As String
        encodedText = Convert.ToBase64String(bytesToEncode)
        Return encodedText
    End Function

'decode function
    Public Function decode(ByVal str As String) As String
        Dim decodedBytes As Byte()
        decodedBytes = Convert.FromBase64String(str)


        Dim decodedText As String
        decodedText = Encoding.UTF8.GetString(decodedBytes)
        Return decodedText
    End Function


here is the for ea
ch loop:
Code:
        For Each i As String In chatlist.Items
            chatlist.Items.Remove(i)
            chatlist.Items.Add(decode(i))
        Next


now this is the error i get:

Input is not a valid Base-64 string because it contains a character that has not been Base - 64 format , more than two fill characters or invalid characters other than a space character .
( translated from swedish to english )

by the way, the
chat log ( mysql table ) for the listbox is clear.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
You can't change a list in a For Each loop that is enumerating it. If you want to replace each encoded text with the corresponding plain text then you should do it before adding anything to the ListBox. Populate an array, convert all the data and then populate the ListBox. The conversion would be done like this:
For i = 0 To myArray.GetUpperBopund(0)
    myArray(i) = decode(myArray(i))
Next
or you could do it without an explicit loop:
myArray = Array.ConvertAll(myArray, Function(s) decode(s))
You could do it in the ListBox but that's a bad idea.
 

John Florinus

Member
Joined
Jun 11, 2016
Messages
16
Location
Sweden
Programming Experience
1-3
I tried this:
        Dim myarray As Array = chatlist.Items.ToString.ToArray()
        For i = 0 To myarray.GetUpperBound(0)
            myarray(i) = decode(myarray(i))
        Next
        chatlist.Items.Clear()
        For Each i As String In myarray
            chatlist.Items.Add(i).ToString()
        Next

and i get the error: Invalid length for a Base-64 characters die or strand .

i also tried
for each i as array in myarray
chatlist.items.add(i).tostring()
Next
but i get the same error.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
Did you actually look at what that code put into myArray? I'm guessing not. It's not what you wanted anyway. How would creating one String from the Items collection and then turning that into an array be useful? Create the array like this:
Dim myArray = chatlist.Items.Cast(Of String)().ToArray()
That will create a String array containing the actual items.
 

John Florinus

Member
Joined
Jun 11, 2016
Messages
16
Location
Sweden
Programming Experience
1-3
I guess i didn't. Anyways, i tried your variable and replaced it with my old variable, tested it, and got my old error again:
Input is not a valid Base-64 string because it contains a character that has not been Base - 64 format , more than two fill characters or invalid characters other than a space character .
 
Top Bottom