Единственный известный мне способ сделать это - это грубая сила с использованием a для l oop в диапазоне p, вы можете довольно быстро восстановить A и B с небольшими числами, но, очевидно, невозможно для чего-либо большего, чем то, что вы есть делаю. Вот код, который восстанавливает A и B.
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
#print(g,y,x)
#print(g,x - (b // a) * y, x-(b//a), (b//a)*y, ( b//a ), y)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
for x in range(0, 16289129):
# Variables Used
sharedPrime = 16289129 # p
sharedBase = 5 #1009 # g
#sharedPrime = 1031267
#sharedBase = 111029
aliceSecret = x # a
bobSecret = 3410160 # b
# Begin
#print( "Publicly Shared Variables:")
#print( " Publicly Shared Prime: " , sharedPrime )
#print( " Publicly Shared Base: " , sharedBase )
# Alice Sends Bob A = g^a mod p
#A = (sharedBase**aliceSecret) % sharedPrime
A = pow(sharedBase,aliceSecret, sharedPrime)
#print( "\n Alice Sends Over Public Chanel: " , A )
# Bob Sends Alice B = g^b mod p
#B = (sharedBase ** bobSecret) % sharedPrime
B = pow(sharedBase,bobSecret, sharedPrime)
#print( "\n Bob Sends Over Public Chanel: ", B )
#print( "\n------------\n" )
#print( "Privately Calculated Shared Secret:" )
# Alice Computes Shared Secret: s = B^a mod p
# aliceSharedSecret = (B ** aliceSecret) % sharedPrime
#aliceSharedSecret = pow(B,aliceSecret,sharedPrime)
#print( " Alice Shared Secret: ", aliceSharedSecret )
# Bob Computes Shared Secret: s = A^b mod p
#bobSharedSecret = (A**bobSecret) % sharedPrime
bobSharedSecret = pow(A,bobSecret,sharedPrime)
#print( " Bob Shared Secret: ", bobSharedSecret )
compare = bobSecret ^ aliceSecret
mi = modinv(sharedBase,sharedPrime)
c=A
d=A
iterx=0
oldc=A
oldd=A
while c !=0:
oldc = c
oldd = d
c=c*mi%sharedPrime
d=(c*mi**2)%sharedPrime
#print(mi)
mi>>=1
iterx+=1
#print(iterx, c)
dfcompare = oldc ^ A
if dfcompare == aliceSecret: # or dfcompare == A or dfcompare == A or dfcompare == bobSharedSecret:
print("alice Key found inside: ", aliceSecret,bobSecret,aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
if dfcompare == bobSecret: # or dfcompare == A or dfcompare == B or dfcompare == bobSharedSecret:
print("bob Key found inside: ", aliceSecret,bobSecret,aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
dfcompare = oldd ^ A
if dfcompare == aliceSecret: # or dfcompare == A or dfcompare == A or dfcompare == bobSharedSecret:
print("alice Key found inside: ", aliceSecret,bobSecret,aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
if dfcompare == bobSecret: # or dfcompare == A or dfcompare == B or dfcompare == bobSharedSecret:
print("bob Key found inside: ", aliceSecret,bobSecret,aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
#print(iterx, oldc)
dfcompare = oldc ^ A
if dfcompare == aliceSecret: # or dfcompare == A or dfcompare == B or dfcompare == bobSharedSecret:
print("alice Key found outside: ", aliceSecret,bobSecret, aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
if dfcompare == bobSecret: # or dfcompare == A or dfcompare == B or dfcompare == bobSharedSecret:
print("bob Key found outside: ", aliceSecret,bobSecret, aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
#break
dfcompare = oldd ^ A
if dfcompare == aliceSecret: # or dfcompare == A or dfcompare == B or dfcompare == bobSharedSecret:
print("alice Key found outside: ", aliceSecret,bobSecret, aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
if dfcompare == bobSecret: # or dfcompare == A or dfcompare == B or dfcompare == bobSharedSecret:
print("bob Key found outside: ", aliceSecret,bobSecret, aliceSecret^bobSecret, mi, c, oldc, A,bobSharedSecret,B, dfcompare == bobSecret, dfcompare == aliceSecret, dfcompare == 266)
вывод:
alice Key found inside: 0 3410160 3410160 1628913 3257826 1 1 1 7231001 False True False
alice Key found inside: 0 3410160 3410160 1628913 3257826 1 1 1 7231001 False True False
alice Key found inside: 514345 3410160 3396057 24 3399166 5388270 1550861 9501427 7231001 False True False
alice Key found inside: 736913 3410160 4142689 25451 181780 1390738 5482147 13392747 7231001 False True False
bob Key found inside: 805168 3410160 3686848 795 14730017 2027477 12797655 13227860 7231001 True False False
alice Key found inside: 1090405 3410160 2403221 397 5406755 9329510 10418179 11317568 7231001 False True False
alice Key found inside: 1319263 3410160 2107823 397 12968756 10691327 11994016 9408910 7231001 False True False
alice Key found inside: 1520691 3410160 2309315 198 15131181 6151666 4909505 3061289 7231001 False True False
bob Key found inside: 1812398 3410160 3125086 0 9989884 9989884 11298316 15863668 7231001 True False False
bob Key found inside: 1812398 3410160 3125086 0 0 9989884 11298316 15863668 7231001 True False False
bob Key found inside: 1812398 3410160 3125086 0 0 9989884 11298316 15863668 7231001 True False False
bob Key found outside: 1812398 3410160 3125086 0 0 9989884 11298316 15863668 7231001 True False False
bob Key found outside: 1812398 3410160 3125086 0 0 9989884 11298316 15863668 7231001 True False False
bob Key found inside: 1848360 3410160 2637016 6 5892567 12707894 16113862 3527100 7231001 True False False
bob Key found inside: 2076229 3410160 2860725 12 4861935 6311004 5522604 5891481 7231001 True False False
bob Key found inside: 2637508 3410160 1848884 198 14344704 14930223 14146015 4071246 7231001 True False False
bob Key found inside: 3121830 3410160 1813078 3181 11985609 834007 3715367 14209381 7231001 True False False
alice Key found inside: 3129104 3410160 1816544 101807 3083406 16029624 1760214 8757757 7231001 False True False
alice Key found inside: 3789467 3410160 907883 99 6405853 2089061 1155243 11032296 7231001 False True False