Created
June 18, 2024 10:00
-
-
Save vvanglro/56cf0fb484d0d59e5dc4debd4349450a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
# 原始数据 | |
data = "wd98" | |
# 使用标准 Base64 字符集对数据进行编码 | |
encoded_data = base64.b64encode(data.encode()).decode() | |
print(f"Base64 Encoded Data: {encoded_data}") | |
# 标准和自定义 Base64 字符集 | |
std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | |
custom_base64chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_" | |
# 将标准 Base64 编码字符集转换为自定义字符集 | |
custom_encoded_data = encoded_data.translate(str.maketrans(std_base64chars, custom_base64chars)) | |
print(f"Custom Base64 Encoded Data: {custom_encoded_data}") | |
# 将自定义 Base64 编码字符集转换回标准字符集 | |
decoded_custom_data = custom_encoded_data.translate(str.maketrans(custom_base64chars, std_base64chars)) | |
print(f"Decoded Custom Data: {decoded_custom_data}") | |
# 使用标准 Base64 字符集进行解码 | |
decoded_data = base64.b64decode(decoded_custom_data).decode() | |
print(f"Decoded Original Data: {decoded_data}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment