分享一款用于Base64编码和解码的工具。
import base64
def base64Encode(inputStr):
try:
encode_data = base64.b64encode(inputStr.encode('utf-8'))
return encode_data.decode('utf-8')
except TypeError as e:
return e
def base64Decode(inputStr):
try:
decode_data = base64.b64decode(inputStr)
return decode_data.decode('utf-8')
except UnicodeDecodeError as e:
return e
if __name__ == "__main__":
type = input("1. 编码 Encode\n2. 解码 Decode\n请选择需要操作的类型: ")
if type.isdigit():
if type == "1":
inputStr = input("请输入编码内容: ")
inputStr = inputStr.strip()
# Hello, World!
data = base64Encode(inputStr = inputStr)
elif type == "2":
inputStr = input("请输入编码内容: ")
# SGVsbG8sIFdvcmxkIQ==
data = base64Decode(inputStr = inputStr)
else:
pass
print(data)
else:
print("请选择 1 或 2")