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

How to realize Base64 and Base32

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to achieve Base64 and Base32". In daily operation, I believe many people have doubts about how to achieve Base64 and Base32. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve Base64 and Base32"! Next, please follow the editor to study!

What is the purpose of Base64

To write Base32, you must first understand Base64, so what is Base64 for? Why is there a Base64? This is the root cause, to figure out the process of Base64 production, then Base32, we can follow the gourd.

We know that in a computer, the unit of data is a byte byte, which is made up of 8 bits of binary and can have a total of 256 different numbers. So how do you transfer these binary data? We need to convert it to ASCII characters, which contain 33 control characters (invisible) and 95 visible characters. If we can convert these binary data into these 95 visible characters, we can transfer them normally. So, we selected 64 of the 95 characters and converted the binary data into these 64 visible characters, so that they could be transmitted normally, which is the origin of Base64. What are these 64 characters?

This is the 64 characters of Base64. So what if we want to implement Base32? By the way, we need to pick out 32 visible characters, as follows:

Private static final char [] toBase32 = {'A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X' 'Yee, 'Zhuan,' 0mm, '1mm,' 2mm, '3pm,' 4pm,'5'}

We chose the uppercase Amurz, plus 0-5, for a total of 32 visible characters.

What are the rules of Base32

All right, the 32 visible characters have been selected, and the next step is to convert the binary to these 32 characters. Let's take a look at what kind of conversion process Base64 is. We have a byte of 8 bits, and 64 is 2 to the power of 6, that is, a byte (8 bits) of data. We need to intercept 6 bits of it and encode it to get its visible characters. What about the remaining two digits? It will encode a 6-bit data with the first four bits of its own. So how many bytes do we need to get a complete bit-free encoding? We want to take the minimum common multiple of 6 and 8, that is, 24 bits are exactly 3 bytes. If we take 6 bits to encode, we can get 4 codes. Let's take a look at the picture below to better understand it.

The corresponding ASCII codes of MMagne are 77 and 97110, respectively.

The corresponding binary is 01001101pr 01100001pr 01101110.

Then we can intercept exactly four codes according to the 6-bit interception, and the corresponding 6-bit binaries are: 010011, 010110, 0000101, 101110.

The corresponding 64-bit code is: T.J. WJ. F.

By the same token, what if we are going to implement Base32? 32 is the fifth power of 2, so when we do the binary intercept, we have to intercept 5 bits at a time. So a byte of 8 bits, intercepted 5 bits, what about the remaining 3 bits? In the same way, the first two bits of the next byte form a new five bits. So how many bytes are intercepted according to 5 bits in order not to lose bits? We are going to take the minimum common multiple of 5 and 8, 40 bits, intercept according to 5 bits, and get exactly 8 codes. 40 bits, exactly 5 bytes, so we need to divide 5 bytes into a group and encode Base32. As shown below:

Compared with the previous Base64,Base32 is to intercept according to 5 bits, and then go to the coding table to find the corresponding characters. All right, we understand the principle, let's move on to the program stage.

Writing program stage

I understand the principle. How to write the program? This is the value of programmers, realizing the rules, functions and logic of reality with programs. But it is also difficult to implement Base32, but some ancestors left us Base64, so it is much easier for us to implement Base32 with reference to Base64.

Base32 coding

First of all, we need to determine the length of the returned byte according to the length of the input byte. Take the above example, the length of the input byte is 5, then the length of the Base32 transcoded byte is 8. So if the length of the input byte is 1, what is the byte length of the returned result? This requires padding, that is to say, the length of the input byte is not a multiple of 5, we need to fill in its length to a multiple of 5, so that after coding, the length of the returned byte is a multiple of 8. In this way, we will not lose information, for example, we only enter one byte, which is 8 bits, and when we encode, we intercept the first 5 bits, so what about the remaining 3 bits? Can not give up, we have to make up 40 bits behind it, make up with 0, intercept the remaining bits in front and add the 0 after the complement, put together 5 bits, and then code them. The rest are all 5-bit binaries of 0, which we encode as "=", which is the same as Base64.

All right, let's first take a look at how to calculate the length of the bytes returned after encoding.

/ / the array length of the returned result int rLength = 8 * ((src.length + 4) / 5); / / the returned result byte [] result = new byte [rLength]

Where src is the input byte array

We need to take a closer look at the formula of the return length, rounding 5 and multiplying it by 8, which is the most basic operation. Let's use the above example to set up the length of the input bytes is 5 bytes, 8 * (5 Universe 5) = 8, we need to return 8 bytes. Let's take a look at the effect of adding 4, for example, if we enter 1 byte, how many bytes will be returned? According to the previous requirements, if the binary length is less than 40 bits, 40 bits should be filled up, that is, the length of the input bytes should be filled up to an integer multiple of 5. Here, by adding 4 and rounding 5, you can fill the number of bits that can be fully encoded, and then multiply it by 8 to get the number of bytes returned. You can think of a few examples to verify whether the results are correct.

Then we define the array that returns the results.

The length of the array that returns the result has been determined. What are we going to do next? Coding, of course, here we are divided into two steps:

First deal with those bytes that can be normally encoded, that is, those that meet multiples of 5, which can be converted from 5 to 8 bytes without padding.

Then process the last few bits, which need to be filled, and fill them into 5 bytes.

The encoding steps have been determined, and the following is to determine the length of bytes that can be encoded normally and the length that needs to be padded, as follows:

/ / the length of normal conversion int normalLength = src.length / 5 * 5 / replacement length int fillLength = (5-(src.length% 5))% 5

There are two more calculation formulas. Let's take a look at them respectively:

The byte length that can be normally encoded is rounded to 5 and then multiplied by 5 to filter out the bytes that do not meet the multiple of 5. These filtered bytes need to be filled up to meet 5 bytes.

This step is to calculate the final number of bits needed to meet the multiple of 5, and finally get the length of the bit that needs to be padded. If the length of the input byte happens to be a multiple of 5 and there is no need to fill, the result of the calculation is 0. You can verify these two formulas.

Next, let's deal with the bytes that can be encoded normally, as follows:

/ / enter byte subscript int srcPos = 0Trachampact / return result subscript int resultPos = 0posiwhile (srcPos

< normalLength) { long bits = ((long)(src[srcPos++] & 0xff)) 30) & 0x1f)]; result[resultPos++] = (byte) toBase32[(int)((bits >

> 25) & 0x1f)]; result [resultPos++] = (byte) toBase32 [(int) ((bits > > 20) & 0x1f)]; result [resultPos++] = (byte) toBase32 [(int) ((bits > 15) & 0x1f)]; result [resultPos++] = (byte) toBase32 [(int) (bits > 10) & 0x1f)]; result [resultPos++] = (byte) toBase32 [(int) (bits > 5) & 0x1f)] Result [resultPos++] = (byte) toBase32 [(int) (bits & 0x1f)];}

Let's first define the subscript of the input byte and the subscript of the returned result, which are used as values and assignments.

Then write a while loop, as long as the input byte subscript is within the normal conversion range, it can be encoded normally.

Next, let's take a look at the details of the while loop. we first have to spell 5 bytes into a 40-bit binary. In the program, we get a long number through displacement operation and | or operation, of course, its binary is made up of 5 bytes.

Here is a pit to explain to you, our first byte shift when using long transformation, why? Because int occupies 4 bytes and 32 bits in Java, when we move 32 bits to the left, it will return to the rightmost position. While long occupies 64 bits, we will not cycle if we move 32 bits to the left. We should pay special attention to this point.

The next step is to split the 40-bit binary, also through the shift operation, intercepting 5 bits from the left, we move 35, 30, 25, 20, 15, 10, 5, 0 to the right respectively, and then operate it with 0x1f. 0x1f is a hexadecimal number, its binary is 0001 1111, that's right, it is five 1s, after shift and 0x1f, only the rightmost 5-bit binary is left, and its value is calculated. Then find the corresponding characters from the 32-bit coding table.

The part that can be encoded normally ends normally, and we should understand more about the use of displacement symbols. Next, let's look at the handling of the ending byte. Code first:

If (fillLength > 0) {switch (fillLength) {case 1: int normalBits1 = (src [srcPos] & 0xff) > 22) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits1 > > 17) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits1 > 12) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits1 > 7) & 0x1f] Result [resultPos++] = (byte) toBase32 [(normalBits1 > 2) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits1 > 14) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits2 > > 9) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits2 > 4) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits2 11) & 0x1f] Result [resultPos++] = (byte) toBase32 [(normalBits3 > > 6) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits3 > > 1) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits3 > 3) & 0x1f]; result [resultPos++] = (byte) toBase32 [(normalBits4)

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: 298

*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

Development

Wechat

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

12
Report