I wrote a batch file a while back that
converts a file to base64.
setlocal
setdos /x-45678
set fh=%@fileopen[%1,r,b]
set r=%@filereadb[%fh,3]
set base64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
set ofs=0
do while %r != **EOF**
set
wds=%@words[%r]
set
w=%@eval[(%@word[0,%r] SHL 16) + (%@word[1,%r] SHL 8) + %@word[2,%r]]
rem
Deconstruct the three bytes (24 bits) into the four 6-bit chunks
echos
%@instr[%@eval[(%w SHR 18) AND 0x3F],1,%base64]
echos
%@instr[%@eval[(%w SHR 12) AND 0x3F],1,%base64]
iff
%wds ge 2 then
echos %@instr[%@eval[(%w SHR 6) AND 0x3F],1,%base64]
else
echos endiff
iff
%wds ge 3 then
echos %@instr[%@eval[%w AND 0x3F],1,%base64]
else
echos endiff
set
/a ofs+=4
iff
%ofs ge 72 then
echo.
set ofs=0
endiff
set
r=%@filereadb[%fh,3]
enddo
set fh=%@fileclose[%fh]
if %ofs != 0 echo.
endlocal
The following will take a single base64
encoded line and decode it. I didn't have the time to finish making
it work with files.
setlocal
setdos /x-45678
set base64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/set ofs=0
set wds=%@len[%1]
rem TCC strips trailing equal signs.
So just add some on the end so we won't have issues.
set i=%1=set resultdo while %ofs LT %wds
set
b1=%@instr[%ofs,1,%i]
set
ofs=%@inc[%ofs]
set
b2=%@instr[%ofs,1,%i]
set
ofs=%@inc[%ofs]
set
b3=%@instr[%ofs,1,%i]
set
ofs=%@inc[%ofs]
set
b4=%@instr[%ofs,1,%i]
set
ofs=%@inc[%ofs]
set
i1=%@eval[%@regexindex[%b1,%base64] AND 0x3F]
set
i2=%@eval[%@regexindex[%b2,%base64] AND 0x3F]
set
i3=%@eval[%@regexindex[%b3,%base64] AND 0x3F]
set
i4=%@eval[%@regexindex[%b4,%base64] AND 0x3F]
set
w1=%@eval[(%i1 SHL 2) OR (%i2 SHR 6)]
set
w2=%@eval[((%i2 SHL 4) AND 0xF0) OR (%i3 SHR 2)]
set
w3=%@eval[((%i3 SHL 6) AND 0xC0) OR %i4]
set
result=%[result]%@char[%w1]
iff
%@ascii[%b3] != 61 then
set result=%[result]%@char[%w2]
iff %@ascii[%b4] != 61 then
set result=%[result]%@char[%w3]
endiff
endiff
enddo
echo %result
endlocal
You could put the above into a b64decode.btm
file, then create a decode function:
function decode=`%@execstr[b64decode
%1]`
and use it within your script as follows:
set password=%@decode[U3VwZXJTZWNyZXRQYXNzd29yZA0K]
If you run the first script (named base64):
base64 con
it will get its input from the command
line and display the encoded result to the display. Just hit Ctrl+Z or
Ctrl+C to terminate.
-Scott
> > -----Original Message-----
> > From: thorsten
> > Sent: Sunday, 5 June 2011 10:22 p.m.
> > Subject: [Support-t-2905] Obfuscating passwords in script
> >
> >
> > Hi,
> >
> > is there a technique/command how to obfuscate passwords in a
> > batch file?
> >
> > In Python I used to decode rot13 or base64 to achieve this.
> >
> > I'm not interested in lengthy scripts, just asking if it can
> > be done in say, two, three lines.
> >
> > I'm aware of input /p, but I need it without interaction.
> >
> > Thorsten
>