Simple Base64 Decoder?
Simple Base64 Decoder?
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!
Re: Simple Base64 Decoder?
I did not try, but this looks relevant. viewtopic.php?f=76&t=102033
Re: Simple Base64 Decoder?
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.mikeyww wrote: ↑27 Jun 2022, 09:42I did not try, but this looks relevant. viewtopic.php?f=76&t=102033
Re: Simple Base64 Decoder?
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.
Re: Simple Base64 Decoder?
libcrypt includes base64 https://github.com/ahkscript/libcrypt.ahk
Re: Simple Base64 Decoder?
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?
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
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)
}
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
- AlphaBravo
- Posts: 586
- Joined: 29 Sep 2013, 22:59
Re: Simple Base64 Decoder?
I use this one viewtopic.php?t=3893#p49863
Re: Simple Base64 Decoder?
Thanks for the reply guys.
I've just had to replace
with
then also add
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?
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!iseahound wrote: ↑27 Jun 2022, 20:15Yes, 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?
Could probably replace GlobalAlloc with VarSetCapacityCode: 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) }
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've just had to replace
Code: Select all
str := "RE9LSSBET0tJIExJVEVSQVRVUkUgQ0xVQiBCVVQgSVRTIEEgRElTTkVZIENIQU5ORUwgQ0FSVE9PTiAoQW5pbWF0ZWQgVGhlbWUgUGFyb2R5KSAKaHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1BcFgxV05VNWhvaw=="
Code: Select all
str := clipboard
Code: Select all
clipboard := get_base64(str)
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?
Re: Simple Base64 Decoder?
I do not fully understand what you are asking. Generally I use string replacement functions with a wrapper:
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.
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
}
Re: Simple Base64 Decoder?
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.iseahound wrote: ↑28 Jun 2022, 12:17I do not fully understand what you are asking. Generally I use string replacement functions with a wrapper:
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.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 }
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.