日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python学习实例(7)

發布時間:2023/12/13 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python学习实例(7) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#========================================================= #第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 = 1break 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//breturn([a]+change_number(x,b)) #the least one goes first! def mod (a,x,b): #計算 a^x mod bL=change_number(x,2)#print("x in binary = ",L)r=a % b; final=1for i in L:if i ==1: final= (final*r) % br = (r*r) % breturn(final) def GCD(x,y): #計算 x與y的最大公約數if x>y: a=x;b=yelse: a=y;b=xif 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 Shar=x%y; z=x//yif 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 ShaVx=[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]%nreturn(d)import random def RSA_key_generation(p,q): #p and q are primes, compute keys e and dphi=(p-1)*(q-1)e=random.randint(3,phi)if e%2==0: e+=1while(GCD(e,phi) !=1):e=random.randint(3,phi)if e%2==0: e+=1d=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*qprint("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)

?

總結

以上是生活随笔為你收集整理的python学习实例(7)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。