Skip to content

Instantly share code, notes, and snippets.

@vvanglro
Created June 18, 2024 10:00
Show Gist options
  • Save vvanglro/56cf0fb484d0d59e5dc4debd4349450a to your computer and use it in GitHub Desktop.
Save vvanglro/56cf0fb484d0d59e5dc4debd4349450a to your computer and use it in GitHub Desktop.
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