-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyGnupg.py
420 lines (401 loc) · 16.3 KB
/
EasyGnupg.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
from skimage.viewer.qt.QtCore import Qt
import operator
import datetime
from PyQt4.QtCore import QAbstractTableModel, SIGNAL
import mainwindow as maingui
import gnupg
import os
import createkeyclass as crkey
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMessageBox
import threading
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
windows = []
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class MyTableModel(QAbstractTableModel):
def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
if self.mylist:
return len(self.mylist[0])
else:
return 0
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
class crkeyExt (crkey.Ui_MainWindow):
def __init__(self,winObj):
self.winObj=winObj
self.setupUi(self.winObj)
self.winObj.show()
self.okbtn.clicked.connect(self.addPkey)
def addPkey(self):
if not self.emailtxt.text().isEmpty() and not self.passtxt.text().isEmpty():
generate = QtGui.QMessageBox()
generate.setText("Generating Key, it might take some time Please wait!")
generate.setWindowTitle("Generating Key")
generate.exec_()
self.genprog.setRange(0,0)
work = Worker(progbar=self.genprog,email=self.emailtxt.text(),passphrase = self.passtxt.text())
# thread = QtCore.QThread()
# work.moveToThread(thread)
#work.threads.connect(gentable(self))
#work.completed.connect(gentable(self))
#self.connect(work,work.signal,gentable(self))
#self.completed.connect(gentable(self))
# self.connect(self.get_thread, SIGNAL("finished()"), gentable(self))
# work.finished.connect(gentable(self))
QtCore.QObject.connect(work, QtCore.SIGNAL("hello"), gentable)
work.start()
# work.loop()
# threading.Thread(target=generatekey(self.emailtxt.text(),self.passtxt.text())).start()
else:
dial = QtGui.QMessageBox()
dial.setText(u"Please Fill all the Fields!")
dial.setWindowTitle("Empty Fields!")
dial.exec_()
#dial.addButton('Okay', QtGui.QMessageBox.RejectRole)
class Worker(QtCore.QThread):
email = "";
passphrase = "";
global progbar
def __init__(self, parent=None, *args,**kwargs):
QtCore.QThread.__init__(self,parent)
#self.signal = QtCore.SIGNAL("signal")
self.Init(*args,**kwargs)
def Init(self,progbar,email,passphrase):
self.email = email
self.passphrase = passphrase
self.progbar = progbar
def run(self):
home = os.path.expanduser("~")
try:
gpg = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg = gnupg.GPG(homedir=home+'/gsenc')
input_data = gpg.gen_key_input(name_email=self.email,passphrase=self.passphrase)
# popupmessageloading("Generating Private Key Please Wait.")
key = gpg.gen_key(input_data)
#print key
self.progbar.setRange(0,1)
self.emit(QtCore.SIGNAL("hello"), "hi from thread")
#self.completed.emit("hello")
#gentable(self)
class encWorker(QtCore.QThread):
file = "";
email = "";
global progbar
def __init__(self, parent=None, *args,**kwargs):
QtCore.QThread.__init__(self,parent)
#self.signal = QtCore.SIGNAL("signal")
self.Init(*args,**kwargs)
def Init(self,progbar,file,email):
self.file = file
self.email = email
self.progbar = progbar
def run(self):
home = os.path.expanduser("~")
try:
gpg = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg = gnupg.GPG(homedir=home+'/gsenc')
# input_data = gpg.gen_key_input(name_email=self.email,passphrase=self.passphrase)
# popupmessageloading("Generating Private Key Please Wait.")
usesless = str(self.file)
location = usesless+'.gpg'
with open(usesless, 'rb') as f:
status = gpg.encrypt_file(
f, recipients=[str(self.email)],
output=location,always_trust=True)
#print key
self.progbar.setRange(0,1)
self.emit(QtCore.SIGNAL("status"), status.stderr)
global encresut
encresut = status.stderr
#self.completed.emit("hello")
#gentable(self)
class decworker(QtCore.QThread):
file = "";
passphrase = "";
global progbar
def __init__(self, parent=None, *args,**kwargs):
QtCore.QThread.__init__(self,parent)
#self.signal = QtCore.SIGNAL("signal")
self.Init(*args,**kwargs)
def Init(self,progbar,file,passphrase):
self.file = file
self.passphrase = passphrase
self.progbar = progbar
def run(self):
home = os.path.expanduser("~")
try:
gpg = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg = gnupg.GPG(homedir=home+'/gsenc')
# print self.file
usesless = str(self.file)
location = usesless[:len(usesless)-4]
with open(usesless, 'rb') as f:
status = gpg.decrypt_file(
f, passphrase=self.passphrase,
output=location)
#print key
self.progbar.setRange(0,1)
# print status.stderr
self.emit(QtCore.SIGNAL("decstatus"), status.stderr)
global encresut
encresut = status.stderr
#self.completed.emit("hello")
#gentable(self)
Headers = ["Type","Key ID","Length","Date","Email","Fingerprint"]
global keytable
encresut = ""
class extendmain (maingui.Ui_MainWindow):
def __init__(self,windowObj):
self.windowObj=windowObj
self.setupUi(self.windowObj)
self.windowObj.show()
self.createkey.clicked.connect(self.showCrkey)
self.importkey.clicked.connect(self.showimportdialog)
self.browsefile.clicked.connect(self.showfiledialog)
self.dec.clicked.connect(self.decrypytfile)
self.enc.clicked.connect(self.encryptfile)
global keytable
keytable = self.keys
# self.windowObj.connect(keytable, SIGNAL('customContextMenuRequested(const QPoint &)'), self.contextMenuEvent)
keytable.setContextMenuPolicy(Qt.CustomContextMenu)
gentable()
keytable.customContextMenuRequested.connect(self.contextMenuEvent)
windows.append(self)
def contextMenuEvent(self):
menu = QtGui.QMenu(self.windowObj)
Exportaction = QtGui.QAction("Export Key",self.windowObj)
Exportaction.triggered.connect(lambda: self.exportkey())
Deletekey = QtGui.QAction("Delete Key",self.windowObj)
Deletekey.triggered.connect(lambda: self.deletekey())
menu.addAction(Exportaction)
menu.addAction(Deletekey)
menu.popup(QtGui.QCursor.pos())
#action = menu.exec_(keytable.mapToGlobal(position))
#menu.popup(QtGui.QCursor.pos())
def deletekey(self):
for i in self.keys.selectionModel().selection().indexes():
row, column = i.row(), i.column()
fingerprints = self.keys.model().index(row,5).data().toPyObject()
keytype = self.keys.model().index(row,0).data().toPyObject()
keytypebool = True
home = os.path.expanduser("~")
try:
gpg1 = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg1 = gnupg.GPG(homedir=home+'/gsenc')
if "pub" in keytype:
keytypebool = False
privatelist = gpg1.list_keys(secret=True)
for pkeyin in privatelist:
if fingerprints in pkeyin.get("fingerprint"):
dial = QtGui.QMessageBox()
dial.setText(u"The private key exists, please delete the private key first!")
dial.setWindowTitle("Missing Public Key!")
dial.exec_()
return
gpg1.delete_keys(fingerprints=str(fingerprints),secret=keytypebool)
gentable()
def exportkey(self):
for i in self.keys.selectionModel().selection().indexes():
row, column = i.row(), i.column()
fingerprint = self.keys.model().index(row,5).data().toPyObject()
keytype = self.keys.model().index(row,0).data().toPyObject()
home = os.path.expanduser("~")
try:
gpg1 = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg1 = gnupg.GPG(homedir=home+'/gsenc')
allkeystemp = []
private = False;
if "sec" in keytype:
private = True;
allkeystemp = gpg1.list_keys(True)
else:
allkeystemp = gpg1.list_keys()
keytoexport = None
for key in allkeystemp:
if fingerprint in key.get("fingerprint"):
if(private):
keytoexport = gpg1.export_keys(key.get("keyid"), True)
else:
keytoexport = gpg1.export_keys(key.get("keyid"))
filename = QtGui.QFileDialog.getSaveFileName(self.windowObj,"Export Key To", ".asc","")
if filename:
with open(filename, 'w') as f:
f.write(keytoexport)
# data = i.data()
#print data.toPyObject()
def encryptfile(self):
try:
for i in self.keys.selectionModel().selection().indexes():
row, column = i.row(), i.column()
keytype = self.keys.model().index(row,0).data().toPyObject()
encemail = self.keys.model().index(row,4).data().toPyObject()
if "pub" not in keytype:
dial = QtGui.QMessageBox()
dial.setText(u"Please select a public key from the table then press encrypt")
dial.setWindowTitle("Missing Public Key!")
dial.exec_()
return
except:
dial = QtGui.QMessageBox()
dial.setText(u"Please select a public key from the table then press encrypt")
dial.setWindowTitle("Missing Public Key!")
dial.exec_()
return
if not self.lineEdit.text().isEmpty():
generate = QtGui.QMessageBox()
generate.setText(u"Encrypting the File, it might take some time Please wait!")
generate.setWindowTitle("Encrypting File")
generate.exec_()
self.cryptoprog.setRange(0,0)
work = encWorker(progbar=self.cryptoprog,file=self.lineEdit.text(),email = encemail)
QtCore.QObject.connect(work, QtCore.SIGNAL("status"), self.showencresult)
work.start()
else:
dial = QtGui.QMessageBox()
dial.setText(u"Please Choose a file")
dial.setWindowTitle("Empty Fields!")
dial.exec_()
def decrypytfile(self):
# try:
# for i in self.keys.selectionModel().selection().indexes():
# row, column = i.row(), i.column()
# keytype = self.keys.model().index(row,0).data().toPyObject()
# encemail = self.keys.model().index(row,4).data().toPyObject()
# if "sec" not in keytype:
# dial = QtGui.QMessageBox()
# dial.setText(u"Please select a private key from the table then press encrypt")
# dial.setWindowTitle("Missing Public Key!")
# dial.exec_()
# return
# except:
# dial = QtGui.QMessageBox()
# dial.setText(u"Please select a private key from the table then press encrypt")
# dial.setWindowTitle("Missing Public Key!")
# dial.exec_()
# return
if not self.lineEdit.text().isEmpty():
# generate = QtGui.QMessageBox()
# generate.setText(u"Decrypting the File, it might take some time Please wait!")
# generate.setWindowTitle("Decrypting File")
# generate.exec_()
input = QtGui.QInputDialog
result = input.getText(self.windowObj, 'Decryption Passphrase', 'Please enter the decryption passphrase:')
if(result[1]):
#if ok:
self.cryptoprog.setRange(0,0)
self.work = decworker(progbar=self.cryptoprog,file=self.lineEdit.text(),passphrase = str(result[0]))
QtCore.QObject.connect(self.work, QtCore.SIGNAL("decstatus"), self.showencresult)
self.work.start()
else:
dial = QtGui.QMessageBox()
dial.setText(u"Please Choose a file")
dial.setWindowTitle("Empty Fields!")
dial.exec_()
def showencresult(self):
self.cryptoprog.setRange(0,1)
generate = QtGui.QMessageBox()
generate.setText(encresut+u"")
generate.setWindowTitle("Result")
generate.exec_()
def showCrkey(self):
crkeyWin=QtGui.QMainWindow()
crkeyUI=crkeyExt(crkeyWin)
windows.append(crkeyUI)
# crkeyWin.show()
def showimportdialog(self):
importkey = str(QtGui.QFileDialog.getOpenFileName(self.windowObj, "Select the Key",""))
if importkey:
home = os.path.expanduser("~")
try:
gpg = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg = gnupg.GPG(homedir=home+'/gsenc')
key_data = open(importkey).read()
import_result = gpg.import_keys(key_data)
gentable()
def showfiledialog(self):
filetop = str(QtGui.QFileDialog.getOpenFileName(self.windowObj, "Select the file",""))
self.lineEdit.setText(filetop)
allkeys = [];
def gentable():
global allkeys
allkeys = []
home = os.path.expanduser("~")
try:
gpg = gnupg.GPG(gnupghome=home+'/gsenc')
except TypeError:
gpg = gnupg.GPG(homedir=home+'/gsenc')
public_keys = gpg.list_keys()
private_keys = gpg.list_keys(True)
for key in private_keys:
temp = []
temp.append(key.get("type"))
temp.append(key.get("keyid"))
temp.append(key.get("length"))
temp.append(datetime.datetime.fromtimestamp(int(key.get("date"))).strftime('%Y-%m-%d %H:%M:%S'))
temp.append(key.get("uids")[0][key.get("uids")[0].find('<')+1:key.get("uids")[0].find('>')])
temp.append(key.get("fingerprint"))
allkeys.append(temp)
for key in public_keys:
temp = []
temp.append(key.get("type"))
temp.append(key.get("keyid"))
temp.append(key.get("length"))
temp.append(datetime.datetime.fromtimestamp(int(key.get("date"))).strftime('%Y-%m-%d %H:%M:%S'))
temp.append(key.get("uids")[0][key.get("uids")[0].find('<')+1:key.get("uids")[0].find('>')])
temp.append(key.get("fingerprint"))
allkeys.append(temp)
table_model = MyTableModel(myapp ,allkeys, Headers)
keytable.setModel(table_model)
keytable.resizeColumnsToContents()
keytable.setSortingEnabled(True)
if __name__ == "__main__":
import sys
myapp=QtGui.QApplication(sys.argv)
MainWIndow=QtGui.QMainWindow()
ui=extendmain(MainWIndow)
# crMainWindow=QtGui.QMainWindow()
# crKeys=crkeyExt(crMainWindow)
# app = QtGui.QApplication(sys.argv)
# MainWindow = QtGui.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
# MainWindow.show()
sys.exit(myapp.exec_())