What is base64 encoding used for?

后端 未结 18 739
滥情空心
滥情空心 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 10:13

    When you have some binary data that you want to ship across a network, you generally don't do it by just streaming the bits and bytes over the wire in a raw format. Why? because some media are made for streaming text. You never know -- some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you've entered a special character combination (like how FTP translates line endings).

    So to get around this, people encode the binary data into characters. Base64 is one of these types of encodings.

    Why 64?
    Because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data's going to end up on the other side of the wire uncorrupted.

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

    The usage of Base64 I'm going to describe here is somewhat a hack. So if you don't like hacks, please do not go on.

    I went into trouble when I discovered that MySQL's utf8 does not support 4-byte unicode characters since it uses a 3-byte version of utf8. So what I did to support full 4-byte unicode over MySQL's utf8? Well, base64 encode strings when storing into the database and base64 decode when retrieving.

    Since base64 encoding and decoding is very fast, the above worked perfectly.

    You have the following points to take note of:

    • Base64 encoding uses 33% more storage

    • Strings stored in the database wont be human readable (You could sell that as a feature that database strings use a basic form of encryption).

    You could use the above method for any storage engine that does not support unicode.

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

    Years ago, when mailing functionality was introduced, so that was utterly text based, as the time passed, need for attachments like image and media (audio,video etc) came into existence. When these attachments are sent over internet (which is basically in the form of binary data), the probability of binary data getting corrupt is high in its raw form. So, to tackle this problem BASE64 came along.

    The problem with binary data is that it contains null characters which in some languages like C,C++ represent end of character string so sending binary data in raw form containing NULL bytes will stop a file from being fully read and lead in a corrupt data.

    For Example :

    In C and C++, this "null" character shows the end of a string. So "HELLO" is stored like this:

    H E L L O

    72 69 76 76 79 00

    The 00 says "stop here".

    Now let’s dive into how BASE64 encoding works.

    Point to be noted : Length of the string should be in multiple of 3.

    Example 1 :

    String to be encoded : “ace”, Length=3

    1) Convert each character to decimal.

    a= 97, c= 99, e= 101

    2) Change each decimal to 8-bit binary representation.

    97= 01100001, 99= 01100011, 101= 01100101

    Combined : 01100001 01100011 01100101

    3) Seperate in a group of 6-bit.

    011000 010110 001101 100101

    4) Calculate binary to decimal

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

    5) Covert decimal characters to base64 using base64 chart.

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

    “ace” => “YWNl”

    Example 2 :

    String to be encoded : “abcd” Length=4, it's not multiple of 3. So to make string length multiple of 3 , we must add 2 bit padding to make length= 6. Padding bit is represented by “=” sign.

    Point to be noted : One padding bit equals two zeroes 00 so two padding bit equals four zeroes 0000.

    So lets start the process :–

    1) Convert each character to decimal.

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

    2) Change each decimal to 8-bit binary representation.

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

    3) Separate in a group of 6-bit.

    011000, 010110, 001001, 100011, 011001, 00

    so the last 6-bit is not complete so we insert two padding bit which equals four zeroes “0000”.

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

    Now, it is equal. Two equals sign at the end show that 4 zeroes were added (helps in decoding).

    4) Calculate binary to decimal.

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

    5) Covert decimal characters to base64 using base64 chart.

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

    “abcd” => “YWJjZA==”

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

    “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”(Wiki, 2017)

    Example could be the following: you have a web service that accept only ASCII chars. You want to save and then transfer user’s data to some other location (API) but recipient want receive untouched data. Base64 is for that. . . The only downside is that base64 encoding will require around 33% more space than regular strings.

    Another Example:: uenc = url encoded = aHR0cDovL2xvYy5tYWdlbnRvLmNvbS9hc2ljcy1tZW4tcy1nZWwta2F5YW5vLXhpaS5odG1s = http://loc.querytip.com/asics-men-s-gel-kayano-xii.html.

    As you can see we can’t put char “/” in URL if we want to send last visited URL as parameter because we would break attribute/value rule for “MOD rewrite” – GET parameter.

    A full example would be: “http://loc.querytip.com/checkout/cart/add/uenc/http://loc.magento.com/asics-men-s-gel-kayano-xii.html/product/93/”

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

    Some transportation protocols only allow alphanumerical characters to be transmitted. Just imagine a situation where control characters are used to trigger special actions and/or that only supports a limited bit width per character. Base64 transforms any input into an encoding that only uses alphanumeric characters, +, / and the = as a padding character.

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

    I use it in a practical sense when we transfer large binary objects (images) via web services. So when I am testing a C# web service using a python script, the binary object can be recreated with a little magic.

    [In python]

    import base64
    imageAsBytes = base64.b64decode( dataFromWS )
    
    0 讨论(0)
提交回复
热议问题