python中非对称加密
时间: 2023-12-04 16:41:50 浏览: 34
根据提供的引用内容,无法回答Python中的非对称加密。引用中只介绍了Python3中的对称加密算法AES和DES3。非对称加密算法包括RSA、DSA等,需要使用公钥和私钥进行加密和解密。如果您需要了解Python中的非对称加密算法,可以参考Python Cryptography Toolkit (pycrypto)或cryptography等第三方库。这些库提供了RSA、DSA等非对称加密算法的实现。您可以使用这些库来实现非对称加密和解密。以下是一个使用RSA算法进行加密和解密的例子:
```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# 生成密钥对
key = RSA.generate(2048)
# 获取公钥和私钥
private_key = key.export_key()
public_key = key.publickey().export_key()
# 加密
message = b'Hello, World!'
cipher = PKCS1_OAEP.new(RSA.import_key(public_key))
encrypted_message = cipher.encrypt(message)
# 解密
cipher = PKCS1_OAEP.new(RSA.import_key(private_key))
decrypted_message = cipher.decrypt(encrypted_message)
print('Encrypted message:', encrypted_message)
print('Decrypted message:', decrypted_message)
```