Decode Base64 into Variable

Discuss RoboTask here
Post Reply
george
Posts: 25
Joined: Sun Mar 25, 2018 3:02 pm

Decode Base64 into Variable

Post by george »

Hi Oleg, I have a Variable saved from an e-mail text that is in Base64 and need to decode it and then save it to a new Variable.

I installed the Basic Script function but I can't find the script to make this happen.

Can you help me?

Thanks
Oleg
Site Admin
Posts: 3004
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Decode Base64 into Variable

Post by Oleg »

To decode base64 string use this script (see below)
This is published here

Code: Select all

Function Base64Decode(ByVal base64String)
  'rfc1521
  '1999 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim dataLength, sOut, groupBegin

  'remove white spaces, If any
  base64String = Replace(base64String, vbCrLf, "")
  base64String = Replace(base64String, vbTab, "")
  base64String = Replace(base64String, " ", "")

  'The source must consists from groups with Len of 4 chars
  dataLength = Len(base64String)
  If dataLength Mod 4 <> 0 Then
    Err.Raise 1, "Base64Decode", "Bad Base64 string."
    Exit Function
  End If


  ' Now decode each group:
  For groupBegin = 1 To dataLength Step 4
    Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
    ' Each data group encodes up To 3 actual bytes.
    numDataBytes = 3
    nGroup = 0

    For CharCounter = 0 To 3
      ' Convert each character into 6 bits of data, And add it To
      ' an integer For temporary storage.  If a character is a '=', there
      ' is one fewer data byte.  (There can only be a maximum of 2 '=' In
      ' the whole string.)

      thisChar = Mid(base64String, groupBegin + CharCounter, 1)

      If thisChar = "=" Then
        numDataBytes = numDataBytes - 1
        thisData = 0
      Else
        thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1
      End If
      If thisData = -1 Then
        Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
        Exit Function
      End If

      nGroup = 64 * nGroup + thisData
    Next

    'Hex splits the long To 6 groups with 4 bits
    nGroup = Hex(nGroup)

    'Add leading zeros
    nGroup = String(6 - Len(nGroup), "0") & nGroup

    'Convert the 3 byte hex integer (6 chars) To 3 characters
    pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
      Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
      Chr(CByte("&H" & Mid(nGroup, 5, 2)))

    'add numDataBytes characters To out string
    sOut = sOut & Left(pOut, numDataBytes)
  Next

  Base64Decode = sOut
End Function
But pay attention that base64 format can contain some binary data and can't be saved into RoboTask variable

