Merge pull request #27666 from chai001125/master

提交译文
This commit is contained in:
geekpi 2022-10-24 08:45:12 +08:00 committed by GitHub
commit 17275f9f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 160 additions and 158 deletions

View File

@ -1,158 +0,0 @@
[#]: subject: "Learn Bash base64 Encode and Decode With Examples"
[#]: via: "https://www.debugpoint.com/bash-base64-encode-decode/"
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
[#]: collector: "lkxed"
[#]: translator: "chai001125"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Learn Bash base64 Encode and Decode With Examples
======
Want to learn about the base64 encode and decode method? Here in this tutorial, we explain the base64 encode and decode steps using bash shell scripting with various examples.
![][1]
The base64 encoding method transmits data over any communication medium by converting binary data to text. This method is primarily used for the email encryption process.
The Base64 method, in general, is a binary-to-text encoding scheme representing 8-byte binary data in ASCII string format. This has several advantages while transmitting or channelling data among various mediums especially those that reliably support text content. Hence, it is widely used on World Wide Web. Probably the most used case of this encoding scheme is using it for email attachments.
As per the Base64 representation table, the binary data can be converted to 64 different ASCII characters which are easy to transmit and printable. This encoding method uses letters A to Z, a to Z, 0 to 9 and + and /.
A total of 64 ASCII characters to represent binary from `000000` to `111111`. Each non-final Base64 digit represents exactly 6 bits of data.
![Base64 Index Table][2]
### Bash base64 encode and decode
#### Syntax
Before you learn about the examples, here is the basic syntax.
```
base64 [OPTIONs] [INFILE] [OUTFILE]
```
Option: You can provide any of the options or combine them as explained below.INFILE: Input can be picked up from standard input (like the command line) or files.OUTFILE: You can redirect the output to the standard output, like the terminal or to a file.
| Arguments | Descriptions |
| :- | :- |
| -e or encode | This option is used to encode any data from standard input or any file. It is the default option. |
| -d or decode | This option is used to decode any encoded data from standard input or any file. |
| -n or noerrcheck | By default, base64 checks error while decoding any data. You can use n or noerrcheck option to ignore checking at the time of decoding. |
| -i, ignore-garbage | This option is used to ignore non-alphabet characters while decoding. |
| -u or help | This option is used to get information about the usage of this command. |
#### Example 1 A basic encode
In Linux, the base64 package is installed by default. Hence, you can use it from the command line easily. To simply encode a string or text, you can pass it through the command line via piping and get the encoded text. In this example, the string debugpoint.com is encoded to base64.
```
echo "debugpoint.com" | base64
```
![bash base64 encode and decode - example 1][3]
The result is the base64 encoded string.
#### Explanation
The encoding method uses several steps to convert the input. The input characters are converted to 8-bit binary values. The entire set of the binary string is split into 6-bit binary values, which are converted to decimals.
Each decimal value is translated to the base64 character via the base64 index table.
In the above example, the first character, “d”, is converted to binary `01100100`. The first 6 bits are `011001`, which is 25 in decimal. The 25 refers to the Z in the base64 index table. And this goes on for the entire stream of text. See the example below.
![Base64 Encode and Decode inner working][4]
#### Example 2 A basic decode
To decode the string, simply pass the encoded value to the base64 with the option `--decode`. And it will give you the exact input string.
![bash base64 encode and decode - example 2 (decode the same example)][5]
#### Example 3 Encode a Text file
The same command can be used to encode a text file and redirect the output to another text file. Heres how.
```
base64 example3.txt > example3-encoded.txt
```
![Encode a text file][6]
#### Example 4 Decode a Text File
And to decode a text file that was encoded using base64, simply use the `--decode` or `-d` switch and pass on the text file name.
```
base64 -d example3-encoded.txt
```
#### Example 5 Encode a custom input from the user
Using bash shell programming, you can take input from the user via the terminal and encode it. But for that, you need to write a simple shell script and execute it after giving executable permission.
Heres a simple example which takes input from the user and displays the encoded string.
```
#!/bin/bash
#Sample program to take input, encode to base64 and display on terminal
#Example by www.debugpoint.com
echo "Enter text for encoding to base64:"
read input_text
output_text=`echo -n $input_text | base64`
echo "The Base64 Encoded text is: $output_text"
```
![Custom input - base64 encode and decode using script][7]
#### Example 6 A Simple Authentication using base64
You can implement a simple authentication system using the above encode and decode method. You can ask the user to enter a password or a secret code. Then store the secret code in a file or compare it on the fly.
If the stored encoded string matches with the user input encoded text, then the user is authenticated. However, it is a straightforward way of checking an authentication, but sometimes useful for simple business cases.
```
#!/bin/bash
#Sample program to take input, encode to base64 and display on terminal
#Example by www.debugpoint.com
echo "Type your password"
read pwd1
decoded_text=`echo 'U2lsZW5jZSBpcyBnb2xkZW4h' | base64 --decode`
if [[ $pwd1 == $decoded_text ]]
then
echo "You are a valid user."
else
echo "You are NOT a valid user."
fi
```
![A Simple Authentication using bash base64][8]
### Conclusion
I hope you get to learn the basics of [Base64][9] encode and decode with these examples. Also, learn a bit about its inner workings. Let me know in the comment box below if this helps you or need additional tutorials on this topic.
--------------------------------------------------------------------------------
via: https://www.debugpoint.com/bash-base64-encode-decode/
作者:[Arindam][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.debugpoint.com/author/admin1/
[b]: https://github.com/lkxed
[1]: https://www.debugpoint.com/wp-content/uploads/2021/11/base64example-1024x576.jpg
[2]: https://www.debugpoint.com/wp-content/uploads/2021/11/Base64-Index-Table.png
[3]: https://www.debugpoint.com/wp-content/uploads/2021/11/bash-base64-encode-and-decode-example-1.jpg
[4]: https://www.debugpoint.com/wp-content/uploads/2021/11/Base64-Encode-and-Decode-inner-working.png
[5]: https://www.debugpoint.com/wp-content/uploads/2021/11/bash-base64-encode-and-decode-example-2-decode-the-same-example.jpg
[6]: https://www.debugpoint.com/wp-content/uploads/2021/11/Encode-a-text-file.png
[7]: https://www.debugpoint.com/wp-content/uploads/2021/11/Custom-input-base64-encode-and-decode-using-script.png
[8]: https://www.debugpoint.com/wp-content/uploads/2021/11/A-Simple-Authentication-using-bash-base64.png
[9]: https://linux.die.net/man/1/base64

View File

@ -0,0 +1,160 @@
[#]: subject: "Learn Bash base64 Encode and Decode With Examples"
[#]: via: "https://www.debugpoint.com/bash-base64-encode-decode/"
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
[#]: collector: "lkxed"
[#]: translator: "chai001125"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
通过示例来学习 Bash base64 的编码和解码
======
你想了解 Base64 编码和解码的方法吗?在本教程中,我们使用 bash shell 脚本和各种示例解释了 Base64 编码和解码步骤。
![][1]
Base64 编码方法通过将二进制数据转换为文本,如此编码数据可以在任何通信媒体进行传输。这种编码方法主要用于电子邮件加密的过程。
总体而言Base64 编码方法是一种二进制到文本的编码方案,以 ASCII 字符串格式表示 8 字节的二进制数据。使用这种编码方法在各种媒介之间传输数据时有几个优势尤其是对于那些能可靠支持文本内容的媒介。因此Base64 编码方法在万维网上被广泛使用。这种编码方案最常用于电子邮件附件的编码上。
根据 Base64 编码表,二进制数据可以经 Base64 编码后可以转换为 64 个不同的 ASCII 字符,包含大写字母 A 到 Z小写字母 a 到 Z数字 0 到 9 ,以及符号 + 和 /,这些字符在传输和打印上十分便捷。
这 64 个 ASCII 字符代表着从 `000000``111111` 的二进制值。每个非末尾的 Base64 编码后的 ASCII 字符恰好代表 6 位二进制值。
![Base64 Index Table][2]
### Bash base64 的编码和解码
#### 句法
在我们提供示例之前,首先介绍 Base64 的基本语法。
```
base64 [OPTIONs] [INFILE] [OUTFILE]
```
选项Option参照下面的表格你可以提供任何的选项或组合多个选项。
输入INFILE你可以从标准输入如命令行或文件中输入。
输出OUTFILE你可以将输出重定向到标准输出如终端或文件中。
| 选项 | 描述 |
| :- | :- |
| -e 或者 encode | 此选项用于对标准输入的数据或从文件中读入的数据进行编码。这是默认选项。 |
| -d 或者 decode | 此选项用于对标准输入的数据或从文件中读入的已 base64 编码数据进行解码。 |
| -n 或者 noerrcheck | 默认情况下base64 在解码数据时,会自动检查是否有错误。你可以使用 n 或 noerrcheck 选项,在解码时忽略检查。 |
| -i, ignore-garbage | 此选项用于在解码时忽略非字母字符。 |
| -u 或者 help | 此选项用于获取有关使用此命令的信息。 |
#### 示例 1基本编码
在 Linux 中,默认已安装好 base64 软件包。因此,你可以轻松地从命令行使用 base64。要对一个字符串或文本进行编码你可以通过管道将其传递到命令行并获取待编码的文本。在下面的示例中对字符串 debugpoint.com 进行了 base64 编码。
```
echo "debugpoint.com" | base64
```
![bash base64 encode and decode - example 1][3]
结果是经过 base64 编码后的字符串。
#### 解释
Base64 编码方法使用下面的几个步骤来转换输入的数据。首先,每个输入字符转换为 8 位二进制值,接着,二进制字符串拆分为一组组 6 位的二进制值,然后,每个 6 位的二进制值被转换为十进制值。
最后,每个十进制值都通过 base64 编码索引表转换为 base64 字符。
在上面的示例中,第一个字符 `d` 被转换为二进制 `01100100`。前 6 位是 `011001`,转换为十进制是 `25`。`25` 在 base64 编码索引表中对应着 `Z`。整个输入的文本流都像如此编码。请参阅以下编码过程的示例。
![Base64 Encode and Decode inner working][4]
#### 示例 2基本解码
要解码字符串,需要将编码值传递给 base64选项为 `--decode`,它将输出你之前输入的字符串。
![bash base64 encode and decode - example 2 (decode the same example)][5]
#### 示例 3对文本文件进行编码
示例 1 中的同一命令也可用于编码文本文件,并将输出重定向到另一个文本文件。方法如下。
```
base64 example3.txt > example3-encoded.txt
```
![Encode a text file][6]
#### 示例 4对文本文件进行解码
要解码使用 base64 编码的文本文件,只需使用 `--decode``-d` 选项,并传递文本文件名。
```
base64 -d example3-encoded.txt
```
#### 示例 5对用户输入的数据进行编码
使用 bash shell 编程,你可以通过终端接收用户的输入,并对其进行 base64 编码。你需要先编写一个简单的 shell 脚本,并在授予可执行权限后执行。
以下就是一个简单的示例,它从用户那里获得输入,然后进行 base64 编码,最终显示编码的字符串。
```
#!/bin/bash
#Sample program to take input, encode to base64 and display on terminal
#Example by www.debugpoint.com
echo "Enter text for encoding to base64:"
read input_text
output_text=`echo -n $input_text | base64`
echo "The Base64 Encoded text is: $output_text"
```
![Custom input - base64 encode and decode using script][7]
#### 示例 6用 base64 进行简单的身份认证
你可以运用上述的编码和解码方法,实现一个简单的身份验证系统。你可以让用户输入密码或密码,然后将密码存储在文件中。或者进行实时比较。
如果存储的编码字符串与用户输入的文本再编码的字符串相匹配,则用户可以通过验证。虽然这是一种检查身份验证的很简单的方法,但有时这对一些简单的业务案例很有用。
```
#!/bin/bash
#Sample program to take input, encode to base64 and display on terminal
#Example by www.debugpoint.com
echo "Type your password"
read pwd1
decoded_text=`echo 'U2lsZW5jZSBpcyBnb2xkZW4h' | base64 --decode`
if [[ $pwd1 == $decoded_text ]]
then
echo "You are a valid user."
else
echo "You are NOT a valid user."
fi
```
![A Simple Authentication using bash base64][8]
### 结论
我希望你能通过这些示例,学会 [Base64][9] 编码和解码的基础知识。此外,你也了解到 Base64 的内部编码方式。如果这对你很有帮助,或你还需要有关此主题的其他教程,请在下面的评论区中告诉我吧。
--------------------------------------------------------------------------------
via: https://www.debugpoint.com/bash-base64-encode-decode/
作者:[Arindam][a]
选题:[lkxed][b]
译者:[chai001125](https://github.com/chai001125)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.debugpoint.com/author/admin1/
[b]: https://github.com/lkxed
[1]: https://www.debugpoint.com/wp-content/uploads/2021/11/base64example-1024x576.jpg
[2]: https://www.debugpoint.com/wp-content/uploads/2021/11/Base64-Index-Table.png
[3]: https://www.debugpoint.com/wp-content/uploads/2021/11/bash-base64-encode-and-decode-example-1.jpg
[4]: https://www.debugpoint.com/wp-content/uploads/2021/11/Base64-Encode-and-Decode-inner-working.png
[5]: https://www.debugpoint.com/wp-content/uploads/2021/11/bash-base64-encode-and-decode-example-2-decode-the-same-example.jpg
[6]: https://www.debugpoint.com/wp-content/uploads/2021/11/Encode-a-text-file.png
[7]: https://www.debugpoint.com/wp-content/uploads/2021/11/Custom-input-base64-encode-and-decode-using-script.png
[8]: https://www.debugpoint.com/wp-content/uploads/2021/11/A-Simple-Authentication-using-bash-base64.png
[9]: https://linux.die.net/man/1/base64