China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-15 00:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Idea Challenge][Discussion] Batch processing for Base64 encoding operations theoretical prototype DigestI View 14,807 Replies 30
Floor 16 Posted 2006-11-18 02:29 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
It's not encryption, but encoding/decoding. The method to transmit binary files with plain text—Base64

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 17 Posted 2006-11-23 04:51
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
There are really many talented people, keep it up!
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 18 Posted 2006-11-23 05:20 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Brother electronixtar's first batch processing about base64 is basically understood, but the second one can't be understood at all, which seriously hit the self-confidence, sweat one first.
Floor 19 Posted 2006-11-23 06:06 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
Bitwise operations reversed are indeed complicated.

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 20 Posted 2006-11-25 03:05 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Wonderful and great post, appreciate it~ :)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 21 Posted 2006-12-20 23:04 ·  中国 山东 聊城 电信
新手上路
Credits 4
Posts 2
Joined 2006-12-20 22:34
19-year member
UID 74072
Gender Male
Status Offline
Must take a good look at the pick-up lines...
Haha!!
Floor 22 Posted 2006-12-23 07:49 ·  中国 湖北 宜昌 电信
初级用户
Credits 97
Posts 32
Joined 2005-12-03 19:43
20-year member
UID 46432
Gender Male
Status Offline
At this time there should not be ]==[0].
Floor 23 Posted 2007-01-11 01:01 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
Post an example of encoding and decoding, which has great limitations. Because I don't know how to convert the decoded numbers back to the original characters. This example simply determines a few character numbers by looking up a table, and Chinese characters are not possible. Also, I don't know how to handle special characters, so there will be many errors when there are special characters (of course, when there are special characters).
If using echo when generating a file, it can reduce some errors when inputting special characters, but it still has to judge the two numbers 0D0A (carriage return and line feed), whatever.
::code by 0401

@echo off
setlocal
set ASCII= !"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~
setlocal enabledelayedexpansion
set B64CODE=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
set str=
set/p "str=Please enter the string to be encoded: "
if not defined str exit/b

call :enc
call :dec
exit/b

:enc
rem The two file names for comparison are:
rem Put the string in a file for comparison (fc) and find the length of the string
set/p=%str%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo The length of the original string is: %strlen% bytes

rem Generate the file for fc to compare with the string
for /l %%l in (0,1,%strlen%) do >>.cmp set/p= <nul

rem Store the 16进制 of the compared string into a variable
for /f "tokens=3" %%i in ('fc/b .cmp .str^|find "A1"') do (set strhex=!strhex!%%i)
del .cmp .str
echo The converted hexadecimal is: %strhex%

:encloop
if not defined strhex goto :encbreak
set chr1=0x!strhex:~0,2!
set chr2=0x!strhex:~2,2!
set chr3=0x!strhex:~4,2!
set strhex=%strhex:~6%
for %%i in (chr1 chr2 chr3) do if "!%%i!"=="0x" set %%i=0
set/a enc1=%chr1%">>"2
set/a enc2=((%chr1%"&"3)"<<"4)"|"(%chr2%">>"4)
set/a enc3=((%chr2%"&"15)"<<"2)"|"(%chr3%">>"6)
set/a enc4=%chr3%"&"63
::echo The shifted data: %enc1% %enc2% %enc3% %enc4%
if %chr3% equ 0 if %chr2% equ 0 (set enc4=64& set enc3=64) else (set enc4=64)
::echo The shifted data is encoded as:!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
set encstr=!encstr!!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
goto :encloop
:encbreak
echo The string encoded data is
goto :eof

:dec
rem And find the length of the encoded string
set/p=%encstr%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo The length of the encoded string is: %strlen% bytes
del .str

rem Find the number of offsets during encoding from the encoded string encstr
set str=
for /l %%l in (0,1,%strlen%) do (
for /l %%m in (0,1,64) do (
if "!encstr:~%%l,1!"=="!B64CODE:~%%m,1!" (
set str=!str!/%%m
)
)
)
set a=1&set b=4

:decloop
if %strlen% equ 0 goto :decbreak
for /f "tokens=%a%-%b% delims=/" %%i in ("%str%") do set enc1=%%i&set enc2=%%j&set enc3=%%k&set enc4=%%l
::echo The offsets of the 4 bytes are respectively: %enc1% %enc2% %enc3% %enc4%
set/a a+=4&set/a b+=4
set/a strlen-=4
set/a chr1=((%enc1%"<<"2)"|"(%enc2%">>"4))-32
set/a chr2=(((%enc2%"&"15)"<<"4)"|"(%enc3%">>"2))-32
set/a chr3=(((%enc3%"&"3)"<<"6)"|"%enc4%)-32
::echo The decimal after decoding the 4 bytes and subtracting 32 is: %chr1% %chr2% %chr3%
if %enc4% equ 64 if %enc3% equ 64 (set chr3=107&set chr2=107) else (set chr3=107)
::echo The decoded data is:!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
set decstr=!decstr!!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
goto :decloop

