我经常听到人们谈论“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

其他回答

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

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

我要在这里描述的Base64的用法有点hack。所以如果你不喜欢黑客,请不要继续。

当我发现MySQL的utf8不支持4字节unicode字符时,我遇到了麻烦,因为它使用了3字节版本的utf8。那么我做了什么来支持完整的4字节unicode MySQL的utf8?base64在存储到数据库时编码字符串,在检索时解码字符串。

由于base64编码和解码非常快,上面的工作非常完美。

你需要注意以下几点:

Base64编码多使用33%的存储空间 存储在数据库中的字符串不是人类可读的(您可以将其作为数据库字符串使用基本加密形式的特性出售)。

对于任何不支持unicode的存储引擎,都可以使用上述方法。

一个十六进制数字是一个啃位(4位)。两个小字节构成8位,也称为1字节。

MD5生成128位输出,该输出使用32个十六进制数字序列表示,这些十六进制数字依次为32*4=128位。128位等于16字节(因为1字节等于8位)。

每个Base64字符编码6位(除了最后一个非填充字符可以编码2,4或6位);以及最后的填充字符(如果有的话)。因此,根据Base64编码,128位哈希至少需要⌈128/6⌉= 22个字符,如果有的话再加上pad。

使用base64,我们可以生成所需长度(6、8或10)的编码输出。 如果我们选择8字符长的输出,它只占用8个字节,而128位哈希输出则占用16个字节。

因此,除了安全性之外,还使用base64编码来减少空间消耗。

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

当我们通过web服务传输大型二进制对象(图像)时,我在实际意义上使用它。因此,当我使用python脚本测试c# web服务时,可以使用一点魔法重新创建二进制对象。

(在python中)

import base64
imageAsBytes = base64.b64decode( dataFromWS )