aes加解密算法python实现
时间: 2023-08-24 21:04:47 浏览: 149
以下是Python中使用AES加解密算法的示例代码:
```python
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
# 密钥(key), 密斯向量(iv)和要加密的明文(plaintext)
key = b'This is a key123'
iv = b'This is an IV456'
plaintext = b'Hello World!'
# 创建AES对象并使用CBC模式进行加密
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
# 将密文进行base64编码
encrypted_text = base64.b64encode(ciphertext)
print('加密后的密文:', encrypted_text)
# 将base64编码的密文解码并进行解密
ciphertext = base64.b64decode(encrypted_text)
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_text = cipher.decrypt(ciphertext)
# 去除解密后的明文的填充
decrypted_text = unpad(decrypted_text, AES.block_size)
print('解密后的明文:', decrypted_text)
```
输出结果:
```
加密后的密文: b'wXQq+zQhZ5QlU+HwP2y6jg=='
解密后的明文: b'Hello World!'
```
注意,以上代码需要安装 `pycryptodome` 库。
阅读全文
相关推荐















