-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
197 lines (164 loc) · 6.94 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
# -*- coding: utf-8 -*-
"""Copyright Mohammad Hassan Zahraee 2016
"""
import os
import sys
import tarfile
import tempfile
from enum import Enum
from os.path import split
from mycipher import MyCipher
# Import Qt modules
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QDialogButtonBox
# Import the compiled UI module
# these modules are auto generated from .ui files designed in Qt Designer
from window_ui import Ui_MainWindow
from password_ui import Ui_PassDialog
BOXTYPE = Enum('BoxType', 'SUCCESS ERROR')
# Create a class for our main window
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# This is always the same
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def add_file(self):
"""Add files or directories to the list of files/directories"""
file_dialog = QFileDialog(self)
if file_dialog.exec_():
filenames = file_dialog.selectedFiles()
self.ui.listWidget.addItems(filenames)
self.ui.encryptButton.setEnabled(True)
def remove_file(self):
"""Removes selected files (addresses) from the list"""
listWidget = self.ui.listWidget
selected = listWidget.selectedItems()
for item in selected:
row = listWidget.row(item)
listWidget.takeItem(row)
del item
if listWidget.count() == 0:
# if list is empty, remove and encrypt buttons disabled
self.ui.removeButton.setEnabled(False)
self.ui.encryptButton.setEnabled(False)
def selection_changed(self):
""" Triggered when there is a change in selection in the list
Item selected/deselected in the list.
"""
listWidget = self.ui.listWidget
if listWidget.count() > 0:
self.ui.removeButton.setEnabled(True)
else:
self.ui.removeButton.setEnabled(False)
def encrypt(self):
"""Implementation of UI's "Encrypt" button function"""
listWidget = self.ui.listWidget
# open a dialog to choose/create file that will be our encrypted file
filter = 'Encrypted (*.encrypted)'
save_file = QFileDialog.getSaveFileName(self,
'Save encrypted file',
filter=filter)[0]
tempfile = save_file + ".tmp"
if save_file:
# Making a tar archive from all files in the list and save tar as
# chosen by user in file dialog
with tarfile.open(tempfile, "w") as tar:
for i in range(listWidget.count()):
item = listWidget.item(i).text()
tar.add(item, split(item)[1])
# Getting password(key) for encryption
pass_dialog = PasswordDlg(self)
if pass_dialog.exec_():
password = pass_dialog.password()
cipher = MyCipher(str(password), save_file)
if cipher.encrypt():
self.message_box("Successful Encryption!")
os.remove(tempfile)
def browse(self):
self.encrypted_file = QFileDialog.getOpenFileName()[0]
self.ui.lineEdit.setText(self.encrypted_file)
def text_changed(self):
if self.ui.lineEdit.text():
self.ui.decryptButton.setEnabled(True)
else:
self.ui.decryptButton.setEnabled(False)
def decrypt(self):
"""Implementation of UI's "Decrypt" button function"""
pass_dialog = PasswordDlg(self)
infile = self.ui.lineEdit.text()
if not os.path.isfile(infile):
self.message_box("File not found!")
return
if pass_dialog.exec_():
password = pass_dialog.password()
cipher = MyCipher(password, infile)
outfile = cipher.decrypt()
if outfile:
dir, fname = split(outfile)
prefix = fname.rsplit('.tar', 1)[0]+' '
outdir = tempfile.mkdtemp(prefix=prefix, dir=dir)
with tarfile.open(outfile, 'r') as tar:
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner=numeric_owner)
safe_extract(tar, outdir)
os.remove(outfile)
self.message_box("Successful Decryption!")
def message_box(self, text, type=BOXTYPE.SUCCESS):
msgBox = QMessageBox()
msgBox.setText(text)
if type == BOXTYPE.SUCCESS:
pixmap = QtGui.QPixmap(":/icon/image/check.png")
elif type == BOXTYPE.ERROR:
pixmap = QtGui.QPixmap(":/icon/image/delete.png")
msgBox.setIconPixmap(pixmap)
msgBox.exec_()
# create a class for our password dialog
class PasswordDlg(QDialog):
""" Class Password Dialog
implementation of password dialog
"""
def __init__(self, parent=None, master=False):
QDialog.__init__(self, parent=parent)
# This is always the same
self.ui = Ui_PassDialog()
self.ui.setupUi(self)
# setting icon for Ok button
ok_button = self.ui.buttonBox.button(QDialogButtonBox.Ok)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/image/check.png"),
QtGui.QIcon.Normal,
QtGui.QIcon.Off)
ok_button.setIcon(icon)
# adjust label if we are asking for master password
if master:
self.ui.label.setText("Enter Your Master Password:")
self.setWindowTitle("Master Password")
def password(self):
return self.ui.lineEdit.text()
def show_password(self):
self.ui.lineEdit.setEchoMode(QLineEdit.Normal)
def main():
# boilerplate code for Qt applications
app = QtWidgets.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()