python实现RSA加密(解密)算法
作者:mrr 时间:2023-04-20 06:12:15
RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密标准。
今天只有短的RSA钥匙才可能被强力方式解破。到2008年为止,世界上还没有任何可靠的攻击RSA算法的方式。只要其密钥的长度足够长,用RSA加密的信息实际上是不能被解破的。但在分布式计算和量子计算机理论日趋成熟的今天,RSA加密安全性受到了挑战。
RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。
核心代码:
# -*- encoding:gbk -*- import math,random#导入模块 def prime_num(max_num):#生成小于max_num的素数列表 prime_num=[] for i in xrange(2,max_num): temp=0 sqrt_max_num=int(math.sqrt(i))+1 for j in xrange(2,sqrt_max_num): if i%j==0: temp=j break if temp==0: prime_num.append(i) return prime_num def rsa_key():#生成密钥的函数 prime=prime_num(400)#小于400的素数列表 p=random.choice(prime[-50:-1])#从后50个素数中随机选择一个作为p q=random.choice(prime[-50:-1])#从后50个素数中随机选择一个作为q while(p==q):#如果p和q相等则重新选择 q=random.choice(prime[-50:-1]) N=p*q r=(p-1)*(q-1) r_prime=prime_num(r) e=random.choice(r_prime)#随机选一个素数 d=0 for n in xrange(2,r): if (e*n)%r==1: d=n break return ((N,e),(N,d)) def encrypt(pub_key,origal):#生成加密用的公钥 N,e=pub_key return (origal**e)%N def decrypt(pri_key,encry):#生成解密用的私钥 N,d=pri_key return (encry**d)%N
下面一段代码给大家介绍python_rsa加密解密
使用python进行rsa加密与加密,包括公钥加密私钥解密,私钥加密公钥解密。(需要安装M2Crypto库)。
代码:
#!/usr/bin/env python
#encoding=utf-8
'''
测试rsa加密解密
'''
from M2Crypto import RSA
msg = 'aaaa-aaaa'
rsa_pub = RSA.load_pub_key('rsa_pub.pem')
rsa_pri = RSA.load_key('rsa_pri.pem')
print '*************************************************************'
print '公钥加密,私钥解密'
ctxt = rsa_pub.public_encrypt(msg, RSA.pkcs1_padding)
ctxt64 = ctxt.encode('base64')
print ('密文:%s'% ctxt64)
rsa_pri = RSA.load_key('rsa_pri.pem')
txt = rsa_pri.private_decrypt(ctxt, RSA.pkcs1_padding)
print('明文:%s'% txt)
print '*************************************************************'
print '私钥加密,公钥解密'
ctxt_pri = rsa_pri.private_encrypt(msg, RSA.pkcs1_padding)
ctxt64_pri = ctxt.encode('base64')
print ('密文:%s'% ctxt64_pri)
txt_pri = rsa_pub.public_decrypt(ctxt_pri, RSA.pkcs1_padding)
print('明文:%s'% txt_pri)
库的安装说明
M2Crypto库的下载地址:
https://github.com/martinpaljak/M2Crypto
或者:https://pypi.python.org/pypi/M2Crypto
依赖的库:openssh-devel gcc swig (这3个库在centos上可以直接使用yum安装)
标签:python,rsa,加密,解密,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python如何测试stdout输出
2023-10-22 23:59:56
SQL查询不重复记录/删除重复记录
2008-11-18 16:08:00
Python快速从注释生成文档的方法
2022-07-11 04:55:37
![](https://img.aspxhome.com/file/2023/2/78642_0s.png)
快速解决cv2.imread()读取图像为BGR的问题
2023-03-11 12:13:16
![](https://img.aspxhome.com/file/2023/9/69039_0s.jpg)
Python编写车票订购系统 Python实现快递收费系统
2023-06-26 15:37:26
![](https://img.aspxhome.com/file/2023/1/126971_0s.jpg)
python实现决策树分类
2021-04-04 13:35:28
![](https://img.aspxhome.com/file/2023/7/94067_0s.png)
PHP中number_format()函数的用法讲解
2023-06-02 15:48:12
![](https://img.aspxhome.com/file/2023/6/55376_0s.png)
Python反射和内置方法重写操作详解
2023-07-06 12:00:02
pandas 使用insert插入一列
2023-03-24 23:51:40
![](https://img.aspxhome.com/file/2023/2/133272_0s.jpg)
解决Laravel使用验证时跳转到首页的问题
2023-07-12 09:02:13
JS实现json数组排序操作实例分析
2024-04-18 09:44:25
![](https://img.aspxhome.com/file/2023/4/136284_0s.png)
一篇文章带你搞懂Go语言标准库Time
2024-05-09 09:54:35
在python中利用pycharm自定义代码块教程(三步搞定)
2022-11-20 01:00:50
![](https://img.aspxhome.com/file/2023/7/88937_0s.jpg)
matplotlib绘制甘特图的万能模板案例
2022-07-11 20:24:08
![](https://img.aspxhome.com/file/2023/9/130589_0s.jpg)
在Django框架中设置语言偏好的教程
2022-01-18 22:47:35
python实现发送邮件
2021-08-06 04:33:28
![](https://img.aspxhome.com/file/2023/7/104837_0s.jpg)
详解pandas数据合并与重塑(pd.concat篇)
2023-06-02 00:05:37
![](https://img.aspxhome.com/file/2023/7/107667_0s.png)
Python对切片命名的实现方法
2023-09-30 09:43:15
MySQL高级特性之集合函数
2009-02-26 16:19:00
比较全面的PHP数组的使用方法小结
2023-11-18 21:30:25