python AES GCM
时间: 2023-11-28 21:45:29 浏览: 244
AES-GCM架构
5星 · 资源好评率100%
以下是使用pycryptodome库实现python AES GCM的示例代码:
```python
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
from Crypto.Util import Counter
# 生成随机密钥和随机IV
key = get_random_bytes(16)
iv = get_random_bytes(16)
# 初始化加密器
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
# 加密明文
plaintext = b'This is a secret message'
ciphertext, tag = cipher.encrypt_and_digest(pad(plaintext, AES.block_size))
# 解密密文
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
decrypted_plaintext = unpad(cipher.decrypt_and_verify(ciphertext, tag), AES.block_size)
# 输出结果
print("Key:", key.hex())
print("IV:", iv.hex())
print("Ciphertext:", ciphertext.hex())
print("Tag:", tag.hex())
print("Decrypted plaintext:", decrypted_plaintext.decode())
```
阅读全文