为什么base64编码的字符串的末尾有=符号

本文翻译自:Why does a base64 encoded string have an = sign at the end

I know what base64 encoding is and how to calculate base64 encoding in C#, however I have seen several times that when I convert a string into base64, there is an = at the end. 我知道什么是base64编码,以及如何在C#中计算base64编码,但是我已经看过好几次了,当我将字符串转换为base64时,最后会有一个=

A few questions came up: 提出了一些问题:

  1. Does a base64 string always end with = ? base64字符串是否总是以=结尾吗?
  2. Why does an = get appended at the end? 为什么在末尾添加=

#1楼

参考:https://stackoom.com/question/T1NN/为什么base-编码的字符串的末尾有-符号


#2楼

It's padding. 它是填充。 From http://en.wikipedia.org/wiki/Base64 : http://en.wikipedia.org/wiki/Base64

In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. 从理论上讲,不需要填充字符,因为可以从Base64位数计算出丢失的字节数。 In some implementations, the padding character is mandatory, while for others it is not used. 在某些实现中,填充字符是强制性的,而对于其他实现则不使用。 One case in which padding characters are required is concatenating multiple Base64 encoded files. 需要填充字符的一种情况是串联多个Base64编码的文件。


#3楼

  1. No. 没有。
  2. To pad the Base64-encoded string to a multiple of 4 characters in length, so that it can be decoded correctly. 将Base64编码的字符串的长度填充为4个字符的倍数,以便可以正确对其进行解码。

#4楼

http://www.hcidata.info/base64.htm http://www.hcidata.info/base64.htm

Encoding "Mary had" to Base 64 将“ Mary had”编码为Base 64

In this example we are using a simple text string ("Mary had") but the principle holds no matter what the data is (eg graphics file). 在此示例中,我们使用一个简单的文本字符串(“ Mary had”),但是无论数据是什么(例如图形文件),该原理都成立。 To convert each 24 bits of input data to 32 bits of output, Base 64 encoding splits the 24 bits into 4 chunks of 6 bits. 为了将输入数据的每24位转换为32位输出,Base 64编码将24位拆分为4个6位的块。 The first problem we notice is that "Mary had" is not a multiple of 3 bytes - it is 8 bytes long. 我们注意到的第一个问题是“ Mary have”不是3字节的倍数-它是8字节长。 Because of this, the last group of bits is only 4 bits long. 因此,最后一组位只有4位长。 To remedy this we add two extra bits of '0' and remember this fact by putting a '=' at the end. 为了解决这个问题,我们添加了两个额外的位“ 0”,并在末尾添加“ =”来记住这一事实。 If the text string to be converted to Base 64 was 7 bytes long, the last group would have had 2 bits. 如果要转换为Base 64的文本字符串长7个字节,则最后一组将有2位。 In this case we would have added four extra bits of '0' and remember this fact by putting '==' at the end. 在这种情况下,我们将添加四个额外的位“ 0”,并通过在最后放置“ ==”来记住这一事实。


#5楼

1-No 1-不

2- As a short answer : The 65th character ("=" sign) is used only as a complement in the final process of encoding a message. 2-简短的回答:第65个字符(“ =”符号)仅在消息编码的最终过程中用作补充。

You will not have a '=' sign if your string has a multiple of 3 characters number, because Base64 encoding takes each three bytes (8bits) and represents them as four printable characters in the ASCII standard. 如果您的字符串具有3个字符的倍数,则不会有'='符号,因为Base64编码每三个字节(8位)占用一个字节,并将它们表示为ASCII标准中的四个可打印字符。

Details : 细节 :

(a) If you want to encode (a)如果要编码

ABCDEFG <=> [ ABC ] [ DEF ] [ G ABCDEFG <=> [ ABC ] [ DEF ] [ G

Base64 will deal(producing 4 characters) with the first block and the second (as they are complete) but for the third it will add a double == in the output in order to complete the 4 needed characters.Thus, the result will be QUJD REVG Rw== (without space) Base64将处理(产生4个字符)第一个块和第二个块(因为它们完成了),但是对于第三个块,它将在输出中添加一个double ==以完成所需的4个字符,因此结果将是QUJD REVG Rw == (无空格)

(b) If you want to encode... (b)如果您想编码...

ABCDEFGH <=> [ ABC ] [ DEF ] [ GH ABCDEFGH <=> [ ABC ] [ DEF ] [ GH

Similarly, it will add just a single = in the end of the output to get 4 characters the result will be QUJD REVG R0g= (without space) 同样,它将在输出末尾仅添加一个=以获得4个字符,结果将为QUJD REVG R0g = (无空格)


#6楼

It serves as padding . 它用作填充

A more complete answer is that a base64 encoded string doesn't always end with a = , it will only end with one or two = if they are required to pad the string out to the proper length. 一个更完整的答案是,如果要求使用base64编码的字符串将字符串填充到适当的长度,则它并不总是=结尾,而是仅以一个或两个=结尾。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值