Also look at my example

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|1075658632
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task445"
Hide=INTEGER|0
ID=INTEGER|298128661
LogOnAsUser=INTEGER|1
Name=STRING|"base64 decode"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""ENCODED"" with value ""SGVsbG8gV29ybGQhISE="""
Params=FOLDER

[Actions\Action1\Params]
expand=STRING|"0"
varname=STRING|"ENCODED"
varvalue=STRING|"SGVsbG8gV29ybGQhISE="

[Actions\Action2]
ActionID=STRING|"A_SCRIPT_VBEVALUATE"
Enabled=INTEGER|-1
Name=STRING|"VB Evaluate"
Params=FOLDER

[Actions\Action2\Params]
expression=STRING|"Base64Decode(""{encoded}"")"
line00000000=STRING|"Function Base64Decode(ByVal base64String)"
line00000001=STRING|"  'rfc1521"
line00000002=STRING|"  '1999 Antonin Foller, Motobit Software, http://Motobit.cz"
line00000003=STRING|"  Const Base64 = ""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"""
line00000004=STRING|"  Dim dataLength, sOut, groupBegin"
line00000006=STRING|"  'remove white spaces, If any"
line00000007=STRING|"  base64String = Replace(base64String, vbCrLf, """")"
line00000008=STRING|"  base64String = Replace(base64String, vbTab, """")"
line00000009=STRING|"  base64String = Replace(base64String, "" "", """")"
line0000000B=STRING|"  'The source must consists from groups with Len of 4 chars"
line0000000C=STRING|"  dataLength = Len(base64String)"
line0000000D=STRING|"  If dataLength Mod 4 <> 0 Then"
line0000000E=STRING|"    Err.Raise 1, ""Base64Decode"", ""Bad Base64 string."""
line0000000F=STRING|"    Exit Function"
line00000010=STRING|"  End If"
line00000013=STRING|"  ' Now decode each group:"
line00000014=STRING|"  For groupBegin = 1 To dataLength Step 4"
line00000015=STRING|"    Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut"
line00000016=STRING|"    ' Each data group encodes up To 3 actual bytes."
line00000017=STRING|"    numDataBytes = 3"
line00000018=STRING|"    nGroup = 0"
line0000001A=STRING|"    For CharCounter = 0 To 3"
line0000001B=STRING|"      ' Convert each character into 6 bits of data, And add it To"
line0000001C=STRING|"      ' an integer For temporary storage.  If a character is a '=', there"
line0000001D=STRING|"      ' is one fewer data byte.  (There can only be a maximum of 2 '=' In"
line0000001E=STRING|"      ' the whole string.)"
line00000020=STRING|"      thisChar = Mid(base64String, groupBegin + CharCounter, 1)"
line00000022=STRING|"      If thisChar = ""="" Then"
line00000023=STRING|"        numDataBytes = numDataBytes - 1"
line00000024=STRING|"        thisData = 0"
line00000025=STRING|"      Else"
line00000026=STRING|"        thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1"
line00000027=STRING|"      End If"
line00000028=STRING|"      If thisData = -1 Then"
line00000029=STRING|"        Err.Raise 2, ""Base64Decode"", ""Bad character In Base64 string."""
line0000002A=STRING|"        Exit Function"
line0000002B=STRING|"      End If"
line0000002D=STRING|"      nGroup = 64 * nGroup + thisData"
line0000002E=STRING|"    Next"
line00000030=STRING|"    'Hex splits the long To 6 groups with 4 bits"
line00000031=STRING|"    nGroup = Hex(nGroup)"
line00000033=STRING|"    'Add leading zeros"
line00000034=STRING|"    nGroup = String(6 - Len(nGroup), ""0"") & nGroup"
line00000036=STRING|"    'Convert the 3 byte hex integer (6 chars) To 3 characters"
line00000037=STRING|"    pOut = Chr(CByte(""&H"" & Mid(nGroup, 1, 2))) + _"
line00000038=STRING|"      Chr(CByte(""&H"" & Mid(nGroup, 3, 2))) + _"
line00000039=STRING|"      Chr(CByte(""&H"" & Mid(nGroup, 5, 2)))"
line0000003B=STRING|"    'add numDataBytes characters To out string"
line0000003C=STRING|"    sOut = sOut & Left(pOut, numDataBytes)"
line0000003D=STRING|"  Next"
line0000003F=STRING|"  Base64Decode = sOut"
line00000040=STRING|"End Function"
line00000042=STRING|"'logmessage(Base64Decode(""SGVsbG8gV29ybGQhISE=""))"
linecount=STRING|"67"
loadfromfile=STRING|"1"
variable=STRING|"decoded"

[Actions\Action3]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""Encoded: {encoded}"""
Params=FOLDER

[Actions\Action3\Params]
icon=STRING|"1"
msg0=STRING|"Encoded: {encoded}"
msg1=STRING|"*******"
msg2=STRING|"Decoded: {decoded}"
msgcount=STRING|"3"
playsound=STRING|"0"
showmessage=STRING|"1"
We plan to implement base64 decoding and encoding in next release.

In addition look here how to encode string to base64 format
Oleg Yershov
george
Posts: 25
Joined: Sun Mar 25, 2018 3:02 pm

Re: Decode Base64 into Variable

Post by george »

Thanks!! :mrgreen:
Post Reply