Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of block chain Base64 coding?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the principle of block chain Base64 coding". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the principle of blockchain Base64 coding"!

The Origin of 1Base64

At present, Base64 has become one of the common coding methods for transmitting 8Bit byte codes on the network. When making a payment system, the message interaction between the systems needs to use Base64 to transcode the plaintext, and then sign or encrypt it, and then transmit it (or Base64 again). So what exactly is the role of Base64?

A situation often encountered in the process of parameter transmission: it is no problem to use all English, but there will be garbled code when it comes to Chinese. Similarly, not all characters transmitted on the network are printable characters, such as binaries, pictures, and so on. The emergence of Base64 is to solve this problem, it is based on 64 printable characters to represent binary data.

When e-mail came out, it could only be transmitted in English, but later, with the increase in the number of users, users of Chinese, Japanese and other languages were also in demand, but these characters could not be effectively processed by servers or gateways, so Base64 appeared. Subsequently, Base64 is also used in URL, Cookie, and web page transfer of a small number of binary files.

The coding principle of 2Base64

The principle of Base64 is relatively simple. Whenever we use Base64, we define an array like this first:

['A','B','C',... 'averse,' baked, 'crested,.' 0percent, '1percent,.' +','/']

Above is the index table of Base64, with 64 printable characters of "A murz, a murz, 0-9, +, /", which is stipulated in the standard Base64 protocol. In daily use, we will also see the "=" or "=" sign appear in the encoding results of Base64, where "=" appears as a padding character, which will be discussed later.

Specific conversion steps

The first step is to divide the string to be converted into a group of three bytes, each byte accounting for 8bit, so there are 24 binary bits.

In the second step, the above 24 binary bits are divided into 4 groups.

The third step is to add two zeros in front of each group, each group from 6 to 8 binary bits, a total of 32 binary bits, or four bytes.

The fourth step is to obtain the corresponding value according to the Base64 coding comparison table (see figure below).

0 A 17 R 34 i 51 z1 B 18 S 35 j 52 02 C 19 T 36 k 53 13 D 20 U 37 l 54 24 E 21 V 38 m 55 35 F 22 W 39 n 56 46 G 23 X 40 o 57 57 H 24 Y 41 p 58 68 I 25 Z 42 Q 59 79 J 26 a 43 r 60 810 K 27 b 44 s 61 911 L 28 c 45 t 62 + 12 M 29 d 46 U 63 / 13 N 30 e 47 v14 O 31 f 48 w 15 P 32 g 49 x16 Q 33 h 50 y

From the above steps, we find that:

The characters in the Base64 character table could originally be represented by 6 bit, but now adding 2 zeros to 8 bit will cause some waste. Therefore, the Base64-encoded text is about 1/3 more than the original text.

Why use a set of 3 bytes? Because the minimum common multiple of 6 and 8 is 24, three bytes are exactly 24 binary bits, with a set of 6 bit bits, which happens to be divided into four groups.

Example illustration

The table in the following figure is an example, and let's analyze the whole process in detail.

The first step: the corresponding ASCII values of "M", "a" and "n" are 77, 97 and 110, respectively, and the corresponding binary values are 01001101, 01100001 and 01101110, respectively. As shown in the second and third lines of the figure, a 24-bit binary string is formed.

Step 2: as shown in the red box, divide the 24 bits into four groups for every 6 bits.

Step 3: add two zeros in front of each of the above groups to expand to 32 binary bits, which becomes four bytes: 00010011, 00010110, 00000101, 00101110. The corresponding values (Base64 coding index) are: 19, 22, 5, 46.

Step 4: search the Base64 coding table with the above values, corresponding to T, W, F, u, respectively. So "Man" Base64 becomes: TWFu after encoding.

Insufficient number of digits

The above is illustrated in terms of three bytes. If the number of bytes is less than three, what should I do?

Two bytes: two bytes with a total of 16 binary bits, still grouped according to the rules. At this time, there are a total of 16 binary bits, each in a group of 6, then the third group lacks 2 bits, which is filled with 0 to get three Base64 codes, and the fourth group uses "=" to make up if there is no data at all. Therefore, in the figure above, "BC" is converted to "QKM=".

One byte: a byte consists of eight binary bits, still grouped according to the rules. At this time, there are a total of 8 binary bits, each with a group of 6, then the second group lacks 4 bits, which is filled with 0 to get two Base64 codes, while the latter two groups have no corresponding data and are all filled with "=". Therefore, in the figure above, "A" is converted to "QQ==".

Matters needing attention

Most encodings are converted from strings to binaries, while Base64 encodings are converted from binary to strings. Just the opposite of the routine

Base64 coding is mainly used in the field of transmission, storage and representation, which can not be regarded as encryption, but can not be seen directly in plaintext. Encryption can also be done by disrupting the Base64 code.

There are many kinds of codes in Chinese (for example: utf-8, gb2312, gbk, etc.), different codes correspond to different Base64 coding results.

3 extension

As we have seen above, Base64 uses 6 bits (the 6th power of 2 is 64) to represent characters, so it becomes Base64. In the same way, Base32 uses 5 bits and Base16 uses 4 bits. You can follow the steps above to evolve.

4Java verification

Finally, we use a piece of Java code to verify the above conversion result:

Package com.secbro2.blog.utils;import sun.misc.BASE64Encoder;/** * @ author zzs * / public class Base64Utils {public static void main (String [] args) {String man = "Man"; String a = "A"; String bc = "BC"; BASE64Encoder encoder = new BASE64Encoder (); System.out.println ("Man base64 result:" + encoder.encode (man.getBytes () System.out.println ("BC base64 result is:" + encoder.encode (bc.getBytes (); System.out.println ("A base64 result is:" + encoder.encode (a.getBytes ();}}

The print result is as follows:

Man base64 result: TWFu BC base64 result: QkM= A base64 result: QQ==

At this point, I believe that everyone on the "blockchain Base64 coding principle is what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report