Decode data in Base64

I am working with metadata from an image extracted in Json format. One such data is a binary string base64 . I read in a documentation that these values are 16-bit . Is there any way to decode them and send them to an array automatically? The solutions I tried are all for 8-bit values.

Author: jsbueno, 2017-11-05

2 answers

To 'decode' a data represented in base64 you can use the function b64decode() of the module base64 as follows:

import base64
b64 = 'TyByYXRvIHJvZXUgYSByb3VwYSBkbyByZWkgZGUgUm9tYQ=='
print base64.b64decode(b64)

Output:

O rato roeu a roupa do rei de Roma
 2
Author: Lacobus, 2017-11-05 20:25:50

About Base64 has nothing to do with "16 bits". As the name implies, your message is broken into pieces representable by 64 characters, i.e.: 6 bits . Using this subset of the total table of 256 characters (8 bits) ensures that the data is all represented by visible and printable characters. This ensures that any content can be, for example, copied and pasted into user applications: cryptographic key pairs use this representation feature, for example.

Also ensures that any content will always be transmitted in full, even by internet protocols that only transmit plain text.. This was the case with early versions of email standards, for example. Until today, when you attach an image in an email, internally it will be attached as base64 or some variant (base85, for example). In your email inbox there may be an option to "view original message", or "raw" - select this and see how images, of type signatures comes encoded as base64.

In Python, to decode a snippet of data in base64 and the like there is a ready module in the standard library that already does everything, called "base64". Just call the function base64.b64decode by passing its encoded text, and it will have the original content in the form of a "bytes"object. Simply save this object as a binary file on disk to have the image file:

dados = {"imagem": "iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAAAAADhOQgPAAAAI0lEQVQI1yXGsQ0AMBDEIO7339kp0iDWiJMMSa6Fr3WYtn4ev0gPAtGQ50YAAAAASUVORK5CYII="}
import base64
imagem = base64.b64decode(dados["imagem"])
with open("image.png", "wb") as img_file:
    img_file.write(imagem)

To have 64 symbols, the format uses the letters from A to Z uppercase and lowercase (52 characters), plus the digits from 0 to 9 (64) and the symbols + and/, totaling 64 digits. The sign of " = "is still used to mark numbers" 0 " that do not enter the decoded data, when the number of original bytes is not divisible by 3. Thus, every 3 8-bit bytes of the original message are converted to 4 6-bit digits. Line breaks are ignored, allowing the data to be formatted so that it can be printed one way "beautiful".

 2
Author: jsbueno, 2017-11-06 00:38:04