miller_rabin算法检测生成大素数的RSA算法实现
import math
from functools import reduce #用于合并字符
from os import urandom #系统随机的字符
import binascii #二进制和ASCII之间转换 #===========================================
def Mod_1(x,n):
'''取模负1的算法:计算x2= x^-1 (mod n)的值,
r = gcd(a, b) = ia + jb, x与n是互素数'''
x0 = x
y0 = n
x1 = 0
y1 = 1
x2 = 1
y2 = 0
while n != 0:
q = x // n
(x, n) = (n, x % n)
(x1, x2) = ((x2 - (q * x1)), x1)
(y1, y2) = ((y2 - (q * y1)), y1)
if x2 < 0:
x2 += y0
if y2 < 0:
y2 += x0
return x2
#===========================================
def Fast_Mod(a,p,m):
'''快速取模指数算法:计算 (a ^ p) % m 的值,可用pow()代替'''
a,p,m=int(a),int(p),int(m)
if (p == 0) :
return 1
r = a % m
k = 1
while (p > 1):
if ((p & 1)!=0):
k = (k * r) % m
r = (r * r) % m
p >>= 1
return (r * k) % m
#===========================================
def randint(n):
'''random是伪随机数,需要更高安全的随机数产生,
所以使用os.urandom()或者SystmeRandom模块,
生成n字节的随机数(8位/字节),返回16进制转为10进制整数返回'''
randomdata = urandom(n)
return int(binascii.hexlify(randomdata),16)
#===========================================
def primality_testing_1(n):
'''测试一,小素数测试,用100以内的小素数检测随机数x,
可以很大概率排除不是素数,#创建有25个素数的元组'''
Sushubiao=(2,3,5,7,11,13,17,19,23,29,31,37,41
,43,47,53,59,61,67,71,73,79,83,89,97)
for y in Sushubiao:
if n%y==0:
return False
return True
#===========================================
def primality_testing_2(n, k):
'''测试二,用miller_rabin算法对n进行k次检测'''
if n < 2:
return False
d = n - 1
r = 0
while not (d & 1):
r += 1
d >>= 1
for _ in range(k):
a = randint(120) #随机数
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, n)
if x == 1:
return False
if x == n - 1:
break
else:
return False
return True
#===========================================
def getprime(byte):
while True :
n=randint(byte)
if primality_testing_1(n) :
if primality_testing_2(n, 10) :
pass
else :continue
else : continue
return n
#===========================================
def RSA():
p=getprime(128) #1024bit的大整数
q=getprime(128)
while p==q: #避免p/q值相同
q=getprime(128)
n=p*q #n值公开
OrLa=(p-1)*(q-1) #欧拉函数
e=524289
'''e的选择:e的二进制表示中应当含有尽量少量的1.
e取e=524289时,其二进制为10000000000000000001,
只有两个1,加密速度快且数字大'''
d=Mod_1(e,OrLa)
print('公钥为({0},{1});\n私钥为({2},{3})'.format(n,e,n,d))
message=input('请输入任意需要加密的内容:')
#从标准输入输出流接收数据,数字化再加解密
message=list(map(ord,message))
print('ciphertext数字化:',message)
ciphertext=[]
for x in message:
ciphertext.append(pow(x,e,n))
print('ciphertext加密:',ciphertext)
message=[]
while True :
message.append(int(input('输入密文组\n(需要结束时输入0):')))
if message[-1]==0:
del message[-1]
break
plaintext=[]
for x in message:
plaintext.append(pow(x,d,n))
print('plaintext解密:',plaintext)
plaintext=list(map(chr,plaintext))
print('plaintext字符化:',plaintext)
print('plaintext=',reduce((lambda x,y: x+y),plaintext))
#=================================================== RSA()
miller_rabin算法检测生成大素数的RSA算法实现的更多相关文章
- Miller_Rabbin算法判断大素数,Pollard_rho算法进行质因素分解
Miller-rabin算法是一个用来快速判断一个正整数是否为素数的算法.它利用了费马小定理,即:如果p是质数,且a,p互质,那么a^(p-1) mod p恒等于1.也就是对于所有小于p的正整数a来说 ...
- 计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)
题目链接:https://nanti.jisuanke.com/t/25985 题目: Description: Goldbach's conjecture is one of the oldest ...
- 数学--数论--Miller_Rabin判断一个大数是不是素数(随机算法)
前提知识 1,费马定理:ap−1=1(mod p)a^{p-1}=1(mod\ p)ap−1=1(mod p)
- 重复造轮子之RSA算法(一) 大素数生成
出于无聊, 打算从头实现一遍RSA算法 第一步, 大素数生成 Java的BigInteger里, 有个现成的方法 public static BigInteger probablePrime(int ...
- RSA算法
RSA.h #ifndef _RSA_H #define _RSA_H #include<stdio.h> #include<iostream> #include<mat ...
- 基于私钥加密公钥解密的RSA算法C#实现
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...
- RSA算法原理与加密解密 求私钥等价求求模反元素 等价于分解出2个质数 (r*X+1)%[(p-1)(q-1)]=0
Rsapaper.pdf http://people.csail.mit.edu/rivest/Rsapaper.pdf [概述Abstract 1.将字符串按照双方约定的规则转化为小于n的正整数m, ...
- RSA算法java实现(BigInteger类的各种应用)
一.RSA算法 1.密钥生成 随机生成两个大素数p.q 计算n=p*q 计算n的欧拉函数f=(p-1)*(q-1) 选取1<e<f,使e与f互素 计算d,ed=1modf 公钥为(e,n) ...
- [转]应用RSACryptoServiceProvider类轻松实现RSA算法
在我们现实当中经常会存在需要对某些数据进行加密保护 然后进行解密的操作,比方,我们需要对某些XML配置信息里面的某些数据进行加密,以防止任何人打开该XML配置信息都能正常的看到该配置信息里面的内容,从 ...
随机推荐
- C#文件上传类,文件流,字节数组等
using System;using System.IO;using System.Web;using System.Web.UI.WebControls; namespace DotNet.Util ...
- win32最简单的htmlayout图形界面demo
1,下载HTMLayoutSDK,放在workspace. SDK下载地址:http://www.terrainformatica.com/htmlayout/HTMLayoutSDK.zip 2,v ...
- win7禁用离开模式,让笔记本卡盖后进入休眠
进入注册列表项 将HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerPower,中的“AwayModeEnabled”删除即 ...
- JPlayer Jquery video视频插件
近日一直在搜关于视频的jquery插件,要求功能全,跨平台,百思不得其解,偶尔找到一个插件JPlayer,国产的,很全.为什么选择JPlayer 简单:几分钟就可以上手编码.部署 可定制:可以方便地用 ...
- 怎样配置mysql_installer_community_V5.6........_setup的数据库?
直接访问查看如下路径就可以根据方法匹配 http://jingyan.baidu.com/album/c35dbcb0f1b1448916fcbcc7.html
- ios deprecated 警告消除 强迫症的选择
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" ...
- 在ASP.NET MVC4中配置Castle
---恢复内容开始--- Castle是针对.NET平台的一个非常优秀的开源项目,重点是开源的哦.它在NHibernate的基础上进一步封装,其原理基本与NHibernate相同,但它较好地解决NHi ...
- 跟着刚哥梳理java知识点——IO(十五)
凡是与输入.输出相关的类.接口都定义在java.io包下 java.io.File类 1.File是一个类,可以有构造器创建其对象.此对象对应着一个文件或者一个目录. 2.File中的类,仅涉及到如何 ...
- dubbo个人总结
dubbo,分布式服务框架,RPC服务框架. 注册中心zk,redis......,使用大多为zk 注册流程 最后一图 服务提供者启动时向/dubbo/com.foo.BarService/provi ...
- 线段树 poj 3468
Description You have N integers, A1, A2, ... ,AN. You need to deal with two kinds of operations. One ...