-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (125 loc) · 4.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
from rsa import RSA
from utils import *
from primes import is_prime
sections = {
"action": {
"valids": ("1", "2", "3"),
"message": 'Choose an action:\n1 - Encrypt\n2 - Decrypt\n3 - Quit\n\n'
},
"values_origin": {
"valids": ("1", "2", "3"),
"message": 'Do you want to use your own values?\n1 - Yes\n2 - No\n3 - Quit\n\n'
},
"show_values": {
"valids": ("1", "2"),
"message": 'Do you want to see the values of the RSA instance?\n1 - Yes\n2 - No\n\n'
},
"continue_with_instance": {
"valids": ("1", "2"),
"message": 'Do you want to keep using this instance?\n1 - Yes\n2 - No\n\n'
},
}
def input_section(section_entry):
option = input(section_entry["message"])
while option not in section_entry["valids"]:
print(section_entry["valids"])
print('Option "{}" is invalid.'.format(option))
option = input(section_entry["message"])
return option
def is_e_valid(p, q, e):
totient = (p-1)*(q-1)
if e >= totient:
return False
elif are_coprimes(e, totient) and is_prime(e):
return True
return False
def get_init_value_input(message, first_value=None):
while True:
try:
value = int(input(message))
invalid_value = type(first_value) == int and first_value == value
while not RSA.is_prime(value) or value <= 2 or invalid_value:
if not RSA.is_prime(value):
print('The chosen value "{}" is not prime. Try another one.'.format(value))
elif value <= 2:
print('The value must be greater than 2. Try another one.')
elif invalid_value:
print('The chosen value must be different than the previous one. Try another one.')
value = int(input(message))
invalid_value = type(first_value) == int and first_value == value
return value
except ValueError:
print('The value typed is not a valid number. Try another one.')
def get_e_value(message):
while True:
try:
value = int(input(message))
e_valid = is_e_valid(p, q, value)
value_lte_2 = value <= 2
while not e_valid or value_lte_2:
if not e_valid:
print('The chosen value "{}" is not valid (must be lower than totient, coprime with totient and also a prime number). Try another one.'.format(value))
elif value_lte_2:
print('The value must be greater than 2. Try another one.')
value = int(input(message))
e_valid = is_e_valid(p, q, value)
value_lte_2 = value <= 2
return value
except ValueError:
print('The value typed is not a valid number. Try another one.')
def ask_input(name):
return input_section(sections[name])
run = True
while run:
print()
print('############################')
print('############################')
print('############################')
print('----------------------------')
print()
print('RSA - PYTHON3 IMPLEMENTATION')
print(' EC10 ')
print('RAFAEL GALANI - RA 082510282')
print(' RENAN DIAS - RA 082510282 ')
print()
print('----------------------------')
print('############################')
print('############################')
print('############################')
print()
use_own_values = ask_input("values_origin")
if use_own_values == "1":
print()
print('Setting values up...')
p = get_init_value_input('P: ')
q = get_init_value_input('Q: ', p)
e = get_e_value('E: ')
print()
rsa = RSA(p, q, e)
elif use_own_values == "2":
rsa = RSA.get_random_rsa()
elif use_own_values == "3":
run = False
break
show_values = ask_input("show_values")
if show_values == "1":
rsa.show_values()
continue_with_instance = "1"
while continue_with_instance == "1":
print()
action = ask_input("action")
print()
if action == "1":
word = input('Choose a word: ')
print('Word encrypted: {}'.format(
rsa.encrypt(word)
))
elif action == "2":
values = input('Input the word encrypted values: ')
values = list(map(int, values.split(', ')))
print('Word decrypted: {}'.format(
rsa.decrypt(values)
))
continue_with_instance = ask_input("continue_with_instance")
run = continue_with_instance != "3"