python學習例項(7)
阿新 • • 發佈:2018-12-14
#========================================================= #第8章 資訊保安(Information Security)的python程式 #========================================================= #==================== #8.3 措施和技術 #==================== #++++++++++++++++++++ #8.3.1 密碼學 #++++++++++++++++++++ #非對稱加密 #<程式:把n分解成p*q> import math n = 221 m = int(math.ceil(math.sqrt(n))) flag = 0 for i in range(2,m+1,1): if n % i == 0: print(i,int(n/i)) flag = 1 break if flag == 0: print ("Cannot find!") #<程式:RSA加密解密實現> # All the functions are written by Edwin Sha def change_number (x, b): #這個函式把一個十進位制數x轉換成一串二進位制數 if x < b: L=[x]; return(L) a=x % b; x=x//b return([a]+change_number(x,b)) #the least one goes first! def mod (a,x,b): #計算 a^x mod b L=change_number(x,2) #print("x in binary = ",L) r=a % b; final=1 for i in L: if i ==1: final= (final*r) % b r = (r*r) % b return(final) def GCD(x,y): #計算 x與y的最大公約數 if x>y: a=x;b=y else: a=y;b=x if a%b ==0: return(b) return(GCD(a%b,b)) def Extended_Euclid(x,y,Vx,Vy): #return [a, b] s.t. ax + by = GCD(x,y) #by Edwin Sha r=x%y; z=x//y if r==0: return(y,Vy) Vx[0]=Vx[0]-z*Vy[0] Vx[1]=Vx[1]-z*Vy[1] return(Extended_Euclid(y, r, Vy, Vx)) def Mod_inverse(e, n): # return x : e*x mod n = 1 by Edwin Sha Vx=[1,0] Vy=[0,1] if e>n: G,X=Extended_Euclid(e,n,Vx,Vy) d=X[0]%n else: G,X=Extended_Euclid(n,e,Vx,Vy) d=X[1]%n return(d) import random def RSA_key_generation(p,q): #p and q are primes, compute keys e and d phi=(p-1)*(q-1) e=random.randint(3,phi) if e%2==0: e+=1 while(GCD(e,phi) !=1): e=random.randint(3,phi) if e%2==0: e+=1 d=Mod_inverse(e,phi) if e*d % phi !=1: print("ERROR: e and d are not generated correctly") return (e,d) def RSA_test(p,q): e,d=RSA_key_generation(p,q) n=p*q print("e, d, n: ", e, d, n) M=int(input("Please enter M (<n): ")); while M>=n: M=int(input("Please enter M (< n)")) C=mod(M,e,n) print("Before transmission, original M=",M," is encrypted to Cipher=",C) M1=mod(C,d,n) if M!=M1:print("!!! Error !!!") print("After transmission, Cipher",C, "is decrypted back to:",M1,"\n\n") p=19 q=97 RSA_test(p,q)