base64加密解密的代码
时间: 2024-04-02 21:29:55 浏览: 168
Base64是一种常用的编码方式,可以将二进制数据转换成可打印的ASCII字符。下面是一个示例的Base64加密解密的代码:
加密:
```python
import base64
def base64_encode(data):
encoded_bytes = base64.b64encode(data)
encoded_string = encoded_bytes.decode('utf-8')
return encoded_string
data = b'Hello, World!'
encoded_data = base64_encode(data)
print(encoded_data)
```
解密:
```python
import base64
def base64_decode(encoded_string):
encoded_bytes = encoded_string.encode('utf-8')
decoded_bytes = base64.b64decode(encoded_bytes)
return decoded_bytes
encoded_data = 'SGVsbG8sIFdvcmxkIQ=='
decoded_data = base64_decode(encoded_data)
print(decoded_data)
```
阅读全文