Simple Base64 Decoder?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Epoch
Posts: 41
Joined: 04 Jun 2021, 11:09

Simple Base64 Decoder?

Post by Epoch » 27 Jun 2022, 09:20

I wondered whether AHK can decode a base64 string into normal text instead of having to install yet another browser addon just for that? While searching for "base64"I was only able to find advanced stuff like pBitmap and wav. conversions which are beyond my intended usage (and understanding), rather than a simple script that would merely read a base64 string stored in clipboard and decode it into normal text then replace clipboard with it, store it on a variable or simply open a messagebox with it - any of these would do. Thanks!

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Simple Base64 Decoder?

Post by mikeyww » 27 Jun 2022, 09:42

I did not try, but this looks relevant. viewtopic.php?f=76&t=102033

Epoch
Posts: 41
Joined: 04 Jun 2021, 11:09

Re: Simple Base64 Decoder?

Post by Epoch » 27 Jun 2022, 10:25

mikeyww wrote:
27 Jun 2022, 09:42
I did not try, but this looks relevant. viewtopic.php?f=76&t=102033
Thanks for the reply. I've tried to make some sense out of it but I am afraid this thread goes into dev territory which is certainly beyond my scope of understanding, I mean I cannot even understand whether something in this thread would do the trick, let alone adapt it to my needs. Also I see sha256 mentioned which as far as I know, it's a different thing? I've tried using the code found in the last post by teadrinker viewtopic.php?p=453710#p453710 but it simply returns a "TipVXiucCj2xRJTq0Gjv4bC0yBQ6XWVG3KHiLlICgc=" string no matter what's on my clipboard.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Simple Base64 Decoder?

Post by mikeyww » 27 Jun 2022, 10:32

I'm not an expert in this area. Others here may know & can reply. You can also post a reply in those other threads, so that the subscribers can get notified.

ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: Simple Base64 Decoder?

Post by ahk7 » 27 Jun 2022, 16:06


iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Simple Base64 Decoder?

Post by iseahound » 27 Jun 2022, 20:15

Yes, the main reason is because base64 encoding adds 33% bloat, so if you have a text value that can be represented in Unicode, why would you add an extra 33% to the size of your string? :lol:

Code: Select all

str := "RE9LSSBET0tJIExJVEVSQVRVUkUgQ0xVQiBCVVQgSVRTIEEgRElTTkVZIENIQU5ORUwgQ0FSVE9PTiAoQW5pbWF0ZWQgVGhlbWUgUGFyb2R5KSAKaHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1BcFgxV05VNWhvaw=="
MsgBox % get_base64(str)

   get_base64(str) {
      ; Retrieve the size of bytes from the length of the base64 string.
      str := Trim(str)
      flags := 0x1 ; CRYPT_STRING_BASE64
      size := StrLen(RTrim(str, "=")) * 3 // 4

      bin := DllCall("GlobalAlloc", "uint", 0, "uptr", size, "ptr")
      DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", flags, "ptr", bin, "uint*", size, "ptr", 0, "ptr", 0)

      try return StrGet(bin, size, "CP0")
      finally DllCall("GlobalFree", "ptr", bin)
   }
Could probably replace GlobalAlloc with VarSetCapacity

libcrypt isn't equipped to handle returning the string as a non-byref parameter https://github.com/ahkscript/libcrypt.ahk/blob/deb005b7b59605393a00674a33be1a79871c2806/src/BinStr.ahk#L11

User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Simple Base64 Decoder?

Post by AlphaBravo » 27 Jun 2022, 21:11


Epoch
Posts: 41
Joined: 04 Jun 2021, 11:09

Re: Simple Base64 Decoder?

Post by Epoch » 28 Jun 2022, 11:21

Thanks for the reply guys.
iseahound wrote:
27 Jun 2022, 20:15
Yes, the main reason is because base64 encoding adds 33% bloat, so if you have a text value that can be represented in Unicode, why would you add an extra 33% to the size of your string? :lol:

Code: Select all

str := "RE9LSSBET0tJIExJVEVSQVRVUkUgQ0xVQiBCVVQgSVRTIEEgRElTTkVZIENIQU5ORUwgQ0FSVE9PTiAoQW5pbWF0ZWQgVGhlbWUgUGFyb2R5KSAKaHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1BcFgxV05VNWhvaw=="
MsgBox % get_base64(str)

   get_base64(str) {
      ; Retrieve the size of bytes from the length of the base64 string.
      str := Trim(str)
      flags := 0x1 ; CRYPT_STRING_BASE64
      size := StrLen(RTrim(str, "=")) * 3 // 4

      bin := DllCall("GlobalAlloc", "uint", 0, "uptr", size, "ptr")
      DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", flags, "ptr", bin, "uint*", size, "ptr", 0, "ptr", 0)

      try return StrGet(bin, size, "CP0")
      finally DllCall("GlobalFree", "ptr", bin)
   }
Could probably replace GlobalAlloc with VarSetCapacity

libcrypt isn't equipped to handle returning the string as a non-byref parameter https://github.com/ahkscript/libcrypt.ahk/blob/deb005b7b59605393a00674a33be1a79871c2806/src/BinStr.ahk#L11
I am not going to pretend I understand what's going on behind the scenes (DllCalls etc.) but this seems to do the job, without even resorting to a library. Thank you!
I've just had to replace

Code: Select all

