Python实现RSA算法

  1 #coding:utf-8
  2 import random
  3 __author__ = 'Kerne0'
  4 class RSA:
  5     def __init__(self):
  6         self.p=0#大素数P
  7         self.q=0#大素数Q
  8         self.n=0#p*q
  9         self.nx=0#(p-1)*(q-1)
 10         self.e=0#公钥
 11         self.d=0#私钥
 12 
 13     #计算a*b%n
 14     def ModMul(self,a,b,n):
 15         res=0
 16         while(b):
 17             if(b&1):
 18                 res=(res+a)%n
 19             a=(a+a)%n
 20             b=b>>1
 21         return res
 22 
 23     #a^b % n
 24     def ModExp(self,a,b,n):
 25         res=1
 26         while(b):
 27             if(b&1):
 28                 res=self.ModMul(res,a,n)
 29             a=self.ModMul(a,a,n)
 30             b=b>>1
 31         return res
 32 
 33     #MillersRabin素性测试
 34     def MillersRabin(self,n):
 35         for x in range(10):#可以自定义次数
 36             a=random.randint(2,n-1)
 37             if self.ModExp(a,n-1,n)!=1:
 38                 return False
 39         return True
 40 
 41     #随机数产生
 42     def PrimeRandom(self,m=0):
 43         flag=True
 44         while(flag):
 45             for x in range(12):
 46                 y=random.randint(0,9)
 47                 m=m*10+y
 48             flag=not(self.MillersRabin(m))
 49             if flag==True:
 50                 m=0
 51         return m
 52 
 53     #计算e,辗转相除法
 54     def CalculationE(self,nx):
 55         y=0
 56         while(True):
 57             for x in range(5):
 58                 r=random.randint(1,9)
 59                 y=y*10+r
 60             a=y
 61             while(nx%a!=0):
 62                 a,nx=nx%a,a
 63             if a==1:
 64                 break
 65             y=0
 66         return  y
 67 
 68     #扩展的欧几里德算法
 69     def EGcd(self,a,b,x,y):
 70         if b==0:
 71             x[0]=1
 72             y[0]=0
 73             return a
 74         ans=self.EGcd(b,a%b,x,y)
 75         temp=x[0]
 76         x[0]=y[0]
 77         y[0]=temp-a/b*y[0]
 78         return ans
 79 
 80     #计算d
 81     def Cal(self,a,m):
 82         x=[0]
 83         y=[0]
 84         gcd=self.EGcd(a,m,x,y)
 85         if 1%gcd!=0:
 86             return -1
 87         x[0]*=1/gcd
 88         m=abs(m)
 89         ans=x[0]%m
 90         if ans<=0:
 91             ans+=m
 92         return ans
 93 
 94     #生成公私钥
 95     def rsa(self):
 96         self.p=self.PrimeRandom()
 97         print "大素数P:     ",self.p
 98         self.q=self.PrimeRandom()
 99         print "大素数Q:     ",self.q
100         self.n=self.p*self.q
101         print "P与Q乘积:    ",self.n
102         self.nx=(self.p-1)*(self.q-1)
103         print "(p-1)*(q-1)  ",self.nx
104         self.e=self.CalculationE(self.nx)
105         print "e            ",self.e
106         self.d=self.Cal(self.e,self.nx)
107         print "d            ",self.d
108 
109     #加解密测试
110     def RSAT(self,stringM):
111         print '明文:',stringM
112         #将字符串stringM转换为列表lm
113         lm=[]
114         for x in stringM:
115             lm.append(ord(x))
116         #将每个字符的ASC值加密存入lc
117         lc=[]
118         stringC=''
119 
120         for m in lm:
121             mx=self.ModExp(m,self.e,self.n)
122             lc.append(mx)
123             stringC+=str(mx)
124         print '密文:',stringC
125         #将密文解密,存入lx列表中
126         lx=[]
127         for c in lc:
128             lx.append(self.ModExp(c,self.d,self.n))
129         #将列表转换为字符串并输出
130         string=''
131         for ch in lx:
132             string+=chr(ch)
133         print '解密:',string
134 if __name__=="__main__":
135     s=RSA()
136     s.rsa()
137     x='hello world'
138     s.RSAT(x)

运行结果截图:

posted @ 2015-12-22 20:07  Kerne0  阅读(1507)  评论(2编辑  收藏  举报