-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
161 lines (129 loc) · 5.42 KB
/
gui.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
import wx
import subprocess
import searchencrypt
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import hashes, hmac
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
import os
import sys
import json
from getpass import getpass
from base64 import b64encode, b64decode
import mysql.connector
########################################################################
class LoginDialog(wx.Dialog):
"""
Class to define login dialog
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="Login")
self.logged_in = False
# user info
user_sizer = wx.BoxSizer(wx.HORIZONTAL)
user_lbl = wx.StaticText(self, label="Username:")
user_sizer.Add(user_lbl, 0, wx.ALL|wx.CENTER, 5)
self.user = wx.TextCtrl(self)
user_sizer.Add(self.user, 0, wx.ALL, 5)
# pass info
p_sizer = wx.BoxSizer(wx.HORIZONTAL)
p_lbl = wx.StaticText(self, label="Password:")
p_sizer.Add(p_lbl, 0, wx.ALL|wx.CENTER, 5)
self.password = wx.TextCtrl(self, style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
self.password.Bind(wx.EVT_TEXT_ENTER, self.onLogin)
p_sizer.Add(self.password, 0, wx.ALL, 5)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(user_sizer, 0, wx.ALL, 5)
main_sizer.Add(p_sizer, 0, wx.ALL, 5)
btn = wx.Button(self, label="Login")
btn.Bind(wx.EVT_BUTTON, self.onLogin)
main_sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(main_sizer)
#----------------------------------------------------------------------
def onLogin(self, event):
"""
Check credentials and login
"""
username= "symmetric"
password = "encryption"
self.user = self.user.GetValue()
user_password = self.password.GetValue()
# if user_password == password and self.user==username:
print ("You are now logged in!")
self.logged_in = True
self.Close()
userfile = open('users.json', 'r')
try:
users = json.load(userfile)
except:
users = {}
userfile.close()
if not self.user in users.keys():
print("This user does not exist. Creating new user.")
# In the case of a new user, we have to generate both salts
encsalt = os.urandom(16)
idxsalt = os.urandom(16)
users[self.user] = (b64encode(encsalt).decode('UTF-8'), b64encode(idxsalt).decode('UTF-8'))
userfile = open('users.json', 'w')
json.dump(users, userfile, indent=6)
userfile.close()
encsalt = users[self.user][0]
idxsalt = users[self.user][1]
self.enckey = searchencrypt.deriveKey(user_password.encode('UTF-8'), b64decode(encsalt.encode('UTF-8')))
self.idxkey = searchencrypt.deriveKey(user_password.encode('UTF-8'), b64decode(idxsalt.encode('UTF-8')))
# else:
# print ("Username or password is incorrect!")
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Blind Indexing Search")
panel = MyPanel(self)
#################################
k_sizer = wx.BoxSizer(wx.HORIZONTAL)
k_lbl = wx.StaticText(self, label="Keyword:")
k_sizer.Add(k_lbl, 0, wx.ALL|wx.CENTER, 5)
#self.keyword = wx.TextCtrl(self, style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
#self.keyword.Bind(wx.EVT_TEXT_ENTER, self.search)
self.keyword = wx.TextCtrl(self)
k_sizer.Add(self.keyword, 0, wx.ALL, 5)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(k_sizer, 0, wx.ALL, 5)
btn = wx.Button(self, label="Search")
btn.Bind(wx.EVT_BUTTON, self.search)
main_sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(main_sizer)
################################
# Ask user to login
self.dlg = LoginDialog()
self.dlg.ShowModal()
authenticated = self.dlg.logged_in
if not authenticated:
self.Close()
self.Show()
def search(self, event,):
keyword = self.keyword.GetValue()
db = searchencrypt.connectToDB('cloudstorage.cwyqmpoiw0xl.us-east-1.rds.amazonaws.com', 'symmetric', 'encryption', 'world')
try:
res=searchencrypt.search_by_blindindex(db, keyword, self.dlg.idxkey, self.dlg.enckey, self.dlg.user)
except:
print("No records found.")
return
print(res)
##output to GUI
#wx.MessageBox("hi" , res)
wx.MessageBox("%s" %res, "search result")
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()