VB.NET - Encode Decode - Asked By silna chandra on 10-Dec-08 07:12 AM

hi

Please post vb code for Encode and decode of strings.

I have a file named a.txt

It contains two strings as i/p which are delimited by "|"

 

ABC Corp Ltd | Direct State

How to Encode this string and write it in the same File at Same location .

?

thnks

silna

useful - selva murugan replied to silna chandra on 10-Dec-08 07:18 AM

Dim vencode64 As String = Convert.FromBase64String("Hello World")

the above give me an error because Convert.FromBase64String("Hello World") will resulted as byte() instead of string

but what i want is coverting "hello world" into something like "SGVsbG8gV29ybGQ=" as string

"base64_encode()" is the method used in PHP to convert a string to base64.

how it's done in asp.net??
byte[] ToByte = System.Text.Encoding.Default.GetBytes("Hello world"); 
		string base64String = System.Convert.ToBase64String(ToByte);
http://www.vbforums.com/showthread.php?t=287324
http://www.motobit.com/util/base64-decoder-encoder.asp

 
 

Can you be more specific with "encode"? - Peter Bromberg replied to silna chandra on 10-Dec-08 07:22 AM

It sounds like you are asking how to use the Split and Join method of the String class.

Plz Post code for vb.net not C# - silna chandra replied to selva murugan on 10-Dec-08 07:25 AM

hi

thks for ur reply but I don't have much idea about c#  so Please post the codes in vb.net

thks

silna

Like This - silna chandra replied to Peter Bromberg on 10-Dec-08 07:31 AM

Hi

I m new to vb.net but I know split is used for separating strings from delimiters .

I don't need that 

For Encode Look at this Function


Public Function ToBase64(ByVal data() As Byte) As String

If data Is Nothing Then Throw New ArgumentNullException("data")

Return Convert.ToBase64String(data)

End Function

Public Function FromBase64(ByVal base64 As String) As Byte()

If base64 Is Nothing Then Throw New ArgumentNullException("base64")

Return Convert.FromBase64String(base64)

End Function

 

I m not able to call this function properly

Dim abyte As Byte()

abyte = FromBase64("orignal string")

It should retutrn some encoded string and i will store that to same file

[][][][][][][][]

In this manner

Later on use [][]][][][][] for decode function to get output orignal string

 

Your ToBase64 method is asking for a single byte not a byte array. - Peter Bromberg replied to silna chandra on 10-Dec-08 09:36 AM

Should be:

Public Function ToBase64(ByVal data() As Byte()) As String

Plz be more specific - silna chandra replied to Peter Bromberg on 10-Dec-08 09:52 AM

hi

Please be more specific

I m not able to call both the functions from Main

FromBase64(ByVal base64 As String)

It's giving error


thks

silna

i need this - silna chandra replied to Peter Bromberg on 10-Dec-08 09:55 AM

hi

It would be helpful to me if u could demonstrate how to call both the functions from main

with resp declarations.

and i hope that i will be able to write the encoded string to a file using

Dim writer As StreamWriter = New StreamWriter("F:\BIC\Company.txt")

and then use decode funcn to decode the encoded string

thks

anlis

Reply - Binny ch replied to silna chandra on 10-Dec-08 10:24 AM

VB.NET also contains MD5. It's in System.Security.Cryptography Namespace

Function getMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5 object.
        Dim md5Hasher As MD5 = MD5.Create()

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()

    End Function


    ' Verify a hash against a string.
    Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
        ' Hash the input.
        Dim hashOfInput As String = getMd5Hash(input)

        ' Create a StringComparer an comare the hashes.
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase

        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If

    End Function

Go through thses links:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx



VB.NET example to base-64 encode and decode a string - Binny ch replied to silna chandra on 10-Dec-08 10:25 AM

Dim crypt As New Chilkat.Crypt2()

'  Any string argument automatically begins the 30-day trial.
Dim success As Boolean
success = crypt.UnlockComponent("30-day trial")
If (success <> true) Then
    MsgBox("Crypt component unlock failed")
    Exit Sub
End If


Dim s As String
s = "A friend called me up the other day and talked about investing in a dot-com that sells lobsters. Internet lobsters. Where will this end? --Donald Trump"

crypt.CryptAlgorithm = "none"
crypt.EncodingMode = "base64"

'  Other possible EncodingMode settings are:
'  "quoted-printable", "hex", and "url"

Dim qp As String
qp = crypt.EncryptStringENC(s)

TextBox1.Text = TextBox1.Text & qp & vbCrLf
TextBox1.Refresh()

Dim decoded As String
decoded = crypt.DecryptStringENC(qp)

TextBox1.Text = TextBox1.Text & decoded & vbCrLf
TextBox1.Refresh()

Alos see this link:

http://www.example-code.com/vbdotnet/base64-encode-decode.asp


TRY THIS CODE - C_A P replied to silna chandra on 11-Dec-08 01:55 AM
Encoding Strings using Escape Characters: Basic Example

Public Function Encode(ByVal Val As String) As String
    Dim i As Long, curChar As String

    For i = 1 To Len(Val)
        curChar = Mid(Val, i, 1)
        If curChar = vbCr Then
            Encode = Encode & "\c"
        ElseIf curChar = vbLf Then
            Encode = Encode & "\l"
        ElseIf curChar = "\" Then
            Encode = Encode & "\\"
        Else
            Encode = Encode & curChar
        End If
    Next i

End Function

Public Function Decode(ByVal Val As String) As String
    Dim i As Long, curChar As String, EscapeMode As Boolean
    
    For i = 1 To Len(Val)
        curChar = Mid(Val, i, 1)
        If EscapeMode = False Then
            If curChar = "\" Then
                EscapeMode = True
            Else
                Decode = Decode & curChar
            End If
        Else
            If curChar = "\" Then
                Decode = Decode & "\"
            ElseIf curChar = "c" Then
                Decode = Decode & vbCr
            ElseIf curChar = "l" Then
                Decode = Decode & vbLf
            End If
            EscapeMode = False
        End If
    Next i
    
End Function