本文介绍了为什么我们使用Base64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我想发送

 你好
世界!

一种方法是以ASCII格式发送

  72 101 108 108 111 10 119 111 114 108 100 33 

但是字节10可能不会被正确解释为另一端的换行符。因此,我们使用ASCII子集对此进行编码。

  83 71 86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61 

这是以更多的数据传输为代价,即使接收器恰好对字符集的其余部分有不同的解释,接收器也可以以预期的方式解码数据。

解决方案

您的第一个错误是认为ASCII编码和Base64编码是可互换的。他们不是。它们用于不同的目的。




  • 在ASCII中编码文本时,以文本字符串开始并将其转换为字节序列

  • 当您在Base64中编码数据时,您将以字节序列开始,并将其转换为文本字符串。



为了理解为什么Base64是必要的,我们需要一点计算历史。






计算机以二进制 - 0和1进行通信,但人们通常希望与更丰富的表单数据(如文本或图像)进行通信。为了在计算机之间传输该数据,首先必须将其编码为0和1,发送,然后再次解码。以文本为例 - 有许多不同的方式来执行这种编码。如果我们可以同意一个单一的编码,这将会简单得多,但是很遗憾这不是这样。



最初创建了很多不同的编码(例如),它使用每个字符不同的位数,直到最终ASCII成为每个7位的标准字符。然而,大多数计算机存储由8位组成的字节的二进制数据,因此不适合传输数据类型。一些系统甚至会擦除最高有效位。此外,系统中的行结束编码的差异意味着ASCII字符10和13有时也被修改。



要解决这些问题编码。这允许您将已知为安全发送的字节编码为字节,而不会损坏(ASCII字母数字字符和几个符号)。缺点是使用Base64编码消息增加其长度 - 每3个字节的数据编码为4个ASCII字符。



要可靠地发送文本,您可以第一使用您选择的文本编码(例如UTF-8)然后之后编码为字节。Base64将生成的二进制数据编码为文本字符串,可以安全发送编码为ASCII。接收器将必须逆转此过程以恢复原始消息。这当然要求接收者知道使用了哪些编码,并且这些信息通常需要单独发送。



历史上,它已被用于对电子邮件中的二进制数据进行编码其中电子邮件服务器可以修改行结尾。更现代的例子是使用Base64编码来。这里需要对数据进行编码,以避免诸如'<'和'>'之类的字符被解释为标签。






这是一个工作示例:



我想发送一条两行的短信

 
Hello
世界!

如果我以ASCII(或UTF-8)格式发送它,它将会是这样:

  72 101 108 108 111 10 119 111 114 108 100 33 

字节10在某些系统中已损坏,因此我们可以将这些字节编码为64个Base64字符串:

 SGVsbG8sCndvcmxkIQ == 

当使用ASCII编码时,其格式如下:

  86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61 

是已知的安全字节,因此任何系统损坏此消息的机会很小。我可以发送此邮件,而不是我的原始邮件,让接收者撤消该过程以恢复原始邮件。


Wikipedia says

But is it not that data is always stored/transmitted in binary because the memory that our machines have store binary and it just depends how you interpret it? So, whether you encode the bit pattern 010011010110000101101110 as Man in ASCII or as TWFu in Base64, you are eventually going to store the same bit pattern.

If the ultimate encoding is in terms of zeros and ones and every machine and media can deal with them, how does it matter if the data is represented as ASCII or Base64?

What does it mean "media that are designed to deal with textual data"? They can deal with binary => they can deal with anything.


Thanks everyone, I think I understand now.

When we send over data, we cannot be sure that the data would be interpreted in the same format as we intended it to be. So, we send over data coded in some format (like Base64) that both parties understand. That way even if sender and receiver interpret same things differently, but because they agree on the coded format, the data will not get interpreted wrongly.

From Mark Byers example

If I want to send

Hello
world!

One way is to send it in ASCII like

72 101 108 108 111 10 119 111 114 108 100 33

But byte 10 might not be interpreted correctly as a newline at the other end. So, we use a subset of ASCII to encode it like this

83 71 86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61

which at the cost of more data transferred for the same amount of information ensures that the receiver can decode the data in the intended way, even if the receiver happens to have different interpretations for the rest of the character set.

解决方案

Your first mistake is thinking that ASCII encoding and Base64 encoding are interchangeable. They are not. They are used for different purposes.

  • When you encode text in ASCII, you start with a text string and convert it to a sequence of bytes.
  • When you encode data in Base64, you start with a sequence of bytes and convert it to a text string.

To understand why Base64 was necessary in the first place we need a little history of computing.


Computers communicate in binary - 0s and 1s - but people typically want to communicate with more rich forms data such as text or images. In order to transfer this data between computers it first has to be encoded into 0s and 1s, sent, then decoded again. To take text as an example - there are many different ways to perform this encoding. It would be much simpler if we could all agree on a single encoding, but sadly this is not the case.

Originally a lot of different encodings were created (e.g. Baudot code) which used a different number of bits per character until eventually ASCII became a standard with 7 bits per character. However most computers store binary data in bytes consisting of 8 bits each so ASCII is unsuitable for tranferring this type of data. Some systems would even wipe the most significant bit. Furthermore the difference in line ending encodings across systems mean that the ASCII character 10 and 13 were also sometimes modified.

To solve these problems Base64 encoding was introduced. This allows you to encode aribtrary bytes to bytes which are known to be safe to send without getting corrupted (ASCII alphanumeric characters and a couple of symbols). The disadvantage is that encoding the message using Base64 increases its length - every 3 bytes of data is encoded to 4 ASCII characters.

To send text reliably you can first encode to bytes using a text encoding of your choice (for example UTF-8) and then afterwards Base64 encode the resulting binary data into a text string that is safe to send encoded as ASCII. The receiver will have to reverse this process to recover the original message. This of course requires that the receiver knows which encodings were used, and this information often needs to be sent separately.

Historically it has been used to encode binary data in email messages where the email server might modify line-endings. A more modern example is the use of Base64 encoding to embed image data directly in HTML source code. Here it is necessary to encode the data to avoid characters like '<' and '>' being interpreted as tags.


Here is a worked example:

I wish to send a text message with two lines

Hello
world!

If I send it as ASCII (or UTF-8) it will look like this:

72 101 108 108 111 10 119 111 114 108 100 33

The byte 10 is corrupted in some systems so we can base 64 encode these bytes as a Base64 string:

SGVsbG8sCndvcmxkIQ==

Which when encoded using ASCII looks like this:

83 71 86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61

All the bytes here are known safe bytes, so there is very little chance that any system will corrupt this message. I can send this instead of my original message and let the receiver reverse the process to recover the original message.

这篇关于为什么我们使用Base64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:37