The base64
module in Python provides functions for encoding binary data to a base64-encoded string and decoding a base64-encoded string back to binary data. Base64 is commonly used for encoding binary data to store and transfer over media that are designed to deal with text.
Table of Contents
- Introduction
- Key Functions
base64.b64encode
base64.b64decode
base64.standard_b64encode
base64.standard_b64decode
base64.urlsafe_b64encode
base64.urlsafe_b64decode
base64.b32encode
base64.b32decode
base64.b16encode
base64.b16decode
- Examples
- Basic Base64 Encoding and Decoding
- URL-Safe Base64 Encoding and Decoding
- Base32 Encoding and Decoding
- Base16 Encoding and Decoding
- Real-World Use Case
- Conclusion
- References
Introduction
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. The base64
module in Python provides functions to perform base64 encoding and decoding.
Key Functions
base64.b64encode
Encodes binary data to a base64-encoded string.
import base64
encoded = base64.b64encode(b'Hello, World!')
print(encoded)
Output:
b'SGVsbG8sIFdvcmxkIQ=='
base64.b64decode
Decodes a base64-encoded string back to binary data.
import base64
decoded = base64.b64decode(b'SGVsbG8sIFdvcmxkIQ==')
print(decoded)
Output:
b'Hello, World!'
base64.standard_b64encode
Encodes binary data to a standard base64-encoded string.
import base64
encoded = base64.standard_b64encode(b'Hello, World!')
print(encoded)
Output:
b'SGVsbG8sIFdvcmxkIQ=='
base64.standard_b64decode
Decodes a standard base64-encoded string back to binary data.
import base64
decoded = base64.standard_b64decode(b'SGVsbG8sIFdvcmxkIQ==')
print(decoded)
Output:
b'Hello, World!'
base64.urlsafe_b64encode
Encodes binary data to a URL-safe base64-encoded string.
import base64
encoded = base64.urlsafe_b64encode(b'Hello, World!')
print(encoded)
Output:
b'SGVsbG8sIFdvcmxkIQ=='
base64.urlsafe_b64decode
Decodes a URL-safe base64-encoded string back to binary data.
import base64
decoded = base64.urlsafe_b64decode(b'SGVsbG8sIFdvcmxkIQ==')
print(decoded)
Output:
b'Hello, World!'
base64.b32encode
Encodes binary data to a base32-encoded string.
import base64
encoded = base64.b32encode(b'Hello, World!')
print(encoded)
Output:
b'JBSWY3DPEBLW64TMMQ======'
base64.b32decode
Decodes a base32-encoded string back to binary data.
import base64
decoded = base64.b32decode(b'JBSWY3DPEBLW64TMMQ======')
print(decoded)
Output:
b'Hello, World!'
base64.b16encode
Encodes binary data to a base16-encoded string.
import base64
encoded = base64.b16encode(b'Hello, World!')
print(encoded)
Output:
b'48656C6C6F2C20576F726C6421'
base64.b16decode
Decodes a base16-encoded string back to binary data.
import base64
decoded = base64.b16decode(b'48656C6C6F2C20576F726C6421')
print(decoded)
Output:
b'Hello, World!'
Examples
Basic Base64 Encoding and Decoding
import base64
# Encode data
data = b'Hello, World!'
encoded = base64.b64encode(data)
print('Encoded:', encoded)
# Decode data
decoded = base64.b64decode(encoded)
print('Decoded:', decoded)
Output:
Encoded: b'SGVsbG8sIFdvcmxkIQ=='
Decoded: b'Hello, World!'
URL-Safe Base64 Encoding and Decoding
import base64
# Encode data
data = b'Hello, World!'
encoded = base64.urlsafe_b64encode(data)
print('Encoded:', encoded)
# Decode data
decoded = base64.urlsafe_b64decode(encoded)
print('Decoded:', decoded)
Output:
Encoded: b'SGVsbG8sIFdvcmxkIQ=='
Decoded: b'Hello, World!'
Base32 Encoding and Decoding
import base64
# Encode data
data = b'Hello, World!'
encoded = base64.b32encode(data)
print('Encoded:', encoded)
# Decode data
decoded = base64.b32decode(encoded)
print('Decoded:', decoded)
Output:
Encoded: b'JBSWY3DPEBLW64TMMQ======'
Decoded: b'Hello, World!'
Base16 Encoding and Decoding
import base64
# Encode data
data = b'Hello, World!'
encoded = base64.b16encode(data)
print('Encoded:', encoded)
# Decode data
decoded = base64.b16decode(encoded)
print('Decoded:', decoded)
Output:
Encoded: b'48656C6C6F2C20576F726C6421'
Decoded: b'Hello, World!'
Real-World Use Case
Encoding Binary Data for HTTP Transmission
When transmitting binary data over HTTP, it is common to encode the data using base64 to ensure it remains intact during transport.
import base64
def encode_for_http(data):
return base64.urlsafe_b64encode(data).decode('utf-8')
def decode_from_http(data):
return base64.urlsafe_b64decode(data.encode('utf-8'))
binary_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10'
encoded_data = encode_for_http(binary_data)
print('Encoded for HTTP:', encoded_data)
decoded_data = decode_from_http(encoded_data)
print('Decoded from HTTP:', decoded_data)
Output:
Encoded for HTTP: iVBORw0KGgoAAAANSUhEUgAA
Decoded from HTTP: b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10'
Conclusion
The base64
module in Python provides a convenient way to encode and decode binary data to and from base64 encoding. This is particularly useful for transmitting binary data over text-based protocols such as HTTP.
Comments
Post a Comment
Leave Comment