我经常听到人们谈论“64进制编码”。它的用途是什么?


当前回答

base64是一个二进制到文本的编码方案,以ASCII字符串格式表示二进制数据。Base64被设计用来跨通道传输二进制格式的数据。它接受任何形式的数据并将其转换为纯文本的长字符串。以前我们不能传输大量的数据,如文件,因为它是由2⁸比特字节组成的,但我们的实际网络使用2⁷比特字节。这就是base64编码出现的原因。但是base64到底是什么意思呢?

让我们来理解base64的含义。

base64 = base+64

我们可以调用base64作为基数64的表示。Base64仅使用6位(2 = 64个字符)来确保可打印的数据是人类可读的。但是,如何?我们也可以写base65或base78,但为什么只写64呢?让我们证明一下。 Base64编码包含64个字符来编码任何字符串。 base64包含:

10数值即,0,1,2,3,.....9。

26大写字母,即A,B,C,D,.......Z。

26个小写字母,即a,b,c,d,........z。

两个特殊字符,即+,/。取决于你的操作系统。

base64算法遵循的步骤如下:

count the number of characters in a String. If it is not multiple of 3 pad with special character i.e., = to make it multiple of 3. Encode the string in ASCII format. Now, it will convert the ASCII to binary format 8-bit each. After converting to binary format, it will divide binary data into chunks of 6-bits each. The chunks of 6-bit binary data will now be converted to decimal number format. Using the base64 Index Table, the decimals will be again converted to a string according to the table format. Finally, we will get the encoded version of our input string.

其他回答

它用于将任意二进制数据转换为ASCII文本。

例如,电子邮件附件就是通过这种方式发送的。

几年前,当邮件功能被引入时,它完全是基于文本的,随着时间的推移,对图像和媒体(音频、视频等)等附件的需求出现了。当这些附件通过互联网发送时(基本上是以二进制数据的形式),原始形式的二进制数据损坏的概率很高。因此,为了解决这个问题,BASE64出现了。

二进制数据的问题是它包含null字符,在一些语言中,如C, c++表示字符串的结束,因此以包含null字节的原始形式发送二进制数据将阻止文件被完全读取并导致损坏的数据。

例如:

在C和c++中,这个“null”字符表示字符串的结束。所以"HELLO"是这样存储的:

H e l l o

72 69 76 76 79 00

00表示“停在这里”。

现在让我们深入研究BASE64编码是如何工作的。

注意:字符串的长度应该是3的倍数。

例1:

要编码的字符串:" ace ",长度=3

将每个字符转换为十进制。

A = 97, c= 99, e= 101

将每个小数改为8位二进制表示。

97= 01100001, 99= 01100011, 101= 01100101

合并:01100001 01100011 01100101

在一组6位中分离。

011000 010110 001101 100101

从二进制到十进制计算

011000= 24, 010110= 22, 001101= 13, 100101= 37

使用base64 chart将十进制字符转换为base64。

24= Y, 22= W, 13= N, 37= l

“ace”=>“YWNl”

例2:

要编码的字符串:" abcd "长度=4,不是3的倍数。因此,要使字符串长度为3的倍数,我们必须添加2位填充使length= 6。填充位用“=”符号表示。

需要注意的是:一个填充位等于两个000,所以两个填充位等于四个0 0000。

所以让我们开始这个过程:-

将每个字符转换为十进制。

A = 97, b= 98, c= 99, d= 100

将每个小数改为8位二进制表示。

97= 01100001, 98= 01100010, 99= 01100011, 100= 01100100

在一组6位中分离。

011000, 010110, 001001, 100011, 011001, 00

所以最后6位是不完整的,所以我们插入两个填充位,等于4个零“0000”。

011000, 010110, 001001, 100011, 011001, 000000 ==

现在,它是相等的。末尾的两个等号表示添加了4个零(有助于解码)。

将二进制计算为十进制。

011000= 24, 010110= 22, 001001= 9, 100011= 35, 011001= 25, 000000=0 ==

使用base64 chart将十进制字符转换为base64。

24= Y, 22= W, 9= j, 35= j, 25= Z, 0= A ==

“abcd”=>“YWJjZA==”

Base-64编码是一种获取二进制数据并将其转换为文本的方法,这样就更容易在电子邮件和HTML表单数据中传输。

http://en.wikipedia.org/wiki/Base64

在计算机的早期,当电话线系统间的通信不是特别可靠时,一种快速而肮脏的验证数据完整性的方法被使用:“位奇偶校验”。在这种方法中,传输的每个字节都有7位数据,第8位将是1或0,以强制字节中1位的总数为偶数。

因此,0x01将作为0x81传输;0x02将是0x82;0x03仍然是0x03等等。

为了进一步完善这个系统,当定义ASCII字符集时,只有00-7F被分配字符。(直到今天,所有设置在80-FF范围内的字符都是非标准的)

当时的许多路由器都把奇偶校验和字节转换放在硬件中,迫使连接到它们的计算机严格处理7位数据。这迫使电子邮件附件(以及所有其他数据,这就是为什么HTTP和SMTP协议是基于文本的)转换为纯文本格式。

这些路由器很少能活到90年代。我非常怀疑它们中任何一个现在还在使用。

从http://en.wikipedia.org/wiki/Base64

The term Base64 refers to a specific MIME content transfer encoding. It is also used as a generic term for any similar encoding scheme that encodes binary data by treating it numerically and translating it into a base 64 representation. The particular choice of base is due to the history of character set encoding: one can choose a set of 64 characters that is both part of the subset common to most encodings, and also printable. This combination leaves the data unlikely to be modified in transit through systems, such as email, which were traditionally not 8-bit clean. Base64 can be used in a variety of contexts: Evolution and Thunderbird use Base64 to obfuscate e-mail passwords[1] Base64 can be used to transmit and store text that might otherwise cause delimiter collision Base64 is often used as a quick but insecure shortcut to obscure secrets without incurring the overhead of cryptographic key management Spammers use Base64 to evade basic anti-spamming tools, which often do not decode Base64 and therefore cannot detect keywords in encoded messages. Base64 is used to encode character strings in LDIF files Base64 is sometimes used to embed binary data in an XML file, using a syntax similar to ...... e.g. Firefox's bookmarks.html. Base64 is also used when communicating with government Fiscal Signature printing devices (usually, over serial or parallel ports) to minimize the delay when transferring receipt characters for signing. Base64 is used to encode binary files such as images within scripts, to avoid depending on external files. Can be used to embed raw image data into a CSS property such as background-image.