在现代信息安全领域,AES(Advanced Encryption Standard)是一种广泛应用的对称加密算法。今天,我们将用Python3实现AES的加解密功能,为数据安全保驾护航!💻🔒
首先,确保安装了`pycryptodome`库,这是Python中操作AES的经典工具。通过命令`pip install pycryptodome`即可完成安装。🌟
接下来是代码部分:
```python
from Crypto.Cipher import AES
import base64
def encrypt(text, key):
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
padded_text = text + (AES.block_size - len(text) % AES.block_size) chr(AES.block_size - len(text) % AES.block_size)
encrypted_text = base64.b64encode(cipher.encrypt(padded_text.encode())).decode()
return encrypted_text
def decrypt(encrypted_text, key):
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
decrypted_text = cipher.decrypt(base64.b64decode(encrypted_text)).decode()
return decrypted_text.rstrip(chr(0))
```
运行后,你可以轻松实现加密和解密操作。🙌
AES不仅高效,还支持多种模式和填充方式,适合保护敏感信息。快试试吧!🚀✨