Encoding and Decoding Base64 Strings in Python
In this post, we will learn Base64 encoding and decoding for strings in Python. In Base64 encoding, we convert the given bytes into ASCII characters. Each Base64 character is 6-bit long. The available characters in Base64 encoding are given below:
- All uppercase letters
- All lowercase letters
- 0-9 digits
- +
- /
How to convert a string into a Base64 string?
We can easily convert a given string into a Base64 string by following the below steps for every character:
- Get the ASCII value of the character.
- Get the 8-bit binary equivalent of obtained ASCII value.
- Regroup the digits to convert 8-bit character chunk into 6-bit character chunk.
- Now, convert the 6-bit character chunks to decimal value.
We can use the below Base64 encoding table to align the values.
Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 000000 | A | 16 | 010000 | Q | 32 | 100000 | g | 48 | 110000 | w | |||
1 | 000001 | B | 17 | 010001 | R | 33 | 100001 | h | 49 | 110001 | x | |||
2 | 000010 | C | 18 | 010010 | S | 34 | 100010 | i | 50 | 110010 | y | |||
3 | 000011 | D | 19 | 010011 | T | 35 | 100011 | j | 51 | 110011 | z | |||
4 | 000100 | E | 20 | 010100 | U | 36 | 100100 | k | 52 | 110100 | 0 | |||
5 | 000101 | F | 21 | 010101 | V | 37 | 100101 | l | 53 | 110101 | 1 | |||
6 | 000110 | G | 22 | 010110 | W | 38 | 100110 | m | 54 | 110110 | 2 | |||
7 | 000111 | H | 23 | 010111 | X | 39 | 100111 | n | 55 | 110111 | 3 | |||
8 | 001000 | I | 24 | 011000 | Y | 40 | 101000 | o | 56 | 111000 | 4 | |||
9 | 001001 | J | 25 | 011001 | Z | 41 | 101001 | p | 57 | 111001 | 5 | |||
10 | 001010 | K | 26 | 011010 | a | 42 | 101010 | q | 58 | 111010 | 6 | |||
11 | 001011 | L | 27 | 011011 | b | 43 | 101011 | r | 59 | 111011 | 7 | |||
12 | 001100 | M | 28 | 011100 | c | 44 | 101100 | s | 60 | 111100 | 8 | |||
13 | 001101 | N | 29 | 011101 | d | 45 | 101101 | t | 61 | 111101 | 9 | |||
14 | 001110 | O | 30 | 011110 | e | 46 | 101110 | u | 62 | 111110 | + | |||
15 | 001111 | P | 31 | 011111 | f | 47 | 101111 | v | 63 | 111111 | / |
Next, we will see how we can encode strings to Base64 and decode Base64 strings using Python.
Base64 Encoding and Decoding in Python
Python provides us with a module named ‘base64’ to encode and decode strings.
Have a look at the Python program given below.
import base64 string = "Codespeedy is fun." string_bytes = string.encode('ascii') b64_bytes = base64.b64encode(string_bytes) b64_string = b64_bytes.decode('ascii') print("Base64 Encoded String: ", b64_string)
Output:
Base64 Encoded String: Q29kZXNwZWVkeSBpcyBmdW4u
In the above program, we have first converted the given string into byte-like objects. Next, we have encoded it into base64 encoding using the base64 module which gives us a byte-like object. We use the decode() method to get the encoded string from this byte-like object.
We can also decode a base64 string using the base64 module. See the below Python program.
import base64 b64_string = "Q29kZXNwZWVkeSBpcyBmdW4u" b64_bytes = b64_string.encode('ascii') string_bytes = base64.b64decode(b64_bytes) string = string_bytes.decode('ascii') print(string)
Output:
Codespeedy is fun.
Thank you.
Also read: How to encode a string in MD5 using Python
Leave a Reply