I've seen some other batch files where they convert a file to base 64, do some magic "echo", and whala, here's your file.
I was wondering how exactly could you recreate this? I program in C# so I can code in that to achieve the goal I desire,
and perhaps on the side, create a utility tool to allow for this type of work to be done.
[Question] Using Base64 to store a file in batch
Moderator: DosItHelp
-
- Posts: 36
- Joined: 02 May 2016 18:59
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: [Question] Using Base64 to store a file in batch
The easiest way is the certutil command with the -encode option.
And you can use -decode to convert base64 to a file in the same way.
Code: Select all
Usage:
CertUtil [Options] -encode InFile OutFile
Encode file to Base64
Options:
-f -- Force overwrite
-Unicode -- Write redirected output in Unicode
-UnicodeText -- Write output file in Unicode
-gmt -- Display times as GMT
-seconds -- Display times with seconds and milliseconds
-v -- Verbose operation
-privatekey -- Display password and private key data
-pin PIN -- Smart Card PIN
-sid WELL_KNOWN_SID_TYPE -- Numeric SID
22 -- Local System
23 -- Network Service
24 -- Local Service
CertUtil -? -- Display a verb list (command list)
CertUtil -encode -? -- Display help text for the "encode" verb
CertUtil -v -? -- Display all help text for all verbs
And you can use -decode to convert base64 to a file in the same way.
-
- Posts: 36
- Joined: 02 May 2016 18:59
Re: [Question] Using Base64 to store a file in batch
@ShadowTheif
Thank you for your very helpful reply! This will help me a great amount.
Thank you for your very helpful reply! This will help me a great amount.
Re: [Question] Using Base64 to store a file in batch
This is the code that I use for "encode_b64.bat":
Either drag/drop a file onto this "encode_b64.bat" or you pass the file name as argument when you call it. It automatically creates the batch file to decode and restore your passed file.
Regards
aGerman
Code: Select all
@echo off
for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" (
echo CertUtil.exe not found.
pause
exit /b
)
certutil.exe -f -encode "%~1" "temp.b64~" || (pause&exit /b)
>"decode_%~n1_b64.bat" (
echo(@echo off
echo(for /f %%%%i in ("certutil.exe"^) do if not exist "%%%%~$path:i" (
echo( echo CertUtil.exe not found.
echo( pause
echo( exit /b
echo(^)
echo(^>"temp.b64~" (
for /f "usebackq delims=" %%i in ("temp.b64~") do echo(echo(%%i
echo(^)
echo(certutil.exe -f -decode "temp.b64~" "%~nx1"
echo(del "temp.b64~"
echo(pause
)
del "temp.b64~"
Either drag/drop a file onto this "encode_b64.bat" or you pass the file name as argument when you call it. It automatically creates the batch file to decode and restore your passed file.
Regards
aGerman
Re: [Question] Using Base64 to store a file in batch
Another way to decode base64 using cscript hybrid.
I also use base64 encoding to store data files in batch scripts. I use certutil to encode data, then paste the resulting encoded data removing the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines.
To extract the data, certutil may be used again (it is more simple), but I use this code (more complex, as I am).
This script decode and extract as many data sections as found enclosed between :base64Encoded:section:.
I also use base64 encoding to store data files in batch scripts. I use certutil to encode data, then paste the resulting encoded data removing the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines.
To extract the data, certutil may be used again (it is more simple), but I use this code (more complex, as I am).
This script decode and extract as many data sections as found enclosed between :base64Encoded:section:.
Code: Select all
@echo off
:searchAndDecode
SetLocal EnableDelayedExpansion & set "_FILES_=" & set "fil=" & set "ini="
set "_vbs_file_=%TEMP%\%~n0.vbs"
::search for encoded data
for /F "usebackq tokens=1-3 delims=:" %%1 in (`findstr /B /N ":base64Encoded:" "%~f0"`) do (
if "%%3" EQU "!fil!" (set "_FILES_=!_FILES_!!fil!:!ini!:%%1,") else (set "fil=%%3" & set "ini=%%1")
)
set "_FILES_=%_FILES_:~0,-1%"
rem create a vbscript to do the decoding
for %%# in (%_FILES_%) do (
for /F "tokens=1-3 delims=:" %%1 in ("%%#") do (
(
echo/set outStream=CreateObject("ADODB.Stream"^) ^: outStream.Type=1 ^: outStream.Open
echo/set oFS=CreateObject("Scripting.FileSystemObject"^)
echo/set inStream=oFS.OpenTextFile("%~f0",1,0,0^)
echo/set oXML=CreateObject("MSXml2.DOMDocument"^)
echo/set oItem=oXML.createElement("tmp"^)
echo/oItem.DataType="bin.base64" ^: for i=1 to %%2 step 1 ^: inStream.readline ^: next
echo/do while i^<%%3 ^: oItem.text=inStream.readline ^: decodedBytes=oItem.NodeTypedValue ^: outStream.Write decodedBytes ^: i=i+1 ^: loop
echo/outStream.SaveToFile "%TEMP%\%%1",2 ^: inStream.close ^: outStream.close
echo/set oItem=nothing ^: set oXML=nothing ^: set outStream=nothing ^: set inStream=nothing ^: set oFS=nothing
)>"%_vbs_file_%"
Cscript.exe /B /E:vbs "%_vbs_file_%" >NUL
start notepad "%TEMP%\%%1"
)
)
del /F /Q "%_vbs_file_%" 2>NUL
EndLocal
exit/B 0
:base64Encoded:loremIpsum.txt:
TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Np
bmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgDQp1dCBs
YWJvcmUgZXQgZG9sb3JlIG1hZ25hIGFsaXF1YS4gVXQgZW5pbSBhZCBtaW5pbSB2
ZW5pYW0sIHF1aXMgbm9zdHJ1ZCBleGVyY2l0YXRpb24gdWxsYW1jbyANCmxhYm9y
aXMgbmlzaSB1dCBhbGlxdWlwIGV4IGVhIGNvbW1vZG8gY29uc2VxdWF0LiBEdWlz
IGF1dGUgaXJ1cmUgZG9sb3IgaW4gcmVwcmVoZW5kZXJpdCBpbiB2b2x1cHRhdGUg
DQp2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlh
dHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lk
ZW50LCANCnN1bnQgaW4gY3VscGEgcXVpIG9mZmljaWEgZGVzZXJ1bnQgbW9sbGl0
IGFuaW0gaWQgZXN0IGxhYm9ydW0uDQoNClNlZCB1dCBwZXJzcGljaWF0aXMgdW5k
ZSBvbW5pcyBpc3RlIG5hdHVzIGVycm9yIHNpdCB2b2x1cHRhdGVtIGFjY3VzYW50
aXVtIGRvbG9yZW1xdWUgbGF1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