I'm endlessly searching forum for simple Binary (exe file) to Base64 encoded txt file converting function. I found lots of examples but lots of them are String to Base64 encoders. Some of them Binary2base64 but not able to decode back. Some of them not universal base64's cant decode back in other patforms.
Some people refer to https://github.com/ahkscript/libcrypt.ahk but that example over there is out of my understanding level.
Just want to chose a binary file and get the txt file and decode it anywhere.
Thank for helping.
Can you please share a working Binary to Base64 Encode function? Topic is solved
-
- Posts: 104
- Joined: 07 Aug 2015, 15:53
-
- Posts: 104
- Joined: 07 Aug 2015, 15:53
Re: Can you please share a working Binary to Base64 Encode function?
It doesnt work universally. See the last three answers in refered post. It add spaces between lines and the solution mentioned there is not solving the problem.
So my search continues..
Re: Can you please share a working Binary to Base64 Encode function? Topic is solved
Works fine here. The following example will encode AutoHotkey.exe to base64, and then decode it to ahkCopy.exe. I've also tried a command line tool base64, it can decode it back.
Code: Select all
b64 := Base64Enc_FromFile(A_AhkPath)
Base64Dec_ToFile(b64, "ahkCopy.exe")
Run, cmd /c echo MsgBox hello | ahkCopy.exe *
; FileOpen("B64Data.txt", "w").Write(b64)
; RunWait, base64.exe --decode B64Data.txt ahkCopy2.exe
ExitApp
Base64Enc( ByRef Bin, nBytes, LineLength := 64, LeadingSpaces := 0 ) { ; By SKAN / 18-Aug-2017
Local Rqd := 0, B64, B := "", N := 0 - LineLength + 1 ; CRYPT_STRING_BASE64 := 0x1
DllCall( "Crypt32.dll\CryptBinaryToString", "Ptr",&Bin ,"UInt",nBytes, "UInt",0x1, "Ptr",0, "UIntP",Rqd )
VarSetCapacity( B64, Rqd * ( A_Isunicode ? 2 : 1 ), 0 )
DllCall( "Crypt32.dll\CryptBinaryToString", "Ptr",&Bin, "UInt",nBytes, "UInt",0x1, "Str",B64, "UIntP",Rqd )
If ( LineLength = 64 and ! LeadingSpaces )
Return B64
B64 := StrReplace( B64, "`r`n" )
Loop % Ceil( StrLen(B64) / LineLength )
B .= Format("{1:" LeadingSpaces "s}","" ) . SubStr( B64, N += LineLength, LineLength ) . "`n"
Return RTrim( B,"`n" )
}
Base64Dec( ByRef B64, ByRef Bin ) { ; By SKAN / 18-Aug-2017
Local Rqd := 0, BLen := StrLen(B64) ; CRYPT_STRING_BASE64 := 0x1
DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
, "UInt",0, "UIntP",Rqd, "Int",0, "Int",0 )
VarSetCapacity( Bin, 128 ), VarSetCapacity( Bin, 0 ), VarSetCapacity( Bin, Rqd, 0 )
DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
, "Ptr",&Bin, "UIntP",Rqd, "Int",0, "Int",0 )
Return Rqd
}
Base64Enc_FromFile(FileName) {
FileGetSize, nBytes, %FileName%
FileRead, Bin, *c %FileName%
return Base64Enc(Bin, nBytes)
}
Base64Dec_ToFile(ByRef B64, FileName) {
nBytes := Base64Dec(B64, Bin)
FileOpen(FileName, "w").RawWrite(Bin, nBytes)
}
-
- Posts: 4309
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Can you please share a working Binary to Base64 Encode function?
This is a bit weird approach. There is CRYPT_STRING_NOCRLF flag.