-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
202 lines (171 loc) · 5.29 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Program realizujący wymianę kluczy przy użyciu algorytmu Diffie'go Hellmana
# oraz biblioteki do wysyłania wiadomości email.
import csv
import smtplib
import getpass
from random import randrange
from email.mime.text import MIMEText
from email.utils import formataddr
import Banners
def menuWybierzA(p):
y=0
while y != 1 | y != 2 | y != 3 | y != 4:
Banners.bannerWybierzA()
y = int(input("Twój wybór to: "))
if y == 1:
liczbaA = int(input("Wpisz liczbę: "))
menuZapis(liczbaA)
return liczbaA
elif y == 2:
liczbaA = genA(p)
print("Wygenerowana liczba A to: ", liczbaA)
menuZapis(liczbaA)
return liczbaA
elif y == 3:
liczbaA = wczytaj()
liczbaA = int(liczbaA[0])
return liczbaA
elif y == 4:
liczbaA = 0
return liczbaA
else:
y = 0
print("Wybierz opcję od 1 - 4!")
def menuWczytajKlucz():
y = 0
while y != 1 | 2 | 3:
Banners.bannerWczytajKlucz()
y = int(input("Twój wybór: "))
if y == 1:
klucz = int(input("Wpisz klucz: "))
menuZapis(klucz)
return klucz
elif y == 2:
klucz = wczytaj()
klucz = int(klucz[0])
return klucz
elif y == 3:
klucz = [0, 0]
return klucz
else:
y = 0
print("Wybierz opcję od 1 - 2!\n")
def menuZapis(x):
y = 0
while y != 1:
Banners.bannerMenuZapis()
y = int(input("Twój wybór: "))
if y == 1:
zapiszDoCsv(x)
elif y == 2:
break
else:
print("Wybierz od 1 - 2")
pass
def wczytajPG():
reader = csv.reader(
open('liczby.csv', 'r'),
delimiter=',',
quoting=csv.QUOTE_NONE
)
liczby = list(reader)
return liczby[0]
def wczytaj():
filename = input("Podaj nazwę bez rozszerzenia: ")
filenamecsv = filename + ".csv"
reader = csv.reader(
open(filenamecsv, 'r'),
delimiter=',',
quoting=csv.QUOTE_NONE
)
liczby = list(reader)
return liczby[0]
def zapiszDoCsv(x):
filename = input("Podaj nazwę bez rozszerzenia: ")
filenamecsv = filename + ".csv"
with open(filenamecsv, 'w', encoding='utf-8') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow([x])
def genA(p):
a = randrange(2, p - 2)
return a
def genKlucz(g, a, p):
A = (g ** a) % p
return A
def wyslijEmail(key):
gmail_user = str(input('Podaj swój adres Gmail: '))
gmail_password = getpass.getpass('Podaj hasło gmail: ')
adresat = str(input('Podaj email odbiorcy: '))
sent_from = gmail_user
to = adresat
klucz = "Klucz to: " + str(key)
email_text = MIMEText(klucz)
email_text['From'] = formataddr((str(sent_from), str(sent_from)))
email_text['To'] = formataddr((str(adresat), str(adresat)))
email_text['Subject'] = 'Klucz publiczny'
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text.as_string())
server.close()
print('Email wysłany!')
except:
print('Coś nie tak...')
def dane(p,g,liczbaA, wiadomoscJawna, kluczPrywatny):
print("p:\t%d\ng:\t%d\nliczba A:\t%d\n"
"Wiadomość:\t%d\nKlucz prywatny:\t%d\n "
% (p, g, liczbaA, wiadomoscJawna, kluczPrywatny))
def main():
try:
p, g = wczytajPG()
p = int(p)
g = int(g)
liczbaA = 0
except (FileNotFoundError, IOError):
print("Błędny plik lub ścieżka")
wiadomoscJawna = 0
kluczPrywatny = 0
x = 0
Banners.bannerMain()
dane(p, g, liczbaA, wiadomoscJawna, kluczPrywatny)
while x != 7:
Banners.bannerMain()
dane(p, g, liczbaA, wiadomoscJawna, kluczPrywatny)
print("p = %d, g = %d, liczba A = %d" % (p, g, liczbaA))
x = int(input("Wybierz od 1 do 7: "))
if x == 1:
liczbaA = menuWybierzA(p)
elif x == 2:
wiadomoscJawna = menuWczytajKlucz()
wiadomoscJawna = int(wiadomoscJawna[0])
elif x == 3:
wiadomoscJawna = genKlucz(g, liczbaA, p)
print("Klucz: ", wiadomoscJawna)
menuZapis(wiadomoscJawna)
elif x == 4:
try:
kluczPrywatny = genKlucz(wiadomoscJawna, liczbaA, p)
print("Klucz: ", kluczPrywatny)
menuZapis(kluczPrywatny)
except UnboundLocalError:
print("Nie wczytałeś wiadomości!")
elif x == 5:
try:
print("Wiadomość: ", wiadomoscJawna)
print("Klucz: ", kluczPrywatny)
except UnboundLocalError:
print("Nie wygenerowano klucza!")
z = int(input("1 - Zapisz klucz prywatny\n2 - Zapisz klucz publiczny\n 3 - Wróć do Menu"))
if z == 1:
zapiszDoCsv(wiadomoscJawna)
elif z == 2:
zapiszDoCsv(kluczPrywatny)
else:
pass
elif x == 6:
wyslijEmail(wiadomoscJawna)
else:
pass
exit()
main()