str := "RE9LSSBET0tJIExJVEVSQVRVUkUgQ0xVQiBCVVQgSVRTIEEgRElTTkVZIENIQU5ORUwgQ0FSVE9PTiAoQW5pbWF0ZWQgVGhlbWUgUGFyb2R5KSAKaHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1BcFgxV05VNWhvaw=="
with

Code: Select all

str := clipboard
then also add

Code: Select all

clipboard := get_base64(str)
in order to input the base64 string found on the website then get the decoded text string back into the clipboard.
Can AHK replace the original base64 string into the webpage itself with the decoded string just like some browser addons do, or I'll have to go with alternatives such as storing it back into the clipboard as described/exporting it into a .txt file or something?

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Simple Base64 Decoder?

Post by iseahound » 28 Jun 2022, 12:17

I do not fully understand what you are asking. Generally I use string replacement functions with a wrapper:

Code: Select all

; Highlight text, then press Windows + b.
#b:: Highlight.get_base64() ; Note that no parameters are necessary due to the Highlight wrapper.

get_base64(str) {

   ; Validate base64 string to prevent garbage output.
   if not (str ~= "^\s*(?:[A-Za-z0-9+\/]{4})*+(?:[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?\s*$")
      return

   ; Retrieve the size of bytes from the length of the base64 string.
   str := Trim(str)
   flags := 0x1 ; CRYPT_STRING_BASE64
   size := StrLen(RTrim(str, "=")) * 3 // 4

   bin := DllCall("GlobalAlloc", "uint", 0, "uptr", size, "ptr")
   DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", flags, "ptr", bin, "uint*", size, "ptr", 0, "ptr", 0)

   try return StrGet(bin, size, "CP0")
   finally DllCall("GlobalFree", "ptr", bin)
}

class Highlight extends Highlight.Delegate {
   class Delegate {
      __Call(function, args*) {
         IsObject(function)
            ? Paste(function.call("", Copy(), args*))
            : Paste(%function%(Copy(), args*))
      }
   }
}

Copy() {
   Clip0 := ClipboardAll
   Clipboard := ""               ; Must start off blank for detection to work
   Send ^c
   ClipWait 0.5
   if !ErrorLevel
      s := Clipboard
   Clipboard := Clip0
   VarSetCapacity(Clip0, 0)      ; Free memory
   return s                      ; Allows the empty string ("") for side-effects
}

Paste(s) {
   if (s == "")
      return
   Clip0 := ClipboardAll
   Clipboard := s
   Send ^v
   Sleep 50                      ; Don't change Clipboard while it is pasted! (Sleep > 0)
   Clipboard := Clip0            ; Restore original Clipboard
   VarSetCapacity(Clip0, 0)      ; Free memory
}
If you are talking about something similar to google translate where foreign language text is auto replaced, you must use a browser plugin for that.

Epoch
Posts: 41
Joined: 04 Jun 2021, 11:09

Re: Simple Base64 Decoder?

Post by Epoch » 28 Jun 2022, 16:23

iseahound wrote:
28 Jun 2022, 12:17
I do not fully understand what you are asking. Generally I use string replacement functions with a wrapper:

Code: Select all

; Highlight text, then press Windows + b.
#b:: Highlight.get_base64() ; Note that no parameters are necessary due to the Highlight wrapper.

get_base64(str) {

   ; Validate base64 string to prevent garbage output.
   if not (str ~= "^\s*(?:[A-Za-z0-9+\/]{4})*+(?:[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?\s*$")
      return

   ; Retrieve the size of bytes from the length of the base64 string.
   str := Trim(str)
   flags := 0x1 ; CRYPT_STRING_BASE64
   size := StrLen(RTrim(str, "=")) * 3 // 4

   bin := DllCall("GlobalAlloc", "uint", 0, "uptr", size, "ptr")
   DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", flags, "ptr", bin, "uint*", size, "ptr", 0, "ptr", 0)

   try return StrGet(bin, size, "CP0")
   finally DllCall("GlobalFree", "ptr", bin)
}

class Highlight extends Highlight.Delegate {
   class Delegate {
      __Call(function, args*) {
         IsObject(function)
            ? Paste(function.call("", Copy(), args*))
            : Paste(%function%(Copy(), args*))
      }
   }
}

Copy() {
   Clip0 := ClipboardAll
   Clipboard := ""               ; Must start off blank for detection to work
   Send ^c
   ClipWait 0.5
   if !ErrorLevel
      s := Clipboard
   Clipboard := Clip0
   VarSetCapacity(Clip0, 0)      ; Free memory
   return s                      ; Allows the empty string ("") for side-effects
}

Paste(s) {
   if (s == "")
      return
   Clip0 := ClipboardAll
   Clipboard := s
   Send ^v
   Sleep 50                      ; Don't change Clipboard while it is pasted! (Sleep > 0)
   Clipboard := Clip0            ; Restore original Clipboard
   VarSetCapacity(Clip0, 0)      ; Free memory
}
If you are talking about something similar to google translate where foreign language text is auto replaced, you must use a browser plugin for that.
Not sure what it's supposed to happen when pressing Windows + b, I highlighted some base64 string and pressed the hotkey combination but nothing happened? Meaning, I don't even see anything copied in the clipboard.
And yes, that's pretty much what I meant, addons replacing text with an "in place" translation is a good analogy. Having to use one for Base64 decoding makes sense, AHK is not integrated in the browser so I suppose it can't process a webpage.

Post Reply

Return to “Ask for Help (v1)”