批处理之家's Archiver

youxi01 发表于 2008-1-18 18:43

批处理做Base64编码、解码运算理论原型

转载自dos联盟论坛.[code]
::base64conv.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal enabledelayedexpansion
set code=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
set /A B1=10,B2=0x7f,B3=255
echo %B1% %B2% %B3%
:: byte-triplet to base64-quadruple 第一次echo要encode的几个ASCII字符
set /A Q1=(%B1% "&" 252) ">>" 2
set /A Q2=((%B1% "&" 3) "<<" 4) + ((%B2% "&" 240) ">>" 4)
set /A Q3=((%B2% "&" 15) "<<" 2) + ((%B3% "&" 192) ">>" 6)
set /A Q4=(%B3% "&" 63)
::
echo %Q1% %Q2% %Q3% %Q4% 第二次echo已经encode完毕
echo !code:~%Q1%,1! !code:~%Q2%,1! !code:~%Q3%,1! !code:~%Q4%,1!
:: base64-quadruple to  byte-triplet 第三次echo Base64字符流出来了!
:: yes, I know, to work a loop to find the pos in %code% should be here.
set /A B1=(%Q1% "<<" 2) + ((%Q2% "&" 48) ">>" 4)
set /A B2=((%Q2% "&" 15) "<<" 4) + ((%Q3% "&" 60) ">>" 2)
set /A B3=((%Q3% "&" 3) "<<" 6) + %Q4%
::
echo %B1% %B2% %B3% 第四次echo decode到原来的字符
::base64conv.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pause>nul
[/code]

youxi01 发表于 2008-1-18 18:45

世界顶尖级牛人:Herbert Kleebauer 写的 base64.cmd

[i=s] 本帖最后由 lxzzr 于 2012-3-4 22:38 编辑 [/i]

[code]
@echo off
setlocal enabledelayedexpansion


set infile=a.txt
:: set infile=%1

for %%i in (%infile%) do set outfile=%%~ni.b64

echo --->%outfile%
echo Content-Type: application/octet-stream; name="%infile%">>%outfile%
echo Content-Transfer-Encoding: base64>>%outfile%
echo Content-Disposition: inline; filename="%infile%">>%outfile%
echo.>>%outfile%

set t=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

for %%i in (%infile%) do set n=%%~zi
if [%n%]==[0] goto :eof
set /p=<nul>_.a
set /p=<nul>_.b
set /p=a<nul>_.a1
set /p=b<nul>_.b1
set m=1
set /a i=-2

:loop
set /a k="%m%&%n%"
if not [%k%]==[0] copy /b _.a + _.a1>nul
if not [%k%]==[0] copy /b _.b + _.b1>nul
set /a k="%i%&%n%"
if [%k%]==[0] goto :exit

type _.a1>>_.a1
type _.b1>>_.b1
set /a m=m*2
set /a i=i*2
goto :loop
:exit

fc /b %infile% _.a|find ":">_.a1
fc /b %infile% _.b|find ":">>_.a1
sort _.a1>_.b1
set n=
set m=0
set k=0
set q=0
for /f "tokens=1,2" %%i in (_.b1) do (
   if not [!n!]==[%%i] (
        set i=%%j
        call :dig !i:~0,1!
        set /a m=!m!*16+!j!
        call :dig !i:~1,1!
        set /a m=m*16+j
        set /a k=k+1
        if !k!==3 (
           set /a p="m>>18"
           call set x=!x!%%t:~!p!,1%%
           set /a p="(m>>12)&63"
           call set x=!x!%%t:~!p!,1%%
           set /a p="(m>>6)&63"
           call set x=!x!%%t:~!p!,1%%
           set /a p="m&63"
           call set x=!x!%%t:~!p!,1%%
           set /a q=q+1
           if [!q!]==[18] (
                           echo !x!>>%outfile%
                           set x=
                           set q=0)
           set m=0
           set k=0))
   set n=%%i)

if [%k%]==[1] (
           set /a p="m>>2"
           call set x=!x!%%t:~!p!,1%%
           set /a p="(m<<4)&63"
           call set x=!x!%%t:~!p!,1%%==)

if [%k%]==[2] (
           set /a p="m>>10"
           call set x=!x!%%t:~!p!,1%%
           set /a p="(m>>4)&63"
           call set x=!x!%%t:~!p!,1%%
           set /a p="(m<<2)&63"
           call set x=!x!%%t:~!p!,1%%=)

if not [%q%]==[0] echo %x%>>%outfile%

echo.>>%outfile%
echo ----->>%outfile%

del _.a
del _.a1
del _.b
del _.b1

goto :eof

:dig
set j=%1
if [%1]==[A] set j=10
if [%1]==[B] set j=11
if [%1]==[C] set j=12
if [%1]==[D] set j=13
if [%1]==[E] set j=14
if [%1]==[F] set j=15
[/code]

BAT1 发表于 2008-11-22 17:18

看不懂啊。
能附上说明或做成教材吗?

feng 发表于 2009-6-19 13:59

是呀,如果带上说明最好了

plp626 发表于 2012-3-2 14:51

[i=s] 本帖最后由 plp626 于 2012-3-2 16:22 编辑 [/i]

入口参数4个,每3个ascii值为一组,不足的用-1代替;第四个参数为返回值变量名[code]:ascii2base64 <int> <int> <int> <ret> //code by plp;
setlocal enabledelayedexpansion&Set/a a1=%1,a2=%2,a3=%3
set/a "b1=a1/4,b2=a1%%4<<4|a2/16,b3=(a2%%16<<2|a3/64)-3*(a2>>31),b4=a3%%64"
Set s=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
Set $=&Set $=!s:~%b1%,1!!s:~%b2%,1!!s:~%b3%,1!!s:~%b4%,1!
endlocal&Set %4=%$%&goto:eof[/code]

plp626 发表于 2012-3-2 16:08

[i=s] 本帖最后由 plp626 于 2012-3-2 23:17 编辑 [/i]

ascii 转 base64编码[code]@ECHO off
:: base64_d.cmd version 1.0
:: ascii文件(字符串) 转 base64编码存储
:: 用法:第一个参数为文件名,或字符串,建议第一个参数用双引号引起来;
::       若当期路径没有%1文件名, 则%1被当做字符串处理;
:: 程序依赖ascii转hex的外部程序hex.cmd(version 1.1即可),下载地址:
:: http://www.bathome.net/thread-12688-1-1.html

setlocal enabledelayedexpansion
set "str="
for /f "tokens=1-3" %%a in ('hex.cmd "%~1" 3')do (
        if %%b%%c.==. (
                call:ascii2base64 0x%%a -1 -1 ans
        )else if %%c.==. (
                call:ascii2base64 0x%%a 0x%%b -1 ans
        )else (
                call:ascii2base64 0x%%a 0x%%b 0x%%c ans
        )
        set/p=!ans!<nul
)
goto:eof

:ascii2base64 <int> <int> <int> <ret> //code by plp;
setlocal&Set/a a1=%1,a2=%2,a3=%3
set/a "b1=a1/4,b2=a1%%4<<4|a2/16,b3=(a2%%16<<2|a3/64)-3*(a2>>31),b4=a3%%64"
Set s=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
Set $=&Set $=!s:~%b1%,1!!s:~%b2%,1!!s:~%b3%,1!!s:~%b4%,1!
endlocal&Set %4=%$%&goto:eof[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.