GroggyOtter

My absolute failure at base64 decoding

Jan 31st, 2023 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. str := "Hello, world!"
  2. base64_encode(str)
  3.  
  4. base64_encode(strIn) {
  5. Static dwFlags := 0x1 ; Base64 without headers
  6.  
  7. szStrIn := StrPut(strIn) ; Get size of string
  8. bufStrIn := Buffer(szStrIn) ; Create a buffer for string
  9. StrPut(strIn, bufStrIn) ; put string into buffer
  10.  
  11. ; Showing that the buffer exists along with pointer and size
  12. MsgBox "szStrIn: " szStrIn "`nbufStrIn.Ptr: " bufStrIn.Ptr "`nbufStrIn.Size: " bufStrIn.Size
  13.  
  14. bufSzStrOut := Buffer(4, 0) ; Create buffer for return string size
  15.  
  16. ; Fat arrow function b/c only the last 2 options are changed
  17. cryptBin2Str(pszString, pcchString) =>
  18. DllCall("crypt32\CryptBinaryToString" ; https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptbinarytostringw
  19. ,"Ptr" , bufStrIn.Ptr ; [in] const BYTE *pbBinary,
  20. ,"UInt" , bufStrIn.Size ; [in] DWORD cbBinary,
  21. ,"UInt" , dwFlags ; [in] DWORD dwFlags,
  22. ,"Ptr" , pszString ; [out, optional] LPWSTR pszString,
  23. ,"UInt*" , pcchString) ; [in, out] DWORD *pcchString
  24.  
  25. ; Run bin2str
  26. ; pszString is null because I need to get return string length
  27. ; bufSzStrOut receives the calculated length of return string (in TCHARs)
  28. if !cryptBin2Str(0, bufSzStrOut.Ptr)
  29. MsgBox "CryptBinaryToString failed"
  30.  
  31.  
  32. ; Get return string length from buffer
  33. bSize := NumGet(bufSzStrOut.Ptr, 0, "Uint")
  34.  
  35. ; I can't even get to here without screwing things up
  36. ; No error is thrown by the DLL call but bSize is 0
  37. ; I fcking quit at this. I've wasted hours so far and the only thing I've
  38. ; succeeded at is failing in accomplishing anything
  39. MsgBox(bSize)
  40.  
  41.  
  42.  
  43. ; The rest of the code that I ~WOULD~ have tried if I could actually get the most basic
  44. ; of DllCalls to work
  45. ; But I can't because apparently, I ate a bunch of lead paint chips when I was younger
  46. ; and it has permanently damaged what little bit of smooth brain I actually have
  47.  
  48.  
  49.  
  50. ; Create a buffer for the return string
  51. ; Set string size using bSize
  52. bufStrOut := Buffer(bSize)
  53.  
  54. ; Run bin2str again
  55. ; Pass pointer to string buffer in as well as pointer to size of buffer
  56. if !cryptBin2Str(bufStrOut.Ptr, bufSzStrOut.Ptr)
  57. MsgBox "CryptBinaryToString failed"
  58.  
  59. ; This should show the base64 encoded string
  60. MsgBox(StrGet(bufStrOut,,"UTF-8"))
  61. return
  62. }
Add Comment
Please, Sign In to add comment