当前位置:网站首页>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
边栏推荐
- AtCoder Beginner Contest 254
- About text selection in web pages and counting the length of selected text
- Bit by bit of OpenCV calling USB camera
- info [email protected]: The platform “win32“ is incompatible with this module.
- Xilinx Vivado set *. svh as SystemVerilog Header
- Printf function and scanf function in C language
- 【NOI模拟赛】刮痧(动态规划)
- .NET Core 日志系统
- C#代码审计实战+前置知识
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
猜你喜欢
随机推荐
传感器数据怎么写入电脑数据库
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
复用和分用
LeetCode - 搜索二维矩阵
LeetCode 2320. Count the number of ways to place the house
C语言中的算术运算及相关练习题
Factal: Unsafe repository is owned by someone else Solution
CDN 在游戏领域的应用
Xilinx Vivado set *.svh as SystemVerilog Header
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
Introduction to mathjax (web display of mathematical formulas, vector)
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
C语言习题---(数组)
Fundamentals of software testing
华为面试题: 没有回文串
大顶堆、小顶堆与堆排序
c语言入门--数组
Record an error report, solve the experience, rely on repetition
871. Minimum refueling times: simple priority queue (heap) greedy question
Simple verification code generator for 51 single chip microcomputer experiment










