Decode data in Base64

1

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

    
asked by anonymous 05.11.2017 / 19:59

2 answers

2

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

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

Output:

O rato roeu a roupa do rei de Roma
    
05.11.2017 / 21:14
2

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

It also ensures that any content is always transmitted in its entirety, even by Internet protocols that only transmit pure text. This was the case for the first versions of email standards, for example. To date, when attaching an image in an email, internally it is attached as base64 or some variant (base85, for example). In your email inbox there may be an option to "see original message", or "raw" - select this and see how signature-type images are encoded as base64.

In Python, to decode a piece of data in base64 and similar there is a ready module in the default library that already does everything, called "base64". Just call the base64.b64decode function by passing its encoded text, and it will have the original content in the form of a "bytes" object. Just 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)

For 64 symbols, the format uses the letters A through Z (52 characters), plus the digits 0 through 9 (64) and the + and / symbols, totaling 64 digits. The "=" signal is still used to dial "0" numbers that do not enter the decoded data, when the number of original bytes is not divisible by 3. Thus every 3 bytes of 8 bits of the original message are converted to 4 digits of 6 bits. Line breaks are ignored, allowing data to be formatted so it can be printed in a "cute" way.

    
06.11.2017 / 00:56