What is base64 encoding used for?

后端 未结 18 740
滥情空心
滥情空心 2020-11-22 09:45

I\'ve heard people talking about \"base 64 encoding\" here and there. What is it used for?

相关标签:
18条回答
  • 2020-11-22 09:59

    Base64

    Base64 is a generic term for a number of similar encoding schemes that encode binary data by treating it numerically and translating it into a base 64 representation. The Base64 term originates from a specific MIME content transfer encoding.

    Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport. Base64 is used commonly in a number of applications including email via MIME, and storing complex data in XML.

    0 讨论(0)
  • 2020-11-22 10:06

    It's a textual encoding of binary data where the resultant text has nothing but letters, numbers and the symbols "+", "/" and "=". It's a convenient way to store/transmit binary data over media that is specifically used for textual data.

    But why Base-64? The two alternatives for converting binary data into text that immediately spring to mind are:

    1. Decimal: store the decimal value of each byte as three numbers: 045 112 101 037 etc. where each byte is represented by 3 bytes. The data bloats three-fold.
    2. Hexadecimal: store the bytes as hex pairs: AC 47 0D 1A etc. where each byte is represented by 2 bytes. The data bloats two-fold.

    Base-64 maps 3 bytes (8 x 3 = 24 bits) in 4 characters that span 6-bits (6 x 4 = 24 bits). The result looks something like "TWFuIGlzIGRpc3Rpb...". Therefore the bloating is only a mere 4/3 = 1.3333333 times the original.

    0 讨论(0)
  • 2020-11-22 10:06

    It's used for converting arbitrary binary data to ASCII text.

    For example, e-mail attachments are sent this way.

    0 讨论(0)
  • 2020-11-22 10:06

    To expand a bit on what Brad is saying: many transport mechanisms for email and Usenet and other ways of moving data are not "8 bit clean", which means that characters outside the standard ascii character set might be mangled in transit - for instance, 0x0D might be seen as a carriage return, and turned into a carriage return and line feed. Base 64 maps all the binary characters into several standard ascii letters and numbers and punctuation so they won't be mangled this way.

    0 讨论(0)
  • 2020-11-22 10:09

    One hexadecimal digit is of one nibble (4 bits). Two nibbles make 8 bits which are also called 1 byte.

    MD5 generates a 128-bit output which is represented using a sequence of 32 hexadecimal digits, which in turn are 32*4=128 bits. 128 bits make 16 bytes (since 1 byte is 8 bits).

    Each Base64 character encodes 6 bits (except the last non-pad character which can encode 2, 4 or 6 bits; and final pad characters, if any). Therefore, per Base64 encoding, a 128-bit hash requires at least ⌈128/6⌉ = 22 characters, plus pad if any.

    Using base64, we can produce the encoded output of our desired length (6, 8, or 10). If we choose to decide 8 char long output, it occupies only 8 bytes whereas it was occupying 16 bytes for 128-bit hash output.

    So, in addition to security, base64 encoding is also used to reduce the space consumed.

    0 讨论(0)
  • 2020-11-22 10:10

    Aside from what's already been said, two very common uses that have not been listed are

    Hashes:

    Hashes are one-way functions that transform a block of bytes into another block of bytes of a fixed size such as 128bit or 256bit (SHA/MD5). Converting the resulting bytes into Base64 makes it much easier to display the hash especially when you are comparing a checksum for integrity. Hashes are so often seen in Base64 that many people mistake Base64 itself as a hash.

    Cryptography:

    Since an encryption key does not have to be text but raw bytes it is sometimes necessary to store it in a file or database, which Base64 comes in handy for. Same with the resulting encrypted bytes.

    Note that although Base64 is often used in cryptography is not a security mechanism. Anyone can convert the Base64 string back to its original bytes, so it should not be used as a means for protecting data, only as a format to display or store raw bytes more easily.

    Certificates

    x509 certificates in PEM format are base 64 encoded. http://how2ssl.com/articles/working_with_pem_files/

    0 讨论(0)
提交回复
热议问题