Skip to content

Instantly share code, notes, and snippets.

@matheuseduardo
Created July 12, 2018 18:50
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save matheuseduardo/6d205904ec66cf4332c91cb539729ce4 to your computer and use it in GitHub Desktop.
Save matheuseduardo/6d205904ec66cf4332c91cb539729ce4 to your computer and use it in GitHub Desktop.
Base64 encoding / decoding functions 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
%>
@domus71
Copy link

domus71 commented Oct 27, 2023

@KarmaBlackshaw

Korean characters are converted into somewhat unreadable strings.

Dim encode
encode = Base64Encode("{""device"":""pc"",""user"":{""id"":10,""level"":1,""login_id"":""lorem29"",""login_name"":""회원가입신청"",""language"":""en""}}")

It returns

{"device":"pc","user":{"id":10,"level":1,"login_id":"lorem29","login_name":"isOi>?e??iz.i< i2-","language":"en"}}

Any idea why and how to solve this?

I use the Base64 class:
https://gist.github.com/domus71/f593fcc2fec3d9a4eb68fdf2034bf11d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment