Home > Backend Development > Python Tutorial > Base64 encoding and decoding using Python

Base64 encoding and decoding using Python

WBOY
Release: 2023-09-02 13:49:05
Original
1234 people have browsed it

Suppose you have a binary image file that you want to transfer over the network. You're surprised that the other party didn't receive the file correctly - the file just contains weird characters!

Hmm, it looks like you're trying to send the file in raw bits and bytes format, while the media you're using is designed for streaming text.

What is the solution to avoid such problems? The answer is Base64 encoding. In this article, I will show you how to encode and decode binary images using Python. The program is explained as a standalone local program, but you can apply the concept to different applications, such as sending encoded images from a mobile device to a server and many other applications.

What is Base64?

Before we dive into this article, let us first define what Base64 means.

Base64 is a method of encoding 8-bit binary data into a format that can be represented in 6 bits. Only the characters A-Z, a-z, 0-9, , / are used to represent data, where = is used to fill in data. For example, using this encoding, three 8-bit bytes are converted to four 6-bit groups.

The term Base64 is taken from the Multipurpose Internet Mail Extensions (MIME) standard, which is widely used in HTTP and XML and was originally developed for encoding email attachments for transmission.

Why do we use Base64?

Base64 is important for binary data representation, so it allows binary data to be represented in a way that looks and acts as plain text, which makes it more reliable to store in a database, send in an email, or use in other applications. Text-based formats such as XML. Base64 is primarily used to represent data in ASCII string format.

As mentioned in the introduction to this article, without Base64, sometimes the data will not be read at all.

Base64 encoding

Base64 encoding is the process of converting binary data into a limited set of 64 characters. As shown in the first section, these characters are A-Z, a-z, 0-9, , and / (Counting, did you notice that they add up to 64?). This character set is considered the most common and is known as Base64 for MIME. It uses A-Z, a-z and 0-9 for the first 62 values, and and / for The last two values.

Base64 encoded data will end up being longer than the original data, so as mentioned above, for every 3 bytes of binary data, there are at least 4 bytes of Base64 encoded data. This is because we compress the data into a smaller character set.

Have you seen a portion of the original email file shown below (most likely derived from an unsent email)? If so, then you've seen Base64 encoding in action! (If you notice = at the end, you can tell this is Base64 encoding because the equal sign is used for padding during encoding.)

Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

2KfZhNiz2YTYp9mFINi52YTZitmD2YUg2YjYsdit2YXYqSDYp9mE2YTZhyDZiNio2LHZg9in2KrZ
h9iMDQoNCtij2YjYryDZgdmC2Lcg2KfZhNin2LPYqtmB2LPYp9ixINi52YYg2KfZhNmF2YLYsdix
2KfYqiDYp9mE2K/Ysdin2LPZitipINin2YTYqtmKINiq2YbYtdit2YjZhiDYqNmH2Kcg2YTZhdmG
INmK2LHZitivINin2YTYqtmI2LPYuSDZgdmKDQrYt9mE2Kgg2KfZhNi52YTZhSDYp9mE2LTYsdi5
2YrYjCDYudmE2YXYpyDYqNij2YbZiiDYutmK2LEg2YXYqtiu2LXYtSDYqNin2YTYudmE2YUg2KfZ
hNi02LHYudmKINmI2KPZgdiq2YLYryDZhNmE2YXZhtmH2Kwg2KfZhNi52YTZhdmKDQrZhNiw2YTZ
gy4NCg0K2KzYstin2YPZhSDYp9mE2YTZhyDYrtmK2LHYpyDYudmE2Ykg2YbYtdit2YPZhSDZgdmK
INmH2LDYpyDYp9mE2LTYo9mGLg0KDQrYudio2K/Yp9mE2LHYrdmF2YYNCg==
--089e0141aa264e929a0514593016
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64
Copy after login

Base64 is performed in multiple steps, as follows:

  • The text to be encoded is converted to its respective decimal value, i.e. to the corresponding ASCII value (i.e. a:97, b:98, etc.). This is the ASCII table.
  • Convert the decimal value obtained in the above step to its equivalent binary value (i.e. 97: 01100001).
  • Concatenate all binary equivalents to obtain a large set of binary numbers.
  • A large set of binary numbers divided into equal parts, each containing only 6 bits.
  • Equal 6-bit groups are converted to their decimal equivalents.
  • Finally, the decimal equivalent is converted to its Base64 value (i.e. 4: E). Below are the decimal values ​​and their Base64 alphabet.

Base64 decoding

Base64 decoding is the opposite of Base64 encoding. In other words, it is performed by reversing the steps described in the previous section.

So the steps of Base64 decoding can be described as follows:

  • Each character in the string is changed to its Base64 decimal value.
  • The obtained decimal value will be converted to its equivalent binary value.
  • Truncate the first two digits of the binary number from each binary number obtained and combine the set of 6 bits together to form a large string of binary digits.
  • Divide the large string of binary numbers obtained in the previous step into groups of 8 digits.
  • 8-digit binary numbers are converted to their decimal equivalents.
  • Finally, convert the obtained decimal value to the corresponding ASCII value.

Base64 encoding and decoding of strings

Once you understand what's going on behind the scenes, it will be easier to understand how it all works. Let's try encoding and decoding a simple three letter word, Hey.

We first convert each letter of the word to its ASCII equivalent, and then convert the ASCII equivalent to binary. This gives us the following values:

