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
%>
@matheuseduardo
Copy link
Author

I find a issue. If the result is longer than 72 caracters, this result insert a blank space.

Dim cadena
cadena = "MensajeLargoQueSeraCodificadoPorEsteMetodoEstaEsUnaCadena"
Dim cadenaBase64
cadenaBase64  = Base64Encode(cadena)
Response.Write("Codificado base64: " & cadenaBase64)

Result:
Codificado base64: TWVuc2FqZUxhcmdvUXVlU2VyYUNvZGlmaWNhZG9Qb3JFc3RlTWV0b2RvRXN0YUVzVW5hQ2Fk ZW5h
But the correct result is without the black space:
Codificado base64: TWVuc2FqZUxhcmdvUXVlU2VyYUNvZGlmaWNhZG9Qb3JFc3RlTWV0b2RvRXN0YUVzVW5hQ2FkZW5h

nice! I tried with and without space char and works fine.. I know maybe not the best behavior, but came from "msxml" dll library.
I made a "fast research" (I can't run ASP classic right now, maybe on monday) and we have a alternative..

And even that.. I though is not "space" after 72 chars, but a line break after every 72 chars, but rendering in same line "because" HTML, right? (:

As I said, I can try on monday and I'll give you the right answer!

@matheuseduardo
Copy link
Author

@horaez As I said.. I made some test, like this sample code:

<!--#INCLUDE FILE="./base64.inc.asp"--><%
dim sampleText
sampleText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum " _
    & " has been the industry's standard dummy text ever since the 1500s, when an unknown printer " _ 
    & " took a galley of type and scrambled it to make a type specimen book. It has survived not only " _ 
    & " five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
%>
<pre><%=Base64Encode(sampleText)%></pre>

and base64 "chunk" return by every 72 chars and displayed

TG9yZW0gSXBzdW0gaXMgc2ltcGx5IGR1bW15IHRleHQgb2YgdGhlIHByaW50aW5nIGFuZCB0
eXBlc2V0dGluZyBpbmR1c3RyeS4gTG9yZW0gSXBzdW0gIGhhcyBiZWVuIHRoZSBpbmR1c3Ry
eSdzIHN0YW5kYXJkIGR1bW15IHRleHQgZXZlciBzaW5jZSB0aGUgMTUwMHMsIHdoZW4gYW4g
dW5rbm93biBwcmludGVyICB0b29rIGEgZ2FsbGV5IG9mIHR5cGUgYW5kIHNjcmFtYmxlZCBp
dCB0byBtYWtlIGEgdHlwZSBzcGVjaW1lbiBib29rLiBJdCBoYXMgc3Vydml2ZWQgbm90IG9u
bHkgIGZpdmUgY2VudHVyaWVzLCBidXQgYWxzbyB0aGUgbGVhcCBpbnRvIGVsZWN0cm9uaWMg
dHlwZXNldHRpbmcsIHJlbWFpbmluZyBlc3NlbnRpYWxseSB1bmNoYW5nZWQu

@hakansglm
Copy link

hakansglm commented Apr 25, 2021

@horaez As I said.. I made some test, like this sample code:

<!--#INCLUDE FILE="./base64.inc.asp"--><%
dim sampleText
sampleText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum " _
    & " has been the industry's standard dummy text ever since the 1500s, when an unknown printer " _ 
    & " took a galley of type and scrambled it to make a type specimen book. It has survived not only " _ 
    & " five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
%>
<pre><%=Base64Encode(sampleText)%></pre>

and base64 "chunk" return by every 72 chars and displayed

TG9yZW0gSXBzdW0gaXMgc2ltcGx5IGR1bW15IHRleHQgb2YgdGhlIHByaW50aW5nIGFuZCB0
eXBlc2V0dGluZyBpbmR1c3RyeS4gTG9yZW0gSXBzdW0gIGhhcyBiZWVuIHRoZSBpbmR1c3Ry
eSdzIHN0YW5kYXJkIGR1bW15IHRleHQgZXZlciBzaW5jZSB0aGUgMTUwMHMsIHdoZW4gYW4g
dW5rbm93biBwcmludGVyICB0b29rIGEgZ2FsbGV5IG9mIHR5cGUgYW5kIHNjcmFtYmxlZCBp
dCB0byBtYWtlIGEgdHlwZSBzcGVjaW1lbiBib29rLiBJdCBoYXMgc3Vydml2ZWQgbm90IG9u
bHkgIGZpdmUgY2VudHVyaWVzLCBidXQgYWxzbyB0aGUgbGVhcCBpbnRvIGVsZWN0cm9uaWMg
dHlwZXNldHRpbmcsIHJlbWFpbmluZyBlc3NlbnRpYWxseSB1bmNoYW5nZWQu

I solved that problem via replacing Chr(10) and Chr(13) to empty.

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 = Replace(Replace(oNode.text, Chr(10), ""), Chr(13), "")
Set oNode = Nothing
Set oXML = Nothing

End Function

@hakansglm
Copy link

I solved that problem via replacing Chr(10) and Chr(13) to empty.

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 = Replace(Replace(oNode.text, Chr(10), ""), Chr(13), "")
Set oNode = Nothing
Set oXML = Nothing
End Function

@horaez
Copy link

horaez commented Apr 26, 2021

Thanks @matheuseduardo and @hakansglm for your time!

@KarmaBlackshaw
Copy link

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?

@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