from PIL import Image from Crypto.Util.number import * from numpy import array, zeros, uint8 import gmpy2 as gp import cv2 from key import x,y,kn,hint image = cv2.imread("flag.jpg") img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) imagearray = array(img_gray) h = len(imagearray) w = len(imagearray[0])
import libnum e = 65537 n = 85413323752199019806030766630760449394238054889872415531186815348349883843039718091361611175963675771467536496812507338620957273406076058263122453235926619595761737396698699834116678598534261542535530241537247151318756003375573850725841254167462648747492270335084402716816450008370008491069875351593380154253 dp = 1576424214336939000475035870826282526256046059505538052583882122452307602095912733650442447289122473348318614749578285418144935611098423641334952097553125 c = 53653254613997095145108444611576166902006080900281661447007750088487109015427510365774527924664116641019490904245926171500894236952984157500461367769566121581870986304353174732328118576440353500038670030097108081972287049673200783198844842527470746431369314585103203118824985764754487936404004696485346196488 pd = e*dp-1
defext_euclid(a, b): if b == 0: return1, 0, a else: x, y, q = ext_euclid(b, a % b) x, y = y, (x - (a // b) * y) return x, y, q
defmod_inv(a, b): return ext_euclid(a, b)[0] % b #函数返回的第一个数%b
for i inrange(1,e): if pd % i == 0: if n % (pd//i+1) == 0: p = pd//i+1 q = n//p fn = (p-1)*(q-1) d = mod_inv(e,fn) m = pow(c,d,n) print(libnum.n2s(m)) #b'*********** kn = 8 **************'
defC(a,b): ret = 1 for i inrange(b): ret*=(a-i) ret/=(b-i) return ret
defdec(e,dp,k,n): m,t=20,10 P = Zmod(k*n)['x'] x = P.gen() f = e*(dp+x) - 1 + k f = f.monic() f = f.change_ring(ZZ) a = list(f)[0]#常数a L=matrix(ZZ,m+1,m+1) for i inrange(m+1): for j inrange(i+1): L[i,j] = k^(m-i)*n^max(0,t-i)*C(i,j)*pow(a,i-j)*pow((1<<330),j) L = L.LLL() P = ZZ['y'] y = P.gen() for i inrange(m+1): for j inrange(m+1): L[i,j]=L[i,j]//pow(1<<330,j)#矩阵并不能直接乘pow(y,j),但取其行,对其中每一列可以操作
L = L*vector([y^i for i inrange(m+1)])#对应多个多项式方程 for i in L: r = i.roots() iflen(r) > 0: #print(r) returnint(r[0][0]+dp)
e = 715817461603291345056500257 dpm = 443241367152182375089742080168879750401826010196673998 dqm = 976237271910199872999512149615932692866377555493664771 n = 27691523316352200817507757939445581338449495137376447632107748594499386764483747774332325018477605530261133431823935050480971625479504155045304870831284969481372795315373409317596161046086158910790777312868535076795758303488626394277433645375132000508031171867198231228726163618498261234493051930360826747417 kbits = 330 A = (2^(2*kbits)*e^2*dpm*dqm)//n + 1 b = (1 - A*(n-1))%e delta = b^2 - 4*A if delta > 0: assert sqrt(delta)^2 == delta k = (b+sqrt(delta))//2 l = (b-sqrt(delta))//2 print(f'k = {k}') print(f'l = {l}')
result = [dec(e,dpm<<kbits,k,n),dec(e,dqm<<kbits,l,n),dec(e,dpm<<kbits,l,n),dec(e,dqm<<kbits,k,n)] print(result) dp = result[2] dq = result[3] p = (e*dp-1)//l+1 q = (e*dq-1)//k+1 assert n == p*q print(f'p = {p}') print(f'q = {q}')