:decbreak
echo The encoded data decoded is
goto :eof
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
redtek +15 2007-01-26 10:44
electronixtar +11 2007-01-29 04:54
Floor 24 Posted 2007-01-11 03:05 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Brother 0401 is very powerful, admired
Floor 25 Posted 2007-01-11 07:09 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Brother 0401, impressive
Floor 26 Posted 2007-01-25 07:05 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Learned.
I tried to modify the code of 0401, and used DEBUG to write the obtained hexadecimal results into a file.
Then the original characters can be obtained.

I also included another batch file for decimal to hexadecimal conversion for 0401.
There is a length limit for this decoding. I am not familiar with DEBUG.
This part of DEBUG is copied from redtek's batch file for QQ password to PwdHash.

::code by 0401

@echo off
setlocal
setlocal enabledelayedexpansion
set B64CODE=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
set str=
set/p "str=Please enter the string to be encoded: "
if not defined str exit/b

call :enc
call :dec
exit/b

:enc
rem The two comparison file names are:
rem Put the string in the file for comparison (fc) and find the length of the string
set/p=%str%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo The original string length is: %strlen% bytes
set str_len=%strlen%

rem Generate the file for fc to compare with the string
for /l %%l in (0,1,%strlen%) do >>.cmp set/p= <nul

rem Store the hexadecimal of the compared string into a variable
for /f "tokens=3" %%i in ('fc/b .cmp .str^|find "A1"') do (set strhex=!strhex!%%i)
del .cmp .str
echo The converted hexadecimal is: %strhex%

:encloop
if not defined strhex goto :encbreak
set chr1=0x!strhex:~0,2!
set chr2=0x!strhex:~2,2!
set chr3=0x!strhex:~4,2!
set strhex=%strhex:~6%
for %%i in (chr1 chr2 chr3) do if "!%%i!"=="0x" set %%i=0
set/a enc1=%chr1%">>"2
set/a enc2=((%chr1%"&"3)"<<"4)"|"(%chr2%">>"4)
set/a enc3=((%chr2%"&"15)"<<"2)"|"(%chr3%">>"6)
set/a enc4=%chr3%"&"63
::echo The data after shifting is: %enc1% %enc2% %enc3% %enc4%
if %chr3% equ 0 if %chr2% equ 0 (set enc4=64& set enc3=64) else (set enc4=64)
::echo The encoded data after shifting is:!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
set encstr=!encstr!!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
goto :encloop
:encbreak
echo The encoded data of the string is
goto :eof

:dec
rem And find the length of the encoded string
set/p=%encstr%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo The length of the encoded string is: %strlen% bytes
del .str

rem Find the number of offsets during encoding from the encoded string encstr
set str=
for /l %%l in (0,1,%strlen%) do (
for /l %%m in (0,1,64) do (
if "!encstr:~%%l,1!"=="!B64CODE:~%%m,1!" (
set str=!str!/%%m
)
)
)
set a=1&set b=4

:decloop
if %strlen% equ 0 goto :decbreak
for /f "tokens=%a%-%b% delims=/" %%i in ("%str%") do set enc1=%%i&set enc2=%%j&set enc3=%%k&set enc4=%%l
::echo The offsets of the 4 bytes are respectively: %enc1% %enc2% %enc3% %enc4%
set/a a+=4&set/a b+=4
set/a strlen-=4
set/a chr1=((%enc1%"<<"2)"|"(%enc2%">>"4))
set/a chr2=(((%enc2%"&"15)"<<"4)"|"(%enc3%">>"2))
set/a chr3=(((%enc3%"&"3)"<<"6)"|"%enc4%)
::echo The decimal obtained by decoding the 4 bytes and then subtracting 32 is: %chr1% %chr2% %chr3%
call :_d2h chr1 %chr1%
call :_d2h chr2 %chr2%
call :_d2h chr3 %chr3%
::echo The decimal obtained by decoding the 4 bytes and then subtracting 32 is: %chr1% %chr2% %chr3%
::if %enc4% equ 64 if %enc3% equ 64 (set chr3=107&set chr2=107) else (set chr3=107)
::echo The decoded data is:!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
set decstr=!decstr! %chr1% %chr2% %chr3%
goto :decloop

:decbreak
pause
set /a str_len+=1
echo exit|%ComSpec%/kprompt e 100 $_%decstr%$_rcx$_%str_len%$_n tmp.tmp$_w$_q$_|debug>nul
chcp 936>nul
set/p=The decoded data of the encoded data is:<nul
type tmp.tmp
del tmp.tmp>nul
echo.
pause
goto :eof

:_d2h
set hex=
set hexstr=0 1 2 3 4 5 6 7 8 9 A B C D E F
set d=0
for %%i in (%hexstr%) do (set d!d!=%%i&set/a d+=1)
set scanf=%2
set tscanf=
call :d2h
if not defined hex set hex=0
set %1=%hex%
goto :eof

:d2h
if %scanf% equ 0 goto :eof
set/a tscanf=%scanf%"&"15
set/a scanf">>="4
set hex=!d%tscanf%!!hex!
goto :d2h
QQ:366840202
http://chenall.net
Floor 27 Posted 2007-01-25 09:13 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
Hehe, nice. The discussion has made progress again. But I'm also unfamiliar with Debug, it seems I need to catch up.
Floor 28 Posted 2007-01-25 23:56 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Post another relatively perfect one.
Next, modify this a bit more and it can be made into a tool for encoding/decoding specified files with BASE64.

The code can probably be further streamlined.



  1. ::code by 0401
  2. ::chenall modified on 2007.01.25
  3. @echo off
  4. setlocal
  5. setlocal enabledelayedexpansion
  6. set B64CODE=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
  7. set str=
  8. set/p "str=Please enter the string to be encoded: "
  9. if not defined str exit/b

  10. call :enc
  11. call :dec
  12. exit/b

  13. :enc
  14. rem The two file names for comparison are:
  15. rem Put the string in a file for comparison (fc) and find the length of the string
  16. set/p=%str%<nul>.str
  17. for %%f in (.str) do set strlen=%%~zf
  18. echo The length of the original string is: %strlen% bytes
  19. set str_len=%strlen%

  20. rem Generate the file for fc to compare with the string
  21. del _*.cmp 2>nul
  22. for /l %%l in (1,1,%strlen%) do (
  23. >>_a.cmp set/p=a<nul
  24. >>_b.cmp set/p=b<nul
  25. )
  26. fc/b _a.cmp .str|find ":">tmp.str
  27. fc/b _b.cmp .str|find ":">>tmp.str
  28. sort tmp.str>_a.cmp

  29. rem Store the 16 - bit hexadecimal of the compared string into a variable
  30. for /f "tokens=1,3" %%i in (_a.cmp) do (
  31. if not "!n!"=="%%i" set strhex=!strhex!%%j
  32. set n=%%i
  33. )
  34. ::for /f "tokens=3" %%i in ('fc/b .cmp .str^|find ":"') do (set strhex=!strhex!%%i)
  35. del *.cmp *.str 2>nul
  36. echo The converted hexadecimal is: %strhex%

  37. :encloop
  38. if not defined strhex goto :encbreak
  39. set chr1=0x!strhex:~0,2!
  40. set chr2=0x!strhex:~2,2!
  41. set chr3=0x!strhex:~4,2!
  42. set strhex=%strhex:~6%
  43. for %%i in (chr1 chr2 chr3) do if "!%%i!"=="0x" set %%i=0
  44. set/a enc1=%chr1%">>"2
  45. set/a enc2=((%chr1%"&"3)"<<"4)"|"(%chr2%">>"4)
  46. set/a enc3=((%chr2%"&"15)"<<"2)"|"(%chr3%">>"6)
  47. set/a enc4=%chr3%"&"63
  48. ::echo The data after shifting is: %enc1% %enc2% %enc3% %enc4%
  49. if %chr3% equ 0 if %chr2% equ 0 (set enc4=64& set enc3=64) else (set enc4=64)
  50. ::echo The encoded data after shifting is:!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
  51. set encstr=!encstr!!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
  52. ::for %%i in (enc1 enc2 enc3 enc4) do (
  53. :: if !%%i! lss 10 (set/p=0!%%i!<nul>>tmp.bb) else (set/p=!%%i!<nul>>tmp.bb)
  54. ::)
  55. goto :encloop
  56. :encbreak
  57. echo The encoded data of the string [%str%] is [%encstr%]
  58. ::echo 字符串编码后的数据为 >>tmp.aa
  59. goto :eof

  60. :dec
  61. rem And find the length of the encoded string
  62. set/p=%encstr%<nul>.str
  63. for %%f in (.str) do set strlen=%%~zf
  64. echo The length of the encoded string is: %strlen% bytes
  65. del .str

  66. rem Find the number of offsets during encoding from the encoded string encstr
  67. set str=
  68. for /l %%l in (0,1,%strlen%) do (
  69. for /l %%m in (0,1,64) do (
  70. if "!encstr:~%%l,1!"=="!B64CODE:~%%m,1!" (
  71. if %%m lss 10 (
  72. set str=!str!0%%m
  73. ) else (
  74. set str=!str!%%m
  75. )
  76. )
  77. )
  78. )
  79. set a=1&set b=4&set e=0
  80. del debug.src 2>nul

  81. :decloop
  82. if not defined str goto :decbreak
  83. set /a enc1=1!str:~0,2!-100
  84. set /a enc2=1!str:~2,2!-100
  85. set /a enc3=1!str:~4,2!-100
  86. set /a enc4=1!str:~6,2!-100
  87. set str=%str:~8%
  88. for %%i in (enc1 enc2 enc3 enc3 enc4) do (
  89. if !%%i! leq 0 set %%i=0
  90. if !%%i! equ 64 set %%i=0
  91. )
  92. ::echo The offsets of the 4 bytes are respectively: %enc1% %enc2% %enc3% %enc4%
  93. set/a chr1=((%enc1%"<<"2)"|"(%enc2%">>"4))
  94. set/a chr2=(((%enc2%"&"15)"<<"4)"|"(%enc3%">>"2))
  95. set/a chr3=(((%enc3%"&"3)"<<"6)"|"%enc4%)
  96. ::echo The decimal obtained by subtracting 32 from the 4 bytes after decoding is: %chr1% %chr2% %chr3%
  97. call :_d2h chr1 %chr1%
  98. call :_d2h chr2 %chr2%
  99. call :_d2h chr3 %chr3%
  100. ::echo The hexadecimal obtained by subtracting 32 from the 4 bytes after decoding is: %chr1% %chr2% %chr3%
  101. ::echo The decoded data is:!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
  102. set /a ee=0x100+3*e
  103. call :_d2h ee %ee%
  104. >>debug.src echo e %ee% %chr1% %chr2% %chr3%
  105. ::set/p=%chr1%%chr2%%chr3%<NUL >>BB.TXT
  106. set /a e+=1
  107. goto :decloop

  108. :decbreak
  109. pause
  110. set /a str_len+=1
  111. >>debug.src echo rcx
  112. >>debug.src echo %str_len%
  113. >>debug.src echo n tmp.tmp
  114. >>debug.src echo w
  115. >>debug.src echo q
  116. debug<debug.src
  117. del debug.src
  118. chcp 936>nul
  119. echo The encoded data [%encstr%]
  120. echo.
  121. set/p=Decoded as:<nul
  122. type tmp.tmp
  123. del tmp.tmp>nul
  124. echo.
  125. pause
  126. goto :eof

  127. :_d2h
  128. set hex=
  129. set hexstr=0 1 2 3 4 5 6 7 8 9 A B C D E F
  130. set d=0
  131. for %%i in (%hexstr%) do (set d!d!=%%i&set/a d+=1)
  132. set scanf=%2
  133. set tscanf=
  134. call :d2h
  135. if not defined hex set hex=0
  136. set %1=%hex%
  137. goto :eof

  138. :d2h
  139. if %scanf% equ 0 goto :eof
  140. set/a tscanf=%scanf%"&"15
  141. set/a scanf">>="4
  142. set hex=!d%tscanf%!!hex!
  143. goto :d2h
chenall posted on: 2007-01-25 15:35


[ Last edited by chenall on 2007-1-26 at 04:37 AM ]
QQ:366840202
http://chenall.net
Floor 29 Posted 2007-01-26 03:03 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
To brother chenall:

Indeed, a BASE64 encoding/decoding tool can already be written. I also have this plan, but I don't know if there will be time available. Hehe.

By the way, brother, in order to use fc to compare files, you used the fsutil file createnew command to create an equally sized file. But in this way, fsutil can only be used by members of the administrator group, which reduces the applicability to some extent. Why not try the method in floor 5, or use the f sub-command of debug to fill an equally sized file with all binary 0s.

-------------------------------------------------
Supplementary: If you use the f sub-command to fill, you also need to fill two files with different contents, because if you are dealing with a single file, it is not guaranteed that you will not encounter bytes with binary 0.

[ Last edited by 0401 on 2007-1-28 at 01:09 PM ]
Floor 30 Posted 2007-01-26 04:38 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
The code has been modified by referring to the code on floor 5.
QQ:366840202
http://chenall.net
Forum Jump: