Python gcd calulation of rsa

def gcd(e, z): if z == 0: return e else: return gcd(z, e % z) e = int(input("Please enter the first number:")) z = int(input("Please enter the second number:")) print ("The GCD of ",e," and ",z," is ",gcd(e,z)) d = 1 while d < e: if d * e == 1 % z: print (d," * ",e," = 1 (mod ",z,")") d = d + 1 else: d = d + 1 

I am trying to use this code to search for candidates for rsa using brute force, it seems that it should work, but it can not help me?

z = (p - 1) (q - 1) to calculate z, it is used before with p = 47 and q = 59, e = 17 and d = 157, but after starting the program it does not find matches, but it should.

+6
source share
2 answers

Where are you

 if d * e == 1 % z: 

you seem to want to check "Is d*e equals (mod z ) to 1 "

But what you are doing is doing 1 % z (which gives 1 ) and checking if d * e 1 is equal.

Change it to:

 if (d*e) % z == 1: 

and he must carry out the calculation that you intend.

+3
source

some problems from your code:

1) if z = (p-1) (q-1), d * e = 1 (mod z), then z and e are coprime, gcd (e, z) will always be 1, see

2) if you say d * e = 1 (mod z), the code should be if d * e % z == 1

0
source

Source: https://habr.com/ru/post/978704/


All Articles