-
Notifications
You must be signed in to change notification settings - Fork 0
/
LilyXOR.py
358 lines (305 loc) · 9.7 KB
/
LilyXOR.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
import sys, shelve, getpass, string, os, email
import smtplib, poplib
from email import parser
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
#For Simple XOR-Like
from itertools import izip, cycle
import random
import datetime
import time
import binascii
from Tkinter import Tk
from tkFileDialog import askopenfilename
import SimpleXOR
from re import *
now = datetime.datetime.now()
today = datetime.date.today()
pat=compile('([A-Z]*[a-z]*[0-9]*)+@([A-Z]*[a-z]*[0-9]*)+.([A-Z]*[a-z]*[0-9]*)')
def ask(question):
print "\r%s" %question,
return sys.stdin.readline().strip()
def isGoodEmail(addr):
return pat.search(addr)
def getUser():
user = ask('Enter Email: ')
if isGoodEmail(user):
return user
else:
print 'Invalid email address, try again:'
return getUser()
def getReceiver(lst):
receiver=ask('To: ')
if isGoodEmail(receiver):
lst.append(receiver)
more=ask('More recipients?(Y/N)')
if more=='Y' or more=='y':
getReceiver(lst)
else:
print 'Invalid email address, try again:'
return getReceiver(lst)
def getSubject():
subject=ask('Subject: ')
if len(subject)>0: return subject
else:
ans= ask('Proceed without subject?(Y/N) ')
if ans=='N' or ans=='n':return getSubject()
else: return '[No subject]'
def getPassword(user):
password= getpass.getpass('enter password for account %s: '%user)
return password
def sendMail(sender,receiver,subject,text,pwd,files):
msg=MIMEMultipart()
msg['From']=sender
msg['To']=email.Utils.COMMASPACE.join(receiver)
msg['Date']=email.Utils.formatdate(localtime=True)
msg['Subject']=subject
msg.attach(MIMEText(text))
for f in files:
part=MIMEBase('application', "octet-stream")
part.set_payload(open(f,"rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
server=smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(sender[0:sender.find('@')] ,pwd)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
def connect(uid,pwd):
print 'Connecting...'
pop_conn=poplib.POP3_SSL('pop.gmail.com')
pop_conn.user(userid)
pop_conn.pass_(password)
print pop_conn.getwelcome()
return pop_conn
def attach(files):
Tk().withdraw()
path=askopenfilename()
files.append(path)
more=ask('More attachments?(Y/N)')
if more=='Y' or more=='y':
attach(files)
def filess():
list_ = ["email/data.in", "email/data.out", "email/key.in", "email/email.mail"] #You can define any path. The defualt directory is where xor_en.py is.
for p in list_:
open(p, 'w')
#this will insure clean files everytime you run it.
#####################
## XOR Formula ##
## ##
#####################
def xor_en(ms, ke):
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(ms, cycle(ke)))
#####################
## Randomiser ##
## ##
#####################
#string.digits add it instead of asscii_lowercase or uppercase.
def rand(size=[], chars=string.ascii_uppercase + string.ascii_lowercase):
return ''.join(random.choice(chars) for x in range(size))
def composeMail(sender,password,re=0,msg=None):
if re==0:#not a reply
print 'Composing a new message...'
receiver=[]
getReceiver(receiver)
subject=getSubject()
else:# reply
receiver=[msg['Return-Path'] ]#should be a list
print 'replying to %s ...'% receiver
subject='RE:'+msg['Subject']
open_text = os.system("gedit email/email.mail")
read_ = open("email/email.mail", 'r')
read_to_msg = read_.read()
read_.close()
message= read_to_msg + "\nSent from LilyXOR\nLilyMail + Simple XOR-Like"
files=[]
hasFile=ask('Do you have an attachment?(Y/N)')
if hasFile=='Y' or hasFile=='y':
attach(files)
ans= ask('Ready to send?(Y/N)')
if ans=='Y' or ans=='y':
print 'sending...'
sendMail(sender,receiver,subject,message,password,files)
print 'email sent to ',receiver,' successfully!'
else:
print 'Message abandoned'
#####################
## Encrypter ##
## Email ##
#####################
def composeEncryptedMail(sender,password,re=0,msg=None):
if re==0:#not a reply
print 'Composing a new message...'
receiver=[]
getReceiver(receiver)
subject=getSubject()
else:# reply
receiver=[msg['Return-Path'] ]#should be a list
print 'replying to %s ...'% receiver
subject='RE:'+msg['Subject']
os.system("gedit email/data.in") #change 'gedit' with your text editor
path_of_data_in = open("email/data.in", 'r') #path of data.in
data_in = path_of_data_in.read() #read the content
path_of_data_in.close() #close the file
l = len(data_in) #count the char
for i in data_in:
key_in= rand(l) #based on l randomise
path_of_key = open("email/key.in", "w") #path of key.in
path_of_key.write(key_in)
path_of_key.close()
xor_en_x = xor_en(data_in, ke=key_in) #Encryption
path_of_out = open("email/data.out", "w")
path_of_out.write(xor_en_x) #Write the encrypted data
path_of_out.close()
log_header= """Date: """ + str(now) + '\n'+ """
Original Message: """ + str(data_in) + """
Number of Characters: """ + str(l) + '\n'+ """
Key: [""" + str(key_in) + """]""" + '\n'+ """
Encrypted Message: """+ str(xor_en_x) + """"""
log = 'email/log.txt'
log_data = open(log, "w")
log_data.write("\n" + log_header + "\n" )
log_data.close()
print ("""The defualt waiting time is 3s""")
time.sleep(3)
os.system("gedit email/data.in email/data.out email/key.in email/log.txt") #change 'gedit' with your text editor
message = xor_en_x +"\n\n\n\nThis is an encrypted email.\nSent from LilyXOR" #Encrypted message
files=[]
hasFile=ask('Do you have an attachment?(Y/N)')
if hasFile=='Y' or hasFile=='y':
attach(files)
ans= ask('Ready to send?(Y/N)')
if ans=='Y' or ans=='y':
print 'sending...'
sendMail(sender,receiver,subject,message,password,files)
print 'email sent to ',receiver,' successfully!'
else:
print 'Message abandoned'
def extractMessageBody(msg):
for part in msg.walk():
if part.get_content_type()=='text/plain':
return part.get_payload()
def getThisMailAction(messages, num):
print '[OPTIONS: reply, quit...]\n'
global userid,password
msg=messages[num]
action=ask('command: ')
if action=='reply':
composeMail(userid,password,1,msg)
getInsideAction(messages)
elif action=='quit':
getInsideAction(messages)
elif action=='help':
print '[reply] reply this email \n [quit] quit reading this email\n'
getThisMailAction(messages, num)
else:
print 'invalid command'
getThisMailAction(messages, num)
n=0
def getNum():
global n
n=n+1
return n
def chkAttach(messages,num):#save attachments
#only PDF files are checked
msg=messages[num]
attached=False
for part in msg.walk():
if part.get_content_type().find('application/pdf')>-1: attached=True
if attached:
print 'Attachment(s) found!'
ans=ask('Want to download?(Y/N)')
if ans=='Y' or ans=='y':
path=ask('Specify a path to save:')
for part in msg.walk():
ctype=part.get_content_type()
if ctype.find('application/pdf')>-1:
ext=ctype[ctype.find('/')+1:]
fullpath=path+str(getNum())+'.'+ext
open(fullpath,"wb").write(part.get_payload(decode=True))
print 'Attachment saved to %s !' %fullpath
def getInsideAction(messages):
print '[OPTIONS: ls, open, new, quit, newEn...]\n'
global userid,password
action=ask('command: ')
if action=='ls':
print 'Inbox: %s' % len(messages)
i=0
while i<len(messages):
print '['+str(i+1)+'] ',messages[i]['subject']
i=i+1
getInsideAction(messages)
elif action=='quit':
#pop_conn.quit()
userid='' #clear the data
password=''
print 'You are logged out'
getAction()
elif action=='new':
composeMail(userid,password)
getInsideAction(messages)
elif action == 'newEn': #Encrypt new message
composeEncryptedMail(userid,password)
getInsideAction(messages)
elif action=='open':
index=int(ask("please specify the mail number: "))
if 0<index<=len(messages):
print '#'*64
print 'SUBJECT:',messages[index-1]['Subject']
print 'FROM:',messages[index-1]['From']
print 'DATE:',messages[index-1]['Date']
print '-'*24+'message below'+'-'*26+'\n'
print extractMessageBody(messages[index-1])
print '#'*64
chkAttach(messages,index-1)#check attachments
getThisMailAction(messages, index-1)
else:
print 'number out of range!'
getInsideAction(messages)
elif action=='help':
print '[quit] exit login \n [new] compose new email\n [ls] list out mails in mailbox\n [open] open an email'
getInsideAction(messages)
else:
print 'invalid command'
getInsideAction(messages)
def readMail(pop_conn): #get msg from server
msgs= [pop_conn.retr(i) for i in range(1,len(pop_conn.list()[1])+1) ]
msgs=['\n'.join(m[1]) for m in msgs]
msgs= [parser.Parser().parsestr(m) for m in msgs] #a list of raw messages
pop_conn.quit() ##maybe later?
return msgs
def login():
global userid, password ### maybe global is not a good solution?
userid=getUser()
password=getPassword(userid)
connection = connect(userid,password)
messages=readMail(connection)
getInsideAction(messages)
def getAction():
print """
__ _ __ _ __ ____ ___
/ / (_)/ /__ __ | |/_// __ \ / _ \
/ /__ / // // // /_> < / /_/ // , _/
/____//_//_/ \_, //_/|_| \____//_/|_|
/___/ 0.0.1 beta\n
Original(LilyMail) by: Xiaolong Cheng \n
"""
filess()
print 'Only encrypts outgoing emails'
print '\n[OPTIONS: login, quit, sxor...]\n'
action=ask('command: ')
if action=='quit':
sys.exit(0)
elif action=='login':
print 'Please verify your identity '
login()
elif action=='help':
print '[login] start a login session\n [quit] exit program\n [sxor] SimpleXOR'
elif action=='sxor':
SimpleXOR.action()
else:
print 'invalid command...back to sxor'