Base64 Encryption

david84

Member
Joined
May 6, 2010
Messages
20
Programming Experience
Beginner
I've found a few bits of code for this but they don't seem to work, can anybody quickly make me one. I used the following for MD5, anything similar would be perfect. thanks.
VB.NET:
    Function MD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
        Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
        bytes = md5Obj.ComputeHash(bytes)
        Dim strResult As String = ""
        For Each b As Byte In bytes
            strResult += b.ToString("x2")
        Next
        Return strResult
    End Function
 
Base64 is a string encoding, not an encryption.
VB.NET:
Return Convert.ToBase64String(bytes)
 
Base64 is a string encoding, not an encryption.
VB.NET:
Return Convert.ToBase64String(bytes)

I've come accross that code many times, how would I, for example encode Textbox1.Text into Textbox2.Text?
Thanks for the quick reply.
 
What do you mean? What is in first textbox? What is supposed to go in second textbox?
 
To convert plain text to bytes you have to use an instance of the System.Text.Encoding class that corresponds to your text encoding, for example UTF8, and it's GetBytes method to translate the string to bytes. Then you can Base64 encode it as in previous example.
VB.NET:
Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(InputTextBox.Text)
 
VB.NET:
        Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text)
        Convert.ToBase64String(bytes)
I have that so far but how do I convert it back to a string so it can be displayed in textbox2?
 
ToBase64String is a function that returns the string after translation, just assign the result to the target textbox.
 
Back
Top