当前位置:网站首页>Base64 coding can be understood this way
Base64 coding can be understood this way
2022-07-02 15:03:00 【zx_ twenty million two hundred and twenty thousand one hundred 】
Worked as a programmer for four years , Frequently used Base64 code , But why use , And how it works , It has always been without understanding . I saw it in a book recently Base64 The coding principle of , It turned out to be so simple .
Base64 What problems have been solved
If we want to transmit the following problems in the network
Hello Children I'm brick family who are you ? This text contains spaces 、 Line break 、 Invisible characters such as carriage return , In network transmission , Each device may have different processing mechanisms for invisible characters , There will be information transmission errors . At this point, we need a coding mechanism , Turn all invisible characters into visible characters .
Base64 Coding principle
Next, we will pass abcd Two words turn into Base64 code , To understand its principle .
Group the characters into three groups , Not enough time to pass 0 A filling . such as abc Three characters in a group ,d Not enough for three , adopt 0 Complete as d00

2. Convert characters into binary ASCII code

3. Convert the converted binary , Separate every six digits

Q: Why does the first step need to be divided into three characters ?
A: because ASCII Code each character is 8 Bit binary ,3 * 8 = 24. It can be divided into 4 individual 6 Bit binary ,4 * 6 = 24.
6 Bit binary can represent 0-63, We have a rainbow watch , Can be 0-63 Each corresponds to a character , The rainbow table is as follows :

Through the rainbow table , We can put every 6 The binary after bit division is converted

It should be noted that , Where filling occurs 0, Need to use = To express . So the last two are =
Last abcd Convert to Base64 After coding is YWJjZA==, The overall principle is relatively simple , It doesn't involve sophisticated algorithms .
Base64 periphery
because Base64 Transcoding will include +/= character , stay URL Unable to transmit normally . So there is a kind of URL Friendly Base64 code , It exists and is used _- Instead of +/, And do not add =
Base32、Base16 Principle and Base64 Almost the same
Base64 Not suitable for encryption , Because decryption is too simple
Chinese characters pass gb2312、utf-8、gbk After the coding , It can be converted to binary processing . Here is a picture description on the Internet :

Base58 And Base58Check
Base58 It is a coding form used by bitcoin when generating wallet address . It and Base64 The main difference is to remove the characters that are easy to be mistaken by the naked eye 0( zero )、O( Capital O)、I( Capital i)、l( Lowercase letters L) And several characters that affect the double-click selection :/ and + The purpose of this coding is relatively simple , Is to prevent transfer , Read the wrong account , Transfer to wrong account .Base58 The principle of is simpler , It only involves the conversion of Radix , The general steps are as follows :
Convert characters to ASCII code
take ASCII Code to 58 Base number
Through rainbow table mapping
such as abcd transformation :
Turn into ASCII code 97-98-99-100
convert to 58 Base number 3-28-21-49-5-22
Map to 3VNr6P

Base58Check code , As the name suggests, it can be right Base58 Code to check . For example, I am transmitting qH912cvztx When coding , If the network is abnormal and other reasons , Cause data disorder or loss , The receiver should be able to recognize that there is a problem with the data .Base58Check The basic principle of coding is as follows :
stay data Add a version ID in front prefix, Used to identify the encoded data type , For example, the prefix of bitcoin address is 0( Hex is 0x00)
adopt hash Algorithm calculation prefix + data Of hash value :SHA256(prefix + data)
The generated hash The value is truncated to the first four digits , Splice back prefix + data + 4 position hash
Carry out the result generated in the previous step Base58 Coding calculation , Get the final result
边栏推荐
- socket(套接字)与socket地址
- info [email protected]: The platform “win32“ is incompatible with this module.
- AtCoder Beginner Contest 254
- qml 弹窗框架,可定制
- kibana 基础操作
- C RichTextBox controls the maximum number of lines displayed
- Large top heap, small top heap and heap sequencing
- C # delay, start the timer in the thread, and obtain the system time
- 蜻蜓低代码安全工具平台开发之路
- mathML转latex
猜你喜欢

GeoServer offline map service construction and layer Publishing

LeetCode 209. 长度最小的子数组

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting

Btrace- (bytecode) dynamic tracking tool

Actual combat sharing of shutter screen acquisition

微信小程序使用towxml显示公式

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

复用和分用

LeetCode 2320. Count the number of ways to place the house

obsidian安装第三方插件——无法加载插件
随机推荐
##51单片机实验之简易验证码发生器
【NOI模拟赛】伊莉斯elis(贪心,模拟)
Kityformula editor configure font size and spacing
Xilinx Vivado set *. svh as SystemVerilog Header
AtCoder Beginner Contest 254
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
MFC A对话框调用B对话框函数并传参
牛客练习赛101
Printf function and scanf function in C language
HUSTPC2022
CDN 在游戏领域的应用
[noi simulation] Elis (greedy, simulation)
AtCoder Beginner Contest 254
C RichTextBox controls the maximum number of lines displayed
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
SQL 后计算的利器 SPL
C语言中的算术运算及相关练习题
JMeter script parameterization
kibana 基础操作
MFC 定时器使用
