🎉 Python3 实现AES加解密 🎉
在现代信息安全领域,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不仅高效,还支持多种模式和填充方式,适合保护敏感信息。快试试吧!🚀✨
免责声明:本答案或内容为用户上传,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。 如遇侵权请及时联系本站删除。