Decode data in Base64

Asked

Viewed 875 times

1

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

2 answers

2

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

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

Exit:

O rato roeu a roupa do rei de Roma

2

About Base64 has nothing to do with "16 bits". As the name implies, your message is broken into 64-character representative pieces, i.e.: 6 bits. Using this subset of the 256-character (8-bit) total table 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.

It also ensures that any content will always be transmitted in full, even by Internet protocols that only transmit pure text.. This was the case with the early versions of the email patterns, for example. Even today, when an image is attached in an email, it is attached internally 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, type signatures come encoded as Base64.

In Python, to decode a chunk of data in Base64 and similar there is a module ready in the standard library that already does everything, called "Base64". Just call the function base64.b64decode passing its coded text, and will have the original content in the form of an object "bytes". Just save this object as a binary file on disk to get 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 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 is still used to mark "0" numbers that do not enter the decoded data, when the number of original bytes is not divisible by 3. Thus, each 3 8-bit bytes of the original message is converted to 4 6-bit digits. Line breaks are ignored, allowing data to be formatted so that it can be printed in a "cute way".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.