您现在的位置是:网站首页 > 博客日记 >

base64编码、解码

作者:YXN-python 阅读量:74 发布日期:2023-02-28

import base64

# Base64编码
def encode_base64(input_string):
    encoded_bytes = base64.b64encode(input_string.encode('utf-8'))
    encoded_string = base64.urlsafe_b64encode(encoded_bytes).decode('utf-8')
    return encoded_string

# Base64解码
def decode_base64(encoded_string):
    decoded_bytes = base64.urlsafe_b64decode(encoded_string.encode('utf-8'))
    decoded_string = decoded_bytes.decode('utf-8')
    return decoded_string

# 测试
original_string = "Hello, World!"
encoded_string = encode_base64(original_string)
decoded_string = decode_base64(encoded_string)

print("原始字符串:", original_string)
print("编码后的Base64字符串:", encoded_string)
print("解密后的字符串:", decoded_string)

YXN-python

2023-02-28