@@ -6,11 +6,13 @@ def is_prime(n):
6
6
return False
7
7
return True
8
8
9
+
9
10
def gcd (a , b ):
10
11
while b != 0 :
11
12
a , b = b , a % b
12
13
return a
13
14
15
+
14
16
def multiplicative_inverse (a , b ):
15
17
s1 , s2 , m = 1 , 0 , b
16
18
while b != 0 :
@@ -19,6 +21,7 @@ def multiplicative_inverse(a, b):
19
21
s1 , s2 = s2 , s1 - q * s2
20
22
return s1 % m
21
23
24
+
22
25
def powermod (x , y , p ):
23
26
res = 1
24
27
x = x % p
@@ -29,36 +32,41 @@ def powermod(x, y, p):
29
32
x = (x * x ) % p
30
33
return res
31
34
35
+
32
36
def main ():
33
37
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): ' )
35
40
if res == 'y' :
36
41
while True :
37
42
p = int (input ('Enter a prime number: ' ))
38
43
if is_prime (p ):
39
44
break
40
45
else :
41
46
print (p , 'is not a prime number' )
42
-
47
+
43
48
while True :
44
49
q = int (input ('Enter a different prime number: ' ))
45
50
if is_prime (q ) and p * q > 26 :
46
51
break
47
52
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
+
50
56
n = p * q
51
57
phi_n = (p - 1 ) * (q - 1 )
52
-
58
+
53
59
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: ' ))
55
62
if gcd (a , phi_n ) != 1 :
56
63
continue
57
64
else :
58
65
break
59
-
66
+
60
67
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 ()
62
70
63
71
encrypted_string = ""
64
72
encrypted_num = []
@@ -97,7 +105,8 @@ def main():
97
105
elif res == 'n' :
98
106
p , q , a , b = 13 , 17 , 5 , 77
99
107
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 ()
101
110
102
111
encrypted_string = ""
103
112
encrypted_num = []
@@ -139,5 +148,6 @@ def main():
139
148
print ('Invalid command!' )
140
149
continue
141
150
151
+
142
152
if __name__ == '__main__' :
143
153
main ()
0 commit comments