怎么利用Python实现RSA加密解密
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,这篇文章主要介绍"怎么利用Python实现RSA加密解密"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"怎么利用Python实现RSA加密解密"文章能帮助大家解
千家信息网最后更新 2025年11月15日怎么利用Python实现RSA加密解密
这篇文章主要介绍"怎么利用Python实现RSA加密解密"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"怎么利用Python实现RSA加密解密"文章能帮助大家解决问题。
RSA加密实验基本流程:
一、选取两个大素数p、q,并计算得到n、phi_n
二、选取常用的e = 0x10001,方便将幂运算优化为左移,加快运算速度
三、计算d,使用了扩展欧几里得算法
四、输入明文a,将明文转化为可以用于计算的数字形式
五、对a使用快速幂取模,得到密文b,以16进制显示
RSA解密流程:
六、对b使用快速幂取模,得到明文a,以字符形式显示
一、安装模块
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加密解密"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
字符
加密
字符串
编码
生成
流程
公钥
密文
明文
代码
密钥
对象
加密解密
知识
输入
形式
文本
行业
utf-8
运算
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手机无法连接到主机的服务器
数据库安全性基本技术
黑暗之魂为什么不能连服务器了
塔防三国志服务器
万科数据库论文如何下载
在线商家聊天软件开发
网络安全事件新闻源
教学系统的数据库设计与实现论文
软件开发电话技术面试问题
网易mc服务器ip
华自科技软件开发
服务器 图解
公安局网络安全实施计划
牛津词典mdb数据库
中国网络安全大赛sp战队
酷游网络技术有限公司招聘
在数据库查询日期返回utc格式
easyphp数据库
网络安全的团会活动
软件开发有什么热门技术
河北语音网络技术分类价目表
软件开发售前工资一般有多少
oracle数据库的视图
网络安全警示词
计算机保密和网络安全检查
黄浦区立体化软件开发代理品牌
鸠鸠互联网科技合肥分公司
数据库市场优势
如何使用SNS软件开发客户
数据库原理中db指什么