Home » Programming » VB.Net » Base64 encoding / decoding functions in VbScript / Classic ASP

Base64 encoding / decoding functions in VbScript / Classic ASP


With the following functions Base64Encode(sText) and Base64Decode(ByVal vCode), You can easly encode/decode a text in/from base64 in VBScript/Classic ASP:

Function Base64Encode(sText)
  Dim oXML, oNode
  Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
  Set oNode = oXML.CreateElement("base64")
  oNode.dataType = "bin.base64"
  oNode.nodeTypedValue = Stream_StringToBinary(sText)
  Base64Encode = oNode.text
  Set oNode = Nothing
  Set oXML = Nothing
End Function

Function Base64Decode(ByVal vCode)
  Dim oXML, oNode
  Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
  Set oNode = oXML.CreateElement("base64")
  oNode.dataType = "bin.base64"
  oNode.text = vCode
  Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
  Set oNode = Nothing
  Set oXML = Nothing
End Function

Private Function Stream_StringToBinary(Text)
  Const adTypeText = 2
  Const adTypeBinary = 1
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")
  BinaryStream.Type = adTypeText
  BinaryStream.CharSet = "us-ascii"
  BinaryStream.Open
  BinaryStream.WriteText Text
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeBinary
  BinaryStream.Position = 0
  Stream_StringToBinary = BinaryStream.Read
  Set BinaryStream = Nothing
End Function

Private Function Stream_BinaryToString(Binary)
  Const adTypeText = 2
  Const adTypeBinary = 1
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")
  BinaryStream.Type = adTypeBinary
  BinaryStream.Open
  BinaryStream.Write Binary
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeText
  BinaryStream.CharSet = "us-ascii"
  Stream_BinaryToString = BinaryStream.ReadText
  Set BinaryStream = Nothing
End Function


4 Responses

  1. lasertest
    | Reply

    Great post. I used to be checking continuously this blog and
    I’m inspired! Very useful information particularly the closing
    section 🙂 I deal with such information a lot. I was looking for this particular information for a long time.

    Thank you and good luck.

  2. JK. Emily Kim
    | Reply

    Thank you!!!! Very very easy 🙂

  3. ZioNN
    | Reply

    How is it possible to encode / decode latin characters like “ç”? I´ve tested your code and it didnt work. What should I change? Thanks

  4. Dman
    | Reply

    Thanks for the code but beware. I was testing this code in an app and the file kept being deleted on my system. Looking for the cause, I found my antivirus program was quarantining and deleting it, even after I restoring it multiple times. The Avast antivirus program identifies files with this script as the VBS:downloader-JK [Tr] virus. The code itself appears fine, but it’s likely this code is part of that virus and therefore is deemed malicious by some programs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.