中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-29 08:05
47,816 topics / 349,916 posts / today 0 new / 48,265 members
DOS批处理 & 脚本技术(批处理室) » [Idea Challenge][Discussion] Batch processing for Base64 encoding operations theoretical prototype
Printable Version  15,386 / 30
Floor1 electronixtar Posted 2006-11-13 13:07
铂金会员 Posts 2,672 Credits 7,493

::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 ]
Floor2 electronixtar Posted 2006-11-13 13:23
铂金会员 Posts 2,672 Credits 7,493
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 ]
Floor3 pengfei Posted 2006-11-15 00:23
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
The idea of electronixtar is very good. I believe that after understanding this concept and mastering it, it will have great uses.
Floor4 electronixtar Posted 2006-11-15 01:12
铂金会员 Posts 2,672 Credits 7,493
So no one is interested originally~~~Wala wala wala sad啦
Floor5 electronixtar Posted 2006-11-15 01:20
铂金会员 Posts 2,672 Credits 7,493
Post another world-class expert Herbert Kleebauer's written base64.cmd
Floor6 pengfei Posted 2006-11-15 01:21
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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~
Floor7 electronixtar Posted 2006-11-15 01:27
铂金会员 Posts 2,672 Credits 7,493
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~
Floor8 pengfei Posted 2006-11-15 01:35
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Hehe~ Brother electronixtar, you're too kind. I really don't know how.
Floor9 ccwan Posted 2006-11-15 01:35
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Brother electronixtar, there is a space after each line, which made me think there was a problem with the code.
Floor10 lxmxn Posted 2006-11-15 01:46
版主 Posts 4,938 Credits 11,386

  Who says no one is interested~~

  This post is great, worthy of careful study~~~First, give it a thumbs up~
Floor11 electronixtar Posted 2006-11-15 04:49
铂金会员 Posts 2,672 Credits 7,493
re ccwan:

I just copied and pasted it~ ~ Sweat~ ~
Floor12 electronixtar Posted 2006-11-18 01:29
铂金会员 Posts 2,672 Credits 7,493
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.

Floor13 ccwan Posted 2006-11-18 01:43
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Good stuff. Pick-up lines.
Floor14 redtek Posted 2006-11-18 02:06
金牌会员 Posts 1,147 Credits 2,902
Appreciating it~~
Floor15 vkill Posted 2006-11-18 02:25
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
I still haven't seen exactly how to "encrypt"
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023