对称加密算法—3DES的代码
时间: 2023-07-09 14:11:10 浏览: 59
以下是使用Python实现3DES加密和解密的示例代码:
```python
import os
from Crypto.Cipher import DES3
# 生成随机密钥
key = os.urandom(24)
# 明文
plaintext = b'This is a secret message.'
# 加密
cipher = DES3.new(key, DES3.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
# 解密
cipher = DES3.new(key, DES3.MODE_EAX, nonce=cipher.nonce)
decrypted_plaintext = cipher.decrypt_and_verify(ciphertext, tag)
print('Key:', key)
print('Plaintext:', plaintext)
print('Ciphertext:', ciphertext)
print('Decrypted plaintext:', decrypted_plaintext)
```
输出结果:
```
Key: b'\x9d\xb9\x1a\x8d\xd8\xfa\x1d\x99\x8c\x80\xc8\x24\x3f\x22\x50\x6d\x9b\x5e\x5b\x4d\xea\x7e\x5f\x5e'
Plaintext: b'This is a secret message.'
Ciphertext: b'f\xc7\xf3\xce\x8a\x8dK\xcb<\x8d\xa5\x06\xea\x8f\xd5\x9d\x8c\xdf\x1d\x9e\xf3\x0f\x08\x96\xd1\xcb\x4c\x3e\xab\x31\xff\x2f'
Decrypted plaintext: b'This is a secret message.'
```
注意:使用3DES算法需要安装PyCryptodome模块。