GroggyOtter

b64 buff enc/dec

Feb 2nd, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #Requires AutoHotkey >= v2.0 ; Ensures that you're using AHKv2
  2. #SingleInstance Force ; Only 1 instance of the script can run
  3. test()
  4. ExitApp()
  5.  
  6. *Esc::ExitApp
  7.  
  8. test(){
  9. ; SGVsbG8sIHdvcmxkIQ==
  10. txt := "Hello, world!"
  11. enc := buf64_encode(txt)
  12. dec := buf64_decode(enc)
  13. MsgBox "start: " txt
  14. . "`nencode: " enc
  15. . "`ndecode: " dec
  16. }
  17.  
  18. buf64_encode(strIn, encoding := "UTF-8") {
  19. static CRYPT_STRING_BASE64 := 0x1
  20.  
  21. if (encoding = "UTF-8")
  22. suffix := "A"
  23. else
  24. suffix := "W"
  25. ,encoding := "UTF-16"
  26.  
  27. bufStr := Buffer(StrPut(strIn, encoding))
  28. StrPut(strIn, bufStr, encoding)
  29. chars := Buffer(4, 0)
  30.  
  31. if !DllCall("Crypt32\CryptBinaryToString" suffix
  32. ,"Ptr" ,bufStr ; const BYTE *pbBinary,
  33. ,"UInt" ,bufStr.Size ; DWORD cbBinary,
  34. ,"UInt" ,CRYPT_STRING_BASE64 ; DWORD dwFlags,
  35. ,"Ptr" ,0 ; LPWSTR pszString,
  36. ,"Ptr" ,chars) ; DWORD *pcchString
  37. MsgBox("Error getting chars")
  38.  
  39. strOut := Buffer(NumGet(chars, "UInt"), 0)
  40.  
  41. if !DllCall("Crypt32\CryptBinaryToString" suffix
  42. ,"Ptr" ,bufStr ; const BYTE *pbBinary,
  43. ,"UInt" ,bufStr.Size ; DWORD cbBinary,
  44. ,"UInt" ,CRYPT_STRING_BASE64 ; DWORD dwFlags,
  45. ,"Ptr" ,strOut ; LPWSTR pszString,
  46. ,"Ptr" ,chars) ; DWORD *pcchString
  47. MsgBox("Error getting string")
  48.  
  49. return StrGet(strOut, encoding)
  50. }
  51.  
  52. buf64_decode(strIn, encoding := "UTF-8") {
  53. static CRYPT_STRING_BASE64 := 0x1
  54.  
  55. if (encoding = "UTF-8")
  56. suffix := "A"
  57. else
  58. suffix := "W"
  59. ,encoding := "UTF-16"
  60.  
  61. bufStr := Buffer(StrPut(strIn, encoding))
  62. StrPut(strIn, bufStr, encoding)
  63. chars := Buffer(4, 0)
  64.  
  65. if (!DllCall("Crypt32\CryptStringToBinary" suffix
  66. ,"Ptr" , bufStr ;[in] LPCSTR pszString,
  67. ,"UInt" , bufStr.Size ;[in] DWORD cchString,
  68. ,"UInt" , CRYPT_STRING_BASE64 ;[in] DWORD dwFlags,
  69. ,"Ptr" , 0 ;[in] BYTE *pbBinary,
  70. ,"Ptr" , chars ;[in, out] DWORD *pcbBinary,
  71. ,"Ptr" , 0 ;[out] DWORD *pdwSkip,
  72. ,"Ptr" , 0 )) ;[out] DWORD *pdwFlags
  73. MsgBox "Error getting decode size"
  74.  
  75. strOut := Buffer(NumGet(chars, "UInt"))
  76.  
  77. if (!DllCall("Crypt32\CryptStringToBinary" suffix
  78. ,"Ptr" , bufStr ;[in] LPCSTR pszString,
  79. ,"UInt" , bufStr.Size ;[in] DWORD cchString,
  80. ,"UInt" , CRYPT_STRING_BASE64 ;[in] DWORD dwFlags,
  81. ,"Ptr" , strOut ;[in] BYTE *pbBinary,
  82. ,"Ptr" , chars ;[in, out] DWORD *pcbBinary,
  83. ,"Ptr" , 0 ;[out] DWORD *pdwSkip,
  84. ,"Ptr" , 0 )) ;[out] DWORD *pdwFlags
  85. MsgBox "Error getting decode size"
  86.  
  87. return StrGet(strOut, encoding)
  88. }
Add Comment
Please, Sign In to add comment