import gmpy2 p =gmpy2.mpz(473398607161) q =gmpy2.mpz(4511491) e =gmpy2.mpz(17) phi_n= (p - 1) * (q - 1) d = gmpy2.invert(e, phi_n) print("d is:") print (d)
尝试在线网站分解N
http://factordb.com/index.php?query=98554799767
rsarsa
知识点:pqe 求m
Math is cool! Use the RSA algorithm to decode the secret message, c, p, q, and e are parameters for the RSA algorithm.
p = 9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483 q = 11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407 e = 65537 c = 83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034
Use RSA to find the secret message
import gmpy2 p = 9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483 q = 11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407 e = 65537 c = 83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034 n = p * q fn = (p - 1) * (q - 1) d = gmpy2.invert(e, fn) print(d) m = pow(c,d,n) print(m)
import libnum from gmpy2 import invert # 欧几里得算法 defegcd(a, b): if a == 0: return (b, 0, 1) else: g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y)
e = 65537 n = 248254007851526241177721526698901802985832766176221609612258877371620580060433101538328030305219918697643619814200930679612109885533801335348445023751670478437073055544724280684733298051599167660303645183146161497485358633681492129668802402065797789905550489547645118787266601929429724133167768465309665906113 dp = 905074498052346904643025132879518330691925174573054004621877253318682675055421970943552016695528560364834446303196939207056642927148093290374440210503657
c = 140423670976252696807533673586209400575664282100684119784203527124521188996403826597436883766041879067494280957410201958935737360380801845453829293997433414188838725751796261702622028587211560353362847191060306578510511380965162133472698713063592621028959167072781482562673683090590521214218071160287665180751
for i in range(1,e): #在范围(1,e)之间进行遍历 if(dp*e-1)%i == 0: if n%(((dp*e-1)//i)+1) == 0: #存在p,使得n能被p整除 p=((dp*e-1)//i)+1 q=n//(((dp*e-1)//i)+1) phi=(q-1)*(p-1) #欧拉定理 d=gp.invert(e,phi) #求模逆 m=pow(c,d,n) #快速求幂取模运算
#print hashlib.md5(s).hexdigest().upper() k = 'TASC?O3RJMV?WDJKX?ZM'#要还原的明文 for i in range(26): temp1 = k.replace('?',str(chr(65+i)),1) for j in range(26): temp2 = temp1.replace('?',chr(65+j),1) for n in range(26): temp3 = temp2.replace('?',chr(65+n),1) s = hashlib.md5(temp3.encode('utf8')).hexdigest().upper()#注意大小写 if s[:4] == 'E903': #检查元素 print (s) #输出密文