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


当前回答

Base64是一个二进制到文本的编码方案,以ASCII字符串格式表示二进制数据。它被设计用来在网络通道中传输二进制格式的数据。

Base64机制使用64个字符进行编码。这些字符包括:

10个数值:即0,1,2,3,…,9 大写字母:即A,B,C,D,…,Z 小写字母:即a,b,c,d,…,z 2个特殊字符(这些字符取决于操作系统):即+,/

base64如何工作

使用base64算法编码字符串的步骤如下:

计算字符串中的字符数。如果它不是3的倍数,那么用特殊字符(即=)填充它,使它是3的倍数。 使用ASCII表将字符串转换为ASCII二进制格式8位。 转换为二进制格式后,将二进制数据分成6位的块。 将6位二进制数据块转换为十进制数。 根据base64索引表将小数转换为字符串。这个表可以是一个例子,但正如我所说,2个特殊字符可能会有所不同。

现在,我们得到了输入字符串的编码版本。

让我们举个例子:将字符串THS转换为base64编码字符串。

计算字符数:它已经是3的倍数。 转换为ASCII二进制格式8位。我们得到(T)01010100 (H)01001000 (S)01010011 将二进制数据分成6位的块。我们收到010101 000100 100001 010011 将6位二进制数据块转换为十进制数。得到21 4 33 19 根据base64索引表将小数转换为字符串。我们有VEhT

其他回答

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.

这是一种二进制数据的文本编码,其结果文本除了字母、数字和符号“+”、“/”和“=”之外什么都没有。这是一种在专门用于文本数据的媒体上存储/传输二进制数据的方便方法。

但为什么是Base-64?将二进制数据转换为文本的两种替代方法是:

Decimal:将每个字节的十进制值存储为三个数字:045 112 101 037等,其中每个字节由3个字节表示。数据膨胀了三倍。 十六进制:将字节存储为十六进制对:AC 47 0D 1A等,其中每个字节由2个字节表示。数据膨胀了两倍。

Base-64在4个字符中映射3个字节(8 x 3 = 24位),该字符横跨6位(6 x 4 = 24位)。结果看起来像“TWFuIGlzIGRpc3Rpb…”。因此膨胀仅仅是原来的4/3 = 1.3333333倍。

当您有一些想要通过网络传送的二进制数据时,通常不会仅仅以原始格式在网络上传输比特和字节。为什么?因为有些媒体是为文本流而设计的。您永远不会知道——有些协议可能会将二进制数据解释为控制字符(如调制解调器),或者您的二进制数据可能会被搞糟,因为底层协议可能认为您输入了一个特殊的字符组合(如FTP如何转换行结束符)。

为了解决这个问题,人们把二进制数据编码成字符。Base64就是这些编码类型中的一种。

为什么是64年? 因为你通常可以依赖于相同的64个字符出现在许多字符集中,你可以合理地相信你的数据最终会在线路的另一端没有损坏。

从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.

除了已经说过的,还有两种没有列出的非常常见的用法是

散列:

哈希是将一个字节块转换为另一个固定大小的字节块的单向函数,例如128bit或256bit (SHA/MD5)。将结果字节转换为Base64可以更容易地显示散列,特别是在比较完整性的校验和时。在Base64中经常看到哈希值,以至于许多人将Base64本身误认为是哈希值。

密码:

由于加密密钥不一定是文本,而是原始字节,因此有时需要将其存储在文件或数据库中,这在Base64中很方便。结果加密字节也是如此。

注意,尽管Base64经常用于密码学中,但它并不是一种安全机制。任何人都可以将Base64字符串转换回其原始字节,因此不应将其用作保护数据的手段,而应将其用作更容易显示或存储原始字节的格式。

证书

PEM格式的x509证书是base64编码的。http://how2ssl.com/articles/working_with_pem_files/