当前位置:网站首页>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
边栏推荐
猜你喜欢
Solve the problem that El radio group cannot be edited after echo
[apipost] tutorial
Tmall product details interface (APP, H5 end)
Yolov6 training: various problems encountered in training your dataset
Makefile 分隔文件名与后缀
蜻蜓低代码安全工具平台开发之路
Dragonfly low code security tool platform development path
c语言入门--数组
CTO如何帮助业务?
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
随机推荐
实现一个多进程并发的服务器
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
OpenCV调用USB摄像头的点滴
可视化搭建页面工具的前世今生
表格响应式布局小技巧
Leetcode - Search 2D matrix
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
C# 线程传参
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
Base64 编码原来还可以这么理解
info [email protected]: The platform “win32“ is incompatible with this module.
CDN 在游戏领域的应用
Arithmetic operations and related exercises in C language
Bit by bit of OpenCV calling USB camera
Implement a server with multi process concurrency
Printf function and scanf function in C language
mathjax 入门(web显示数学公式,矢量的)
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
[noi simulation] Elis (greedy, simulation)