base64编码和解码
Base64 is an encoding and decoding algorithm. We need the encoding of some data in order to prevent storing and transmission problems. For example, If we try to transfer binary data it may create and affect some network devices because of its data structures. Base64 is generally used different applications like email and MIME, XML, JSON, URL, and HTTP, etc.
Base64是一种编码和解码算法。 我们需要对某些数据进行编码,以防止存储和传输问题。 例如,如果我们尝试传输二进制数据,由于其数据结构,它可能会创建并影响某些网络设备。 Base64通常用于不同的应用程序,例如电子邮件和MIME,XML,JSON,URL和HTTP等。
资料呈现 (Data Presentation)
Base64 uses alphabet, numbers and =
for encoded data. For example, the following examples are valid Base64 encoded data
Base64将字母,数字和=
用于编码数据。 例如,以下示例是有效的Base64编码数据
dGVzdA==
ZGVuZW1l
And following are not valid encoded data
以下是无效的编码数据
dG.?zdA==
ZG;,ZW1l
Base64不加密 (Base64 Is Not Encryption)
Base64 is just encoding format so it is not used to encrypt data to hide from third parties. Base64 encoded data can be easily reverted or decoded back to the text format. It can work two way without a security restriction of password.
Base64只是编码格式,因此它不用于加密数据以对第三方隐藏。 Base64编码的数据可以轻松地还原或解码回文本格式。 它可以两种方式工作,而没有密码的安全限制。
Linux Base64 (Linux Base64)
Now we will look some examples about to encrypt and decrypt Base64 in Linux bash environment.
现在,我们将看一些有关在Linux bash环境中加密和解密Base64的示例。
编码 (Encode)
We will encode file contents named data.txt
with base64
command. We do not provide any option for this.
我们将使用base64
命令对名为data.txt
文件内容进行编码。 我们对此不提供任何选择。
$ base64 data.txt
解码 (Decode)
Decoding is very similar to encode we will just provide -d
option to the base64
command like below. In this example, we will use a file named encoded.txt
which contains base64 encoded data.
解码与编码非常相似,我们仅向base64
命令提供-d
选项,如下所示。 在此示例中,我们将使用一个名为encoded.txt
的文件,其中包含base64编码的数据。
$ base64 -d encoded.txt
Javascript Base64 (Javascript Base64)
Javascript provides btoa
and atob()
functions which are a short form of binary to ascii
and ascii to binary
. We will just provide the text or data we want to convert to these functions like below.
Javascript提供了btoa
和atob()
函数,它们是binary to ascii
以及ascii to binary
的缩写。 我们将仅提供要转换为以下功能的文本或数据,如下所示。
//Encode
atob("Test");
//Decode
btoa("VGVzdAo=")
PHP的Base64 (PHP Base64)
In PHP Programming language we can use base64_encode
and base64_decode
functions like below.
在PHP编程语言中,我们可以使用base64_encode
和base64_decode
函数,如下所示。
//Encoding
base64_encode("Test");
//Decode
base64_decode("VGVzdAo=")
翻译自: https://www.poftut.com/what-is-base64-and-how-to-encode-and-decode-text/
base64编码和解码