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 01:37
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Idea Challenge][Discussion] Batch processing for Base64 encoding operations theoretical prototype DigestI View 14,808 Replies 30
Original Poster Posted 2006-11-13 13:07 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline

::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 First echo the ASCII characters to be encoded
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% Second echo completed encoding
echo !code:~%Q1%,1! !code:~%Q2%,1! !code:~%Q3%,1! !code:~%Q4%,1!
:: Base64-quadruple to byte-triplet Third echo The Base64 characters are out!
:: 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% Fourth echo decoded to the original character
::base64conv.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::



It can be seen that the bit operations of set /a are powerful! I don't understand the above code at all. For details, see

http://groups.google.com/group/alt.msdos.batch.nt/browse_thread/thread/cfe3dbf991d68187/b03cb66dd08c2719?lnk=gst&q=base64&rnum=1#b03cb66dd08c2719

Moderators, experts, and lurkers here, come out and think about how to implement BASE64 encoding/decoding with pure batch processing?

The theoretical research on the Base64 algorithm is very useful. A simple example is: batch processing encryption, binary files embedded in bat, processing EMail, etc.

[ Last edited by electronixtar on 2006-11-15 at 01:25 AM ]

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 2 Posted 2006-11-13 13:23 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
I first put forward an idea, using the fc command to obtain the ASCII code of each byte of the file stream. (In fact, many uses of fc are ignored by everyone). But what is used to restore the ASCII code to characters? Including some garbled codes and invisible characters that batch processing cannot handle (those after ASCII code 128)?

btw, newcomers, theoretical research is boring, but very useful, please support and understand more! In fact, the batch processing technology of some aspects of our alliance is already at the world - top level.

[ Last edited by electronixtar on 2006-11-13 at 01:27 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +1 2006-11-14 23:57

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 3 Posted 2006-11-15 00:23 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
The idea of electronixtar is very good. I believe that after understanding this concept and mastering it, it will have great uses.
Floor 4 Posted 2006-11-15 01:12 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
So no one is interested originally~~~Wala wala wala sad啦

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 5 Posted 2006-11-15 01:20 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Post another world-class expert Herbert Kleebauer's written base64.cmd

@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 == 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 == copy /b _.a + _.a1>nul
if not == copy /b _.b + _.b1>nul
set /a k="%i%&%n%"
if == 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 == (
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 == (
echo !x!>>%outfile%
set x=
set q=0)
set m=0
set k=0))
set n=%%i)


