@@ -6,11 +6,13 @@ def is_prime(n):
66 return False
77 return True
88
9+
910def gcd (a , b ):
1011 while b != 0 :
1112 a , b = b , a % b
1213 return a
1314
15+
1416def multiplicative_inverse (a , b ):
1517 s1 , s2 , m = 1 , 0 , b
1618 while b != 0 :
@@ -19,6 +21,7 @@ def multiplicative_inverse(a, b):
1921 s1 , s2 = s2 , s1 - q * s2
2022 return s1 % m
2123
24+
2225def powermod (x , y , p ):
2326 res = 1
2427 x = x % p
@@ -29,36 +32,41 @@ def powermod(x, y, p):
2932 x = (x * x ) % p
3033 return res
3134
35+
3236def main ():
3337 while True :
34- res = input ('Do you want to enter prime numbers (y) or let the algorithm do it for you (n) or exit (e)? (y/n/e): ' )
38+ res = input (
39+ 'Do you want to enter prime numbers (y) or let the algorithm do it for you (n) or exit (e)? (y/n/e): ' )
3540 if res == 'y' :
3641 while True :
3742 p = int (input ('Enter a prime number: ' ))
3843 if is_prime (p ):
3944 break
4045 else :
4146 print (p , 'is not a prime number' )
42-
47+
4348 while True :
4449 q = int (input ('Enter a different prime number: ' ))
4550 if is_prime (q ) and p * q > 26 :
4651 break
4752 else :
48- print ('Both prime numbers are the same or the product is less than 26!' )
49-
53+ print (
54+ 'Both prime numbers are the same or the product is less than 26!' )
55+
5056 n = p * q
5157 phi_n = (p - 1 ) * (q - 1 )
52-
58+
5359 while True :
54- a = int (input (f'Enter a number such that Greatest Common Divisor with { phi_n } is 1: ' ))
60+ a = int (
61+ input (f'Enter a number such that Greatest Common Divisor with { phi_n } is 1: ' ))
5562 if gcd (a , phi_n ) != 1 :
5663 continue
5764 else :
5865 break
59-
66+
6067 b = multiplicative_inverse (a , phi_n )
61- message = input ('Enter the message to be encrypted (lower case): ' ).lower ()
68+ message = input (
69+ 'Enter the message to be encrypted (lower case): ' ).lower ()
6270
6371 encrypted_string = ""
6472 encrypted_num = []
@@ -97,7 +105,8 @@ def main():
97105 elif res == 'n' :
98106 p , q , a , b = 13 , 17 , 5 , 77
99107 n = p * q
100- message = input ('Enter the message to be encrypted (lower case): ' ).lower ()
108+ message = input (
109+ 'Enter the message to be encrypted (lower case): ' ).lower ()
101110
102111 encrypted_string = ""
103112 encrypted_num = []
@@ -139,5 +148,6 @@ def main():
139148 print ('Invalid command!' )
140149 continue
141150
151+
142152if __name__ == '__main__' :
143153 main ()
0 commit comments