Python怎么实现RSA加密解密
发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章主要介绍了Python怎么实现RSA加密解密的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python怎么实现RSA加密解密文章都会有所收获,下面我们一起来看看吧
千家信息网最后更新 2025年11月13日Python怎么实现RSA加密解密
这篇文章主要介绍了Python怎么实现RSA加密解密的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python怎么实现RSA加密解密文章都会有所收获,下面我们一起来看看吧。
一、安装模块
pip install pycryptodome
二、生成密钥对
密钥对文件生成和读取
代码:
from Crypto.PublicKey import RSAdef create_rsa_pair(is_save=False): ''' 创建rsa公钥私钥对 :param is_save: default:False :return: public_key, private_key ''' f = RSA.generate(2048) private_key = f.exportKey("PEM") # 生成私钥 public_key = f.publickey().exportKey() # 生成公钥 if is_save: with open("crypto_private_key.pem", "wb") as f: f.write(private_key) with open("crypto_public_key.pem", "wb") as f: f.write(public_key) return public_key, private_keydef read_public_key(file_path="crypto_public_key.pem") -> bytes: with open(file_path, "rb") as x: b = x.read() return bdef read_private_key(file_path="crypto_private_key.pem") -> bytes: with open(file_path, "rb") as x: b = x.read() return b三、加密
流程:输入文本(str)→字符串编码(默认utf-8)(bytes)→rsa加密(bytes)→base64编码(bytes)→解码为字符串(str)
代码:
import base64from Crypto.Cipher import PKCS1_v1_5from Crypto.PublicKey import RSAdef encryption(text: str, public_key: bytes): # 字符串指定编码(转为bytes) text = text.encode('utf-8') # 构建公钥对象 cipher_public = PKCS1_v1_5.new(RSA.importKey(public_key)) # 加密(bytes) text_encrypted = cipher_public.encrypt(text) # base64编码,并转为字符串 text_encrypted_base64 = base64.b64encode(text_encrypted ).decode() return text_encrypted_base64 if __name__ == '__main__': public_key = read_public_key() text = '123456' text_encrypted_base64 = encryption(text, public_key) print('密文:',text_encrypted_base64)四、解密
说明:解密流程与加密流程相反(按照加密流程逆序解密)
流程:输入文本(str)→字符串编码(默认utf-8)(bytes)→base64解码(bytes)→rsa解密(bytes)→解码为字符串(str)
代码:
import base64from Crypto.Cipher import PKCS1_v1_5from Crypto import Randomfrom Crypto.PublicKey import RSAdef decryption(text_encrypted_base64: str, private_key: bytes): # 字符串指定编码(转为bytes) text_encrypted_base64 = text_encrypted_base64.encode('utf-8') # base64解码 text_encrypted = base64.b64decode(text_encrypted_base64 ) # 构建私钥对象 cipher_private = PKCS1_v1_5.new(RSA.importKey(private_key)) # 解密(bytes) text_decrypted = cipher_private.decrypt(text_encrypted , Random.new().read) # 解码为字符串 text_decrypted = text_decrypted.decode() return text_decrypted if __name__ == '__main__': # 生成密文 public_key = read_public_key() text = '123456' text_encrypted_base64 = encryption(text, public_key) print('密文:',text_encrypted_base64) # 解密 private_key = read_private_key() text_decrypted = decryption(text_encrypted_base64, private_key) print('明文:',text_decrypted)五、完整代码
import base64from Crypto.Cipher import PKCS1_v1_5from Crypto import Randomfrom Crypto.PublicKey import RSA# ------------------------生成密钥对------------------------def create_rsa_pair(is_save=False): ''' 创建rsa公钥私钥对 :param is_save: default:False :return: public_key, private_key ''' f = RSA.generate(2048) private_key = f.exportKey("PEM") # 生成私钥 public_key = f.publickey().exportKey() # 生成公钥 if is_save: with open("crypto_private_key.pem", "wb") as f: f.write(private_key) with open("crypto_public_key.pem", "wb") as f: f.write(public_key) return public_key, private_keydef read_public_key(file_path="crypto_public_key.pem") -> bytes: with open(file_path, "rb") as x: b = x.read() return bdef read_private_key(file_path="crypto_private_key.pem") -> bytes: with open(file_path, "rb") as x: b = x.read() return b# ------------------------加密------------------------def encryption(text: str, public_key: bytes): # 字符串指定编码(转为bytes) text = text.encode('utf-8') # 构建公钥对象 cipher_public = PKCS1_v1_5.new(RSA.importKey(public_key)) # 加密(bytes) text_encrypted = cipher_public.encrypt(text) # base64编码,并转为字符串 text_encrypted_base64 = base64.b64encode(text_encrypted).decode() return text_encrypted_base64# ------------------------解密------------------------def decryption(text_encrypted_base64: str, private_key: bytes): # 字符串指定编码(转为bytes) text_encrypted_base64 = text_encrypted_base64.encode('utf-8') # base64解码 text_encrypted = base64.b64decode(text_encrypted_base64) # 构建私钥对象 cipher_private = PKCS1_v1_5.new(RSA.importKey(private_key)) # 解密(bytes) text_decrypted = cipher_private.decrypt(text_encrypted, Random.new().read) # 解码为字符串 text_decrypted = text_decrypted.decode() return text_decryptedif __name__ == '__main__': # 生成密钥对 # create_rsa_pair(is_save=True) # public_key = read_public_key() # private_key = read_private_key() public_key, private_key = create_rsa_pair(is_save=False) # 加密 text = '123456' text_encrypted_base64 = encryption(text, public_key) print('密文:', text_encrypted_base64) # 解密 text_decrypted = decryption(text_encrypted_base64, private_key) print('明文:', text_decrypted)运行:
关于"Python怎么实现RSA加密解密"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Python怎么实现RSA加密解密"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
加密
字符
字符串
编码
生成
公钥
流程
加密解密
代码
密文
密钥
对象
知识
内容
文本
明文
篇文章
utf-8
输入
价值
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
提高政治意识做好网络安全
网络安全及舆情监控小组
网络安全 个人信息包
软件开发服务合同续签规定
机房和服务器
适合软件开发的机箱
物联网没有服务器怎么办
网络安全校企合作的好处
空间网络安全关键技术
外卖小程序数据库数据
数据库开发 下载
c 轻量数据库
科技进步比如互联网
信阳直播软件开发
服务器后台数据留存
海致网络技术有限公司融资
魔兽不同服务器寄信
ice服务器停服会有什么影响
为什么要重视网络安全作文
南京市软件开发招聘单位
科技改变生活的作文互联网
恒普网络技术有限公司地址
计算机网络技术专升本考试内容
网络安全毕设论文
实时数据库价格
杭州探途网络技术有限公司
对标技术数据库
关于网络安全议论文800字
福建工程学院网络安全专业校区
it软件开发工程师