if == (
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 == (
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 == echo %x%>>%outfile%


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


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


goto :eof


:dig
set j=%1
if == set j=10
if == set j=11
if == set j=12
if == set j=13
if == set j=14
if == set j=15

Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
redtek +5 2006-11-15 02:49
0401 +2 2007-01-11 00:43

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 6 Posted 2006-11-15 01:21 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
It's not that I'm not interested, it's just that I'm a rookie with insufficient skills. Just listening to your lecture, I can gain more insights, heh heh~
业精于勤而荒于嬉,形成于思而毁于随。
Floor 7 Posted 2006-11-15 01:27 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Pengfei, you're already very skilled but still call yourself a newbie~~~ That's too modest. At least you can be considered an old hand~

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 8 Posted 2006-11-15 01:35 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Hehe~ Brother electronixtar, you're too kind. I really don't know how.
业精于勤而荒于嬉,形成于思而毁于随。
Floor 9 Posted 2006-11-15 01:35 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Brother electronixtar, there is a space after each line, which made me think there was a problem with the code.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 10 Posted 2006-11-15 01:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Who says no one is interested~~

  This post is great, worthy of careful study~~~First, give it a thumbs up~
Floor 11 Posted 2006-11-15 04:49 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
re ccwan:

I just copied and pasted it~ ~ Sweat~ ~

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 12 Posted 2006-11-18 01:29 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
An Introduction to Base64

Author: lcother@163.net

Wonderful Base64 Encoding
Dear readers, you should all be experienced netizens. I dare to ask you here, besides chatting up girls, posting on forums, and throwing bricks... what activity do you do the most? Right, you must say: it's sending and receiving emails! (Who dares to say they haven't sent/received an email? Shoot them!)
There is a security issue when sending/receiving emails - imagine that the love letter you spent all day writing to the school beauty was intercepted by that kid Zhang San from the next dormitory (Could he be a hacker??) Worse still, he's your rival in love... Oh, the consequences are unimaginable! Therefore, we must have a relatively reliable encryption method that can convert the plaintext of the email, at least to get something that can't be seen through at a glance, and the encoding/decoding speed should be fast enough. (At this time, you can imagine again that Zhang San intercepted your mushy love letter, but he saw: "Hey? Why is it all jumbled? Junk mail!!" - Then you can escape the disaster!)

Base64 is an encryption method generated under this background. Its characteristics are: 1. Very fast speed. 2. Can convert string A into string B, and if you just look at string B, you can absolutely not guess the content of string A. Don't believe it? Let's take a look at the following string:

xOO6w6Osu7bTrbniwdnAz8LetcTnzbfXzOy12KOh

Hehe, what is it? Did you guess it? Actually, it's the thing generated by Base64 encoding the following text:

Hello, welcome to Lao Luo's colorful world!

The introduction is over, let's start to explore the substantive things.

Base64 is one of the most common encoding methods used to transmit 8-bit byte codes on the network. You can check RFC2045 ~ RFC2049, where there are detailed specifications of MIME.

Base64 requires converting every three 8-bit bytes into four 6-bit bytes (3*8 = 4*6 = 24), and then adding two high bits 0 to the 6 bits to form four 8-bit bytes, that is to say, the converted string will theoretically be 1/3 longer than the original.

Will this be too abstract? Don't worry, let's look at an example:

Before conversion aaaaaabb ccccdddd eeffffff
After conversion 00aaaaaa 00bbcccc 00ddddee 00ffffff

It should be very clear, right? The above three bytes are the original text, and the following four bytes are the converted Base64 encoding, and their first two bits are both 0.

After conversion, we use a code table to get the string we want (that is, the final Base64 encoding). This table is as follows: (excerpted from RFC2045)


Table 1: The Base64 Alphabet

value Encoding value Encoding value Encoding value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 60 8
9 J 26 a 43 r 61 9
10 K 27 b 44 s 62 +
11 L 28 c 45 t 63 /
12 M 29 d 46 u (pad) =
13 N 30 e 47 v
14 O 31 f 48 w
15 P 32 g 49 x
16 Q 33 h 50 y


Let's look at a practical example to deepen the impression!

Before conversion 10101101 10111010 01110110
After conversion 00101011 00011011 00101001 00110110
Decimal 43 27 42 54
Corresponding values in the code table r b q 2


So the above 24-bit encoding, the encoded Base64 value is rbq2
Decoding is the same, connect the binary bits of rbq2 and then reorganize to get three 8-bit values, and get the original code.
(Decoding is just the reverse process of encoding, so I won't say more here. In addition, there are many RFCs about MIME. If you need detailed information, please find it yourself.)

In a more programming-like way of thinking, the encoding process is as follows:

The first character gets the position of the first target character in the Base64 table by shifting right by 2 bits, and takes the corresponding character on the table according to this value, which is the first target character.
Then shift the first character left by 6 bits and add the second character shifted right by 4 bits to get the second target character.
Then shift the second character left by 4 bits and add the third character shifted right by 6 bits to get the third target character.
Finally, take the right 6 bits of the third character to get the fourth target character.

So easy! That’s all!!!

But wait... The clever you may ask, the number of bytes of the original text should be a multiple of 3. What if this condition is not met?

Our solution is like this: the place where the bytes of the original text are not enough can be filled with all 0s, and the Base64 encoding uses = signs instead. This is why some Base64 encodings end with one or two equal signs, but there are at most two equal signs. Because:

Remainder = original text byte number MOD 3

So the remainder can only be one of 0, 1, 2 in any case. If the remainder is 0, it means that the number of original text bytes is exactly a multiple of 3 (the most ideal situation). If it is 1, in order to make the Base64 encoding a multiple of 4, 2 equal signs need to be added; similarly, if it is 2, 1 equal sign needs to be added.

By this time, everyone should understand it all. If there is still any ambiguity, go back and read it carefully. Actually, it's not difficult to understand.

Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-11-23 06:22

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 13 Posted 2006-11-18 01:43 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Good stuff. Pick-up lines.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 14 Posted 2006-11-18 02:06 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Appreciating it~~
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 15 Posted 2006-11-18 02:25 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I still haven't seen exactly how to "encrypt"
Forum Jump: