服务器在线 - 服务器软件 - 网站地图 服务器在线,专注于服务器技术!

当前位置:主页 > 编程脚本 > Python > 正文

使用python3对字符进行RSA加密

时间:2023-03-18    来源:未知    投稿:admin    点击:

pip3 install cryptography -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com


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()

# 加密明文
plaintext = b"Hello, World!"
ciphertext = public_key.encrypt(plaintext, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))

# 解密密文
decrypted_text = private_key.decrypt(ciphertext, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))

print("明文:", plaintext)
print("密文:", ciphertext)
print("解密后的明文:", decrypted_text)

如果您的问题仍未解决,还可以加入服务器在线技术交流QQ群:8017413寻求帮助。


相关内容
最新热点内容