-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafeMail.py
309 lines (260 loc) · 9.93 KB
/
safeMail.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#! python3
# safeMail.py - Read and send encrypted emails
# TODO: Add option of names with emails
from re import sub
from rich.console import Console
from datetime import datetime as d
from pathlib import Path
import subprocess
import pyinputplus as pyip
import os
import sys
import shutil
import ezgmail
import publicKeyCipher
import makePublicPrivateKeys
console = Console()
try:
ezgmail.init()
except Exception:
console.print(
"Gmail login failed, please check your credentials in credentials.json",
style="bold red",
)
console.print(Exception, style="red")
console.print(
"More information about program on https://www.github.com/foglar/safeMail/",
style="bold blue",
)
sys.exit()
encryptedMessages = []
def main():
logo()
while True:
try:
read_or_send = console.input(
"To view your emails, press [bold red]ENTER[/bold red] > "
)
except KeyboardInterrupt:
subprocess.call("/bin/clear", shell=False)
console.print("\nBye!", style="bold red")
console.print(
"More information about program on https://www.github.com/foglar/safeMail/",
style="bold blue",
)
sys.exit()
print("")
# View emails
if (
read_or_send == ""
or read_or_send.lower().startswith("v")
or read_or_send.lower().startswith("view")
):
senders, emails, timestamps = readEmails()
subprocess.call("/bin/clear", shell=False)
for i in range(len(emails)):
preview = emails[i][:50].replace("\n", " ")
console.print(
f"Email from : [bold red] {senders[i]} [/bold red]\tTime: [bold red] {timestamps[i]} [/bold red]\tIndex: {i+1}"
)
console.print(f"Message : [bold cyan] {preview}... [/bold cyan]")
console.print("")
# Read email
elif read_or_send.lower().startswith("r") or read_or_send.lower().startswith(
"read"
):
emailSender, emailBody, emailTime = readEmail()
subprocess.call("/bin/clear", shell=False)
console.print(
f"Email from : [bold red] {emailSender} [/bold red]\tTime: [bold red] {emailTime} [/bold red]"
)
console.print(f"Message : [bold cyan] {emailBody} [/bold cyan]")
console.print("")
# Send email
elif read_or_send.lower().startswith("s") or read_or_send.lower().startswith(
"send"
):
subprocess.call("/bin/clear", shell=False)
sendEmails()
# Help
elif read_or_send.lower().startswith("h") or read_or_send.lower().startswith(
"help"
):
subprocess.call("/bin/clear", shell=False)
logo()
console.print(
"Simple tool for safe communication with friends via gmail, messages are encrypted with public key cipher",
style="bold blue",
)
console.print(
"\t- To view your emails, press [bold red]ENTER[/bold red]",
style="bold cyan",
)
console.print(
"\t- To read an email, press [bold red]r[/bold red]", style="bold cyan"
)
console.print(
"\t- To send an email, press [bold red]s[/bold red]", style="bold cyan"
)
console.print(
"\t- To quit, press [bold red]q[/bold red]", style="bold cyan"
)
console.print(
"\t- Create new public and private keys with [bold red]nk[/bold red], or [bold red]newkeys[bold red]\n",
style="bold cyan",
)
console.print(
"More information about program on https://www.github.com/foglar/safeMail/\n",
style="bold blue",
)
# New keys
elif read_or_send.lower().startswith("nk") or read_or_send.lower().startswith(
"newkeys"
):
makeNewKeys()
# Exit program
elif read_or_send.lower().startswith("q") or read_or_send.lower().startswith(
"quit"
):
subprocess.call("/bin/clear", shell=False)
for i in range(11):
if os.path.exists(f"cache/email{i}.txt"):
os.remove(f"cache/email{i}.txt")
if os.path.exists("cache/sendEmail.txt"):
os.remove("cache/sendEmail.txt")
console.print("Cache cleared!", style="bold red")
console.print("Bye!", style="bold red")
console.print(
"More information about program on https://www.github.com/foglar/safeMail/",
style="bold blue",
)
sys.exit()
def readEmails():
senders = []
emails = []
timestamps = []
emailsEncrypted = searchEmails()
for i in range(len(emailsEncrypted)):
emailFile = open(f"cache/email{i}.txt", "w")
emailFile.write(emailsEncrypted[i].messages[0].body)
emailFile.close()
emailsDecrypted = decryptEmails(f"cache/email{i}.txt", "my_privkey.txt")
emails.append(emailsDecrypted)
senders.append(emailsEncrypted[i].messages[0].sender)
timestamps.append(emailsEncrypted[i].messages[0].timestamp)
return senders, emails, timestamps
def readEmail():
emailEncrypted = searchEmails()
emailNum = pyip.inputInt("Email number > ", max=len(emailEncrypted))
emailNum -= 1
emailFile = open(f"cache/email{emailNum}.txt", "w")
emailFile.write(emailEncrypted[emailNum].messages[0].body)
emailFile.close()
emailDecrypted = decryptEmails(f"cache/email{emailNum}.txt", "my_privkey.txt")
emailBody = emailDecrypted
emailSender = emailEncrypted[emailNum].messages[0].sender
emailTime = emailEncrypted[emailNum].messages[0].timestamp
return emailSender, emailBody, emailTime
def sendEmails():
console.print("Send email", style="bold blue")
recipient = pyip.inputMenu(list_Contacts(), numbered=True)
if recipient == "New contact":
recipient = addContact()
print("")
publicKey = pyip.inputMenu(list_Keys(), numbered=True)
if publicKey == "Costume key":
publicKey = newPathToKey()
else:
publicKey = "keys/" + publicKey
print("")
message = console.input("Message: ")
emailFile = open("cache/sendEmail.txt", "w")
emailFile.write(message)
emailFile.close()
messageEncrypted = encryptEmails("cache/sendEmail.txt", publicKey, message)
ezgmail.send(recipient, "SafeMail", messageEncrypted)
console.print("Email sent!")
def searchEmails():
encryptedMessages = ezgmail.search("label:inbox", maxResults=10)
return encryptedMessages
def decryptEmails(message, key):
try:
messageDec = publicKeyCipher.readFromFileAndDecrypt(message, key)
except Exception:
messageDec = open(message, "r").read()
return messageDec
def encryptEmails(file, key, message):
messageEnc = publicKeyCipher.encryptAndWriteToFile(file, key, message)
return messageEnc
def makeNewKeys():
subprocess.call("/bin/clear", shell=False)
console.print(
"[bold red]WARNING[/bold red]: This will delete your current keys, so you would be unable to read all your previous conversations, are you sure you want to continue? (y/n)",
style="bold red",
)
if console.input() == "y":
date = d.now().strftime("%Y-%m-%d-%H:%M:%S")
backupFilePath = Path(f"backup/my_pubkey_backup_{date}.txt")
backupFilePath2 = Path(f"backup/my_privkey_backup_{date}.txt")
if not os.path.exists("backup"):
os.mkdir("backup")
shutil.copy(f"my_pubkey.txt", backupFilePath)
shutil.copy(f"my_privkey.txt", backupFilePath2)
os.remove("my_privkey.txt")
os.remove("my_pubkey.txt")
makePublicPrivateKeys.makeKeyFiles("my", 1024)
subprocess.call("/bin/clear", shell=False)
console.print(
"Public key: /keys/my_pubkey.txt, private key: /keys/my_privkey.txt",
style="bold blue",
)
console.print(
"[bold red]WARNING[/bold red]: Don't share privkey.txt with anyone.",
style="bold blue",
)
else:
console.print("Aborting...", style="bold red")
def list_Contacts():
contacts = []
with open("contacts.txt", "r") as f:
contacts = f.readlines()
contacts = [x.strip() for x in contacts]
contacts.append("New contact")
return contacts
def list_Keys():
keys = []
keys = os.listdir("keys")
keys.append("Costume key")
return keys
def addContact():
while True:
contact = pyip.inputEmail("Email address: ")
if contact in list_Contacts():
console.print(f"Contact {contact} already exists!", style="bold red")
else:
with open("contacts.txt", "a") as f:
f.write(contact + "\n")
f.close()
break
console.print(f"Contact {contact} added!", style="bold green")
def newPathToKey():
while True:
key = pyip.inputFilepath("Key path: ")
if os.path.exists(key):
console.print(f"Key {key}!", style="bold green")
break
else:
console.print(f"Path to {key} not exist!", style="bold red")
return key
def logo():
console.print(" __ ___ ___ _ _ ", style="bold cyan")
console.print(" / _| | \/ | (_) |", style="bold cyan")
console.print(" ___ __ _| |_ ___| . . | __ _ _| |", style="bold cyan")
console.print("/ __|/ _` | _/ _ \ |\/| |/ _` | | |", style="bold cyan")
console.print("\__ \ (_| | || __/ | | | (_| | | |", style="bold cyan")
console.print(
"|___/\__,_|_| \___\_| |_/\__,_|_|_|, by [bold red]foglar[bold red]\n",
style="bold cyan",
)
if __name__ == "__main__":
main()