ASCII 索引值 8 位二进制值
H 72 01001000
e 101 01100101
y 121 01111001

换句话说,我们可以像这样以二进制形式编写 Hey

01001000 01100101 01111001
Copy after login

总共 24 位,当转换为 6 位组时,每个位产生四个值:

010010 000110 010101 111001
Copy after login

在 Base64 表中,字符 AZ 由值 025 表示。字符 az 由值 2651 表示。数字 09 由值 5261 表示。字符 +/6263 表示。字符 = 用于在无法将位正确分为 6 组时进行填充。

我们现在将重新排列的位转换为数值,然后获取代表这些数值的字符。

6 位二进制值 Base64 索引值
010010 18
000110 6 G
010101 21 V
111001 57 5

根据我们上面的计算,字母 Hey 在 Base64 编码时将变成 SGV5。我们可以使用以下代码测试这是否正确:

from base64 import b64encode

text_binary = b'Hey'

# SGV5
print(b64encode(text_binary))
Copy after login

整个过程反向完成,在Base64解码后得到我们的原始数据。

现在,我将快速向您展示另一个单词 Heyo 的编码,以解释编码字符串中 = 的出现。

ASCII 索引值 8 位二进制值
H 72 01001000
e 101 01100101
y 121 01111001
o 111 01101111

一共有32位。这将为我们提供五个不同的 6 位组,其中有两个剩余位:11。我们用 0000 填充它们以获得 6 位组。根据上述排列将 6 位组成一组将得到以下结果:

010010 000110 010101 111001 011011 110000
Copy after login

重新排列的位将根据 Base64 索引值返回以下字符。

6 位二进制值 Base64 索引值
010010 18
000110 6 G
010101 21 V
111001 57 5
011011 27 b
110000 48 w

这意味着 Heyo 的 Base64 编码值为 SGV5bw==。每个 = 代表一对 00,我们添加它们用于填充原始位序列。

from base64 import b64encode

text_binary = b'Heyo'

# SGV5bw==
print(b64encode(text_binary))
Copy after login

对图像进行 Base64 编码

现在让我们开始讨论本文的重点。在本节中,我将向您展示如何使用 Python 轻松地对图像进行 Base64 编码。

我将使用以下二进制图像。继续下载它,让我们开始使用 Python! (我假设图像的名称是 deer.gif。)

Base64 encoding and decoding using Python

为了在Python中使用Base64,我们要做的第一件事就是导入base64模块:

导入base64

为了对图像进行编码,我们只需使用函数 base64.b64encode(s) 即可。 Python对该函数的描述如下:

使用 Base64 对类似字节的对象 s 进行编码并返回编码后的字节。

因此,我们可以执行以下操作来对图像进行 Base64 编码:

import base64 
image = open('deer.gif', 'rb') #open binary file in read mode
image_read = image.read()
image_64_encode = base64.b64encode(image_read)
Copy after login

如果您想查看编码过程的输出,请键入以下内容:

打印 image_64_encode

Base64 解码图像

要使用 Python 解码图像,我们只需使用 base64.b64decode(s) 函数。 Python 提及了有关此函数的以下内容:

解码 Base64 编码的类似字节的对象或 ASCII 字符串并返回解码后的字节。

因此,为了解码我们在上一节中编码的图像,我们执行以下操作:

base64.decode(image_64_encode)

把它们放在一起

让我们将用于 Base64 编码和解码图像的程序放在一起。执行此操作的 Python 脚本应如下所示:

import base64
image = open('deer.gif', 'rb')
image_read = image.read()
image_64_encode = base64.b64encode(image_read)
image_64_decode = base64.b64decode(image_64_encode) 
image_result = open('deer_decode.gif', 'wb') # create a writable image and write the decoding result
image_result.write(image_64_decode)
Copy after login

如果您打开桌面上的 deer_decode.gif,您会发现您拥有我们在第一步中编码的原始图像 deer.gif

正如我们从本文中看到的,Python 使执行看似复杂的任务变得非常容易。

URL 安全编码和解码

正如我在本教程前面提到的,除了常规字母数字值之外,Base64 编码还使用字符 +/ 。但是,这些字符在 URL 中具有特殊含义。这意味着使用这些字符的 Base64 编码值如果在 URL 内部使用,可能会导致意外行为。

此问题的一种解决方案是使用 urlsafe_base64encode()urlsafe_base64decode() 函数对任何数据进行编码和解码。这些函数在编码过程中将 + 替换为 -,将 / 替换为 _

下面是一个 Python 示例,显示了这种差异:

import base64

image = open('dot.jpg', 'rb')
image_data = image.read()

unsafe_encode = base64.b64encode(image_data)
safe_encode = base64.urlsafe_b64encode(image_data)

# b'/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNr....
print(unsafe_encode)

# b'_9j_4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP_sABFEdWNr....
print(safe_encode)
Copy after login

学习Python

无论您是刚刚入门还是希望学习新技能的经验丰富的程序员,都可以通过我们完整的 Python 教程指南学习 Python。

这篇文章已根据 Nitish Kumar 的贡献进行了更新。 Nitish 是一名 Web 开发人员,拥有在各种平台上创建电子商务网站的经验。他将业余时间花在个人项目上,让他的日常生活变得更轻松,或者在晚上与朋友一起散步。

The above is the detailed content of Base64 encoding and decoding using Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template