当前位置:网站首页>The best Base64 encoding and decoding tutorial in the whole network, with 6 examples!
The best Base64 encoding and decoding tutorial in the whole network, with 6 examples!
2022-06-24 01:05:00 【Network technology alliance station】
base64 Encoding can convert binary data to text format , And can transmit data on any communication medium .
There are many coding application scenarios , For example, e-mail encryption .
Base64 Method is usually a binary to text encoding scheme , It said 8 Byte binary data to ASCII String format , When transferring or bootstrapping data between various media , There are many advantages , Especially for text support .
according to Base64 Representation table , Binary data can be converted to 64 Different species ASCII character , This conversion is easy to transmit and print .
Using letters A To Z、a To Z、0 To 9 and + and /, in total 64 individual ASCII Character to represent from 000000 To 111111 Binary system , Each non final Base64 The number just stands for 6 Bit data .
Bash base64 Encoding and decoding
grammar
base64 [ Parameters ] [ Input file ] [ The output file ]
- Parameters : The parameters are as follows .
- Input file : You can input from the standard ( Command line ) Or file .
- The output file : You can redirect output to standard output , Such as terminal or file .
(1) Parameters -e or –encode :
Used to encode any data from standard input or any file , Is the default option .
(2) Parameters -d or –decode :
Used to decode any encoded data from standard input or any file .
(3) Parameters -n or –noerrcheck
By default ,base64 Errors are checked when decoding any data , have access to –n or –noerrcheck Option to ignore the check when decoding .
(4) Parameters -i or --ignore-garbage
Used to ignore non alphabetic characters when decoding .
(5) Parameters -u or -help
Used to get information about the usage of this command .
Example 1 – Basic coding
stay Linux in , Default installation base64 package , therefore , You can easily use it from the command line , To simply encode a string or text , You can pipe it to the command line and get the encoded text .
In the following example , character string wljslmz.cn Encoded as base64.
echo "wljslmz.cn" | base64
Output :
d2xqc2xtei5jbgo=
explain
The encoding method uses several steps to transform the input :
- The input character is converted to 8 Bit binary value
- The entire set of binary strings is split into 6 Bit binary value
- Convert to decimal
- Each decimal value passes through base64 The index table is converted to base64 character
Example 2 – Basic decoding
To decode a string , Just use parameters –decode Pass the encoded value to base64, It decodes the string just entered .
echo "d2xqc2xtei5jbgo=" | base64 --decode
Example 3 – Code text file
You can use the same command to encode a text file and redirect the output to another text file :
base64 wljslmz.txt > wljslmz-encode.txt
Example 4 – Decode text file
To decode use base64 Encoded text file , Just use –decode or -d Switch and pass the text file name .
base64 -d wljslmz-encode.txt
Example 5 – Encode custom input from the user
Use bash shell Programming , You can get input from the user through the terminal and encode it , But to do this, you need to write a simple shell Script and execute it after granting executable rights .
The following examples can be used as a reference :
#!/bin/bash # Welcome to the O & M talk !!! # You can enter wljslmz.cn echo " Please enter base64 Encoded character ::" read input_text output_text=`echo -n $input_text | base64` echo "Base64 Characters after encoding : $output_text"
Example 6 – Use base64 Simple authentication for
Use the above encoding and decoding methods , You can implement a simple authentication system .
You can ask the user to enter a password or password , Then the password is stored in a file or dynamically compared , If the stored encoding string matches the encoded text entered by the user , Then the user passes authentication .
This is a very simple method of authentication , Sometimes useful for simple business cases .
#!/bin/bash
# Welcome to the O & M talk , This is a simple script , use base64 code
# Use debugpoint.cn For example
echo " Verify your password "
read pwd1
decoded_text=`echo 'd2xqc2xtei5jbg==' | base64 --decode`
if [[ $pwd1 == $decoded_text ]]
then
echo " Hello, master !"
else
echo " You're a bad guy , Verification failed !!!"
fiLet's try this script :
summary
Above, we have introduced in detail through several examples base64 The principle and use of coding , It must be as long as you look at the above , There must be gains , It is better to move your heart manually , Now let's open the command line , Knock madly .
边栏推荐
- 使用递归形成多级目录树结构,附带可能是全网最详细注释。
- Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
- [CVPR 2020] conference version: a physics based noise formation model for extreme low light raw denoising
- 【ICPR 2021】遥感图中的密集小目标检测:Tiny Object Detection in Aerial Images
- Cvpr2022 𞓜 thin domain adaptation
- Installation and use of winscp and putty
- 杂乱的知识点
- DML操作
- 【osg】OSG开发(04)—创建多个场景视图
- DML operation
猜你喜欢

Apple iphone14 is equipped with Beidou navigation system. What are the advantages of Beidou vs GPS?
Shengdun technology joined dragon lizard community to build a new open source ecosystem

Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)

用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)

Perhaps the greatest romance of programmers is to commemorate their dead mother with a software

【CVPR 2022】高分辨率小目标检测:Cascaded Sparse Query for Accelerating High-Resolution Smal Object Detection

Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization

C语言:利用自定义函数排序

ShardingSphere-proxy-5.0.0容量范围分片的实现(五)
随机推荐
Grab startup logcat
What is memory out of order access?
【Flutter】如何使用Flutter包和插件
Map design
Google Earth engine (GEE) - verification results used by NDVI, NDWI and NDBI to increase classification accuracy (random forest and cart classification)
智能制造时代下,MES管理系统需要解决哪些问题
Efficient integration of heterogeneous single cell transcriptome with scanorama
【小程序】相对路径和绝对路径的表示符
WinSCP和PuTTY的安装和使用
GNN upper edge distributor! Instead of trying to refine pills, you might as well give your GNN some tricks
[shutter] how to use shutter packages and plug-ins
Forward design of business application data technology architecture
【小程序】编译预览小程序时,出现-80063错误提示
Using anydesk remote control for intranet penetration horizontal movement
Isn't this another go bug?
Alibaba interview question: multi thread related
SQL数据库:知识点汇总,期末不挂科
Longest substring without duplicate characters
paddle使用指南
数据管理:业务数据清洗,落地实现方案