rsa加密算法python实现
时间: 2023-09-24 19:14:11 浏览: 82
RSA.rar_RSA算法Python_python实现加密_rsa加密_加密_加密解密
5星 · 资源好评率100%
可以使用Python自带的Cryptography库来实现RSA加密算法,代码如下:
```python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
# 生成密钥对
private_key = rsa.generate_private_key(
public_exponent=65537, key_size=2048)
public_key = private_key.public_key()
# 将密钥对序列化为PEM格式
private_pem = private_key.private_bytes(serialization.Encoding.PEM,
serialization.PrivateFormat.PKCS8,
serialization.NoEncryption())
public_pem = public_key.public_bytes(serialization.Encoding.PEM,
serialization.PublicFormat.SubjectPublicKeyInfo)
# 加密和解密
message = b"Hello, world!"
cipher_text = public_key.encrypt(
message, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None))
plain_text = private_key.decrypt(
cipher_text, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None))
```
希望这能够帮助你。请问还有其他问题吗?
阅读全文