Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires AutoHotkey >= v2.0 ; Ensures that you're using AHKv2
- #SingleInstance Force ; Only 1 instance of the script can run
- test()
- ExitApp()
- *Esc::ExitApp
- test(){
- ; Correct:
- ; SGVsbG8sIHdvcmxkIQ==
- txt := "Hello, world!"
- enc := buf64_encode(txt)
- dec := buf64_decode(enc)
- MsgBox "start: " txt
- . "`nencode: " enc
- . "`ndecode: " dec
- }
- buf64_encode(strIn, encoding := "UTF-8") {
- static dwFlags := 0x40000001 ; CRYPT_STRING_BASE64 + CRYPT_STRING_NOCRLF
- (encoding = "UTF-8") ; If UTF encoding
- ? suffix := "A" ; Use ANSI func
- : (suffix := "W", encoding := "UTF-16") ; Else set encoding and use wide func
- ,chars := Buffer(4, 0) ; Create chars buffer
- ,bufStr := Buffer(StrPut(strIn, encoding)-1) ; Build string buffer
- ,StrPut(strIn, bufStr, encoding) ; Populate buffer
- bin2str(pszString) =>
- DllCall("Crypt32\CryptBinaryToString" suffix ; Call to appropriate func
- ,"Ptr" ,bufStr ; const BYTE *pbBinary,
- ,"UInt" ,bufStr.Size ; DWORD cbBinary,
- ,"UInt" ,dwFlags ; DWORD dwFlags,
- ,"Ptr" ,pszString ; LPWSTR pszString,
- ,"Ptr" ,chars) ; DWORD *pcchString
- return bin2str(0) ; Call to get size of string out
- ? bin2str(strOut := Buffer(NumGet(chars, "UInt"), 0)) ; Call to get string out
- ? StrGet(strOut, encoding) ; Return encoded string
- : MsgBox("Error getting encoded string.") ? 0 : 0 ; Error getting encoded string
- : MsgBox("Error getting string size.") ? 0 : 0 ; Error getting string out size
- }
- buf64_decode(strIn, encoding := "UTF-8") {
- static CRYPT_STRING_BASE64 := 0x1
- (encoding = "UTF-8") ; If UTF encoding
- ? suffix := "A" ; Use ANSI func
- : (suffix := "W", encoding := "UTF-16") ; Else set encoding and use wide func
- ,bufStr := Buffer(StrPut(strIn, encoding)-1) ; Create chars buffer
- ,StrPut(strIn, bufStr, encoding) ; Build string buffer
- ,chars := Buffer(4, 0) ; Populate buffer
- str2bin(pbBinary) =>
- DllCall("Crypt32\CryptStringToBinary" suffix
- ,"Ptr" , bufStr ;[in] LPCSTR pszString,
- ,"UInt" , bufStr.Size ;[in] DWORD cchString,
- ,"UInt" , CRYPT_STRING_BASE64 ;[in] DWORD dwFlags,
- ,"Ptr" , pbBinary ;[in] BYTE *pbBinary,
- ,"Ptr" , chars ;[in, out] DWORD *pcbBinary,
- ,"Ptr" , 0 ;[out] DWORD *pdwSkip,
- ,"Ptr" , 0 ) ;[out] DWORD *pdwFlags
- return str2bin(0) ; Call to get size of string out
- ? str2bin(strOut := Buffer(NumGet(chars, "UInt"), 0)) ; Call to get string out
- ? StrGet(strOut, encoding) ; Return encoded string
- : MsgBox("Error getting encoded string.") ? 0 : 0 ; Error getting encoded string
- : MsgBox("Error getting string size.") ? 0 : 0 ; Error getting string out size
- }
Add Comment
Please, Sign In to add comment