VB - base64 encode/decode

A Colleague was working on removing a 3rd party component from an old ASP website. The only function still being provided by the 3rd party component was Base64 encoding. He was trying to get Base64 working directly in vbscript. We eventually decided to go with a VB6 DLL that would expose Encode/Decode functions which in turn would use the MSXML library to create a Base64 encoded string.

Here's the code...

Option Explicit

Public Function Encode(ByRef StringToEncode As String) As String

   Dim lobjXML As MSXML2.DOMDocument
   Dim lobjNode As MSXML2.IXMLDOMElement
   Dim lConvertedString() As Byte
 
   Set lobjXML = New MSXML2.DOMDocument
   Set lobjNode = objXML.createElement("b64")
   lobjNode.dataType = "bin.base64"
 
   ConvertedString = StrConv(StringToEncode, vbFromUnicode) ' Convert to ByteArray
 
   lobjNode.nodeTypedValue = ConvertedString
   Encode = lobjNode.Text
 
   Set lobjNode = Nothing
   Set lobjXML = Nothing

End Function

Public Function Decode(ByVal EncodedString As String) As String

   Dim lobjXML As MSXML2.DOMDocument
   Dim lobjNode As MSXML2.IXMLDOMElement
   Dim lDecodedString As String
 
   Set lobjXML = New MSXML2.DOMDocument
   Set lobjNode = objXML.createElement("b64")
   lobjNode.dataType = "bin.base64"
   lobjNode.Text = strData
 
   Decode = StrConv(lobjNode.nodeTypedValue, vbUnicode) 'Convert it back into a string to return
 
   Set lobjNode = Nothing
   Set lobjXML = Nothing

End Function

0 comments: