preface

I believe that more than the vast number of programmers have used coding and decoding in a variety of software development, coding and decoding is corresponding, there is coding there is decoding.


Base16, Base32 and Base64 are all encoding methods, which should have their own encoding algorithms. But some people often call them cryptography, such as Base64 cryptography. That’s not entirely true. Because the default base16, base32, base64 information is public, public includes:

  • The way an algorithm works
  • Code table, this is the main one

In base16, base32, base64, once you have mastered the above two kinds of information, it is equivalent to cracking. You can even manually write out the result of coding and write out the decoding content according to the result of coding.

16,32, and 64 are coded in the same way, except for the following two things:

  • The number of subscript bits in a table for a character
  • What is the corresponding code table

What do these two points mean? Firstly, we know that the char data type 2 bytes, and, of course, the second situation, such as 1 byte, but here we have 2 bytes, for example, for example: char r = ‘a’, so if the rules are changed, since you can use two bytes one character at a time, so why can’t use 3 bytes, 5 bytes? When we have other bytes representing a character, we have other effects. The first point above is something like this. When the number of subscript bits changes, the range of the subscript values also changes. For example, the maximum number of two bits is 11 = 3, and the maximum number of three bits is 111 = 7. For the second point, it is a table to look up tables, such as the 9×9 multiplication table. This table is to be queried. Encoding flow With the above two conditions, we summarize the encoding flow into the following points: for example, the string to be encoded is: ILU

  • Convert each character in the ILU string to a value corresponding to the Ascii encoding table, I = 73, L = 76, U = 85.
  • Convert the Ascii values in step 1 to the corresponding binary format, which must form 8 bits and fill 0 with a high value less than 8 bits. For example, the binary of 1 is 1, which is obviously not enough for 8 bits, and should be displayed as :0000 0001. The conversion results of ILU are as follows:


    73 = 01001001


    76 = 01001100


    85 = 01010101


  • The second step is divided according to the rule that y bits specified in the base X encoding algorithm are subscripts of a character in the table. For example, the base 16 rule requires that four bits correspond to one character as a subscript, that is, every four bits are part of a character, so it is divided as follows:


    Part 1:0100 is (73 = first 4 digits of 01001001)


    Part 2:1001 is (73 = last 4 digits of 01001001)


    Part 3:0100


    Part 4:1100


    Part 5:0101


    Part 6:0101


  • Each part divided in step 3 is converted into base 10 and the subscript value corresponding to the base 10 number is obtained as follows:


    0100 = 4,1001 = 9,4,12,5,5


  • The last step, the subscript number obtained in the fourth step to look up the table, get the corresponding characters, connected together, is the coding result


    The default encoding table of Base16 contains 16 characters: digits 0 to 9 and letters A to F. The subscripts and values of each character are tabled as follows:


    Encoding table of Base16

    Subscript code value Subscript code value

    0088

    1199

    2210A

    3311B

    4412C

    5513D

    6614E

    7715F

    The final base16 encoding result of ILU is 494C55



In the custom table code, we can specify our own encoding table, such as the following line: Var encoding = base32. NewEncoding (” ybndrfg8ejkmcpqxot1uwisza345h769 “) copying code ybndrfg8ejkmcpqxot1uwisza345h769 is 32 characters, When the ILU example above was encoded in this table, it would no longer be: 494C55 Summarized in step 3 above, for integer partition (8/4 = 2 divisible), the final result will not be supplemented with the equal sign “=”, and the final result will be supplemented with “=”. The following is the number of bits required by 16,32, and 64 and the total number of characters in the encoding table the number of subscript digits in the encoding table whether the number of characters in the encoding table will be completed if the number of characters is insufficient = base 164 digits 0 to 9 and letters A to F no, Base 325 Uppercase letters A to Z and digits 2 to 7 base 646 Uppercase letters A to Z, lowercase letters A to Z, digits 0 to 9, and “+”, “/” After the name encoding, the amount of data changes. Base 16 is changed from an 8-bit character to A 4-bit character. Data quantification changes from 2 times base32 to 8/5 times base 64 to 8/6=4/3 times completion limit. Take Base32 for example, because every 5 bits represents a character subscript value, and the original data is 8 bits, this means that partitioning will occur in the rest of the case, for example: 8-5 = 3, obviously there are 3 bits left, so what is the minimum number of bits left for the step to occur? This is a least common multiple problem, which is: 5*8 = 40 digits. We can verify that when it’s two characters, it’s 16 bits, 16/5 = 1, and so on. Finally, it is concluded that in base32 encoding, the data to be encoded must be at least >= 40 bits, so that the final encoding result can not have the = sign. For example, if the character to be encoded is 3, it is obvious that the result is: D=======, followed by =. Similarly, base64 is at least 24 bits, and 24 is the least common multiple of 6 and 8. That’s all, remember the main points of this type of encoding:

  • The number of subscript bits in a table for a character
  • What is the corresponding code table

The rest, is to follow the pattern, division, table, the results. Link: https://juejin.cn/post/6844903891973177357 source: the Denver nuggets