NTRU_IoT/legacy-code/deb2/ntru_nino/main.py
2022-02-01 23:45:47 +07:00

164 lines
5.1 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import time
import subprocess
import ntru_nino
import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf
import paho.mqtt.client as paho
#from simplecrypt import encrypt, decrypt
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
os.chdir(ntru_nino.__path__[0])
#broker = "192.168.0.108"
broker = "localhost"
client = paho.Client()
port = 1883
builder = Gtk.Builder()
builder.add_from_file("./Publisher.glade")
# File filter
window2 = builder.get_object("windowCheck")
spinner = builder.get_object("spinner_check")
window = builder.get_object("windowMain")
msg = builder.get_object("message")
key_file = builder.get_object("openfile")
res_text = builder.get_object("result_text")
enc_time = builder.get_object("enc_time")
enc_size = builder.get_object("enc_size")
enc_seq = builder.get_object("enc_seq")
msg_len = builder.get_object("msg_len")
msg_arr = builder.get_object("msg_arr")
enc_401 = builder.get_object("enc_401")
enc_593 = builder.get_object("enc_593")
set_command = 'gcc -shared -fPIC $(cat ./satan.txt) EES401/URG_encrypt.c -o /tmp/401_encrypt.so -I include'
set_command2 = 'gcc -shared -fPIC $(cat ./satan.txt) EES593/URG_encrypt.c -o /tmp/593_encrypt.so -I include'
# filterfilter1 = Gtk.FileFilter()
# filterfilter1.set_name("Shared Object")
# filterfilter1.add_pattern("*.so")
# key_file.add_filter(filterfilter1)
if not (os.path.isfile("/tmp/401_encrypt.so")):
# window2.show()
# spinner.start()
# time.sleep(3)
os.system(set_command)
os.system(set_command2)
# subprocess.run(str(set_command), shell=True, text=True)
# spinner.stop()
# window2.hide()
window.show()
# window.show_all()
class mainWindowHandler:
def u_encrypt(self, arg, enc_method):
so_file = enc_method
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
tmp_num = "07"
c_return = u_enc.main(arg.encode('utf-8'), tmp_num.encode("utf-8"))
# print("Python Log : C Return ", c_return)
_ctypes.dlclose(u_enc._handle)
return c_return
def file_handler(self, f_name, num):
num = str(num).zfill(2)
f = open("/tmp/"+f_name+"_"+num+".dat", "rb")
return f.read()
def payload_process(self, plain):
enc_glob = enc_box
cip_glob = cip_box
print(enc_glob)
# time_enc = self.u_encrypt(plain, key_file.get_filename())
time_enc = self.u_encrypt(plain, enc_glob)
# plain = self.file_handler("cipher_EES401", 7)
plain = self.file_handler(cip_glob, 7)
bff_text = Gtk.TextBuffer()
bff_text.set_text(str(plain, errors="replace"))
res_text.set_buffer(bff_text)
enc_size.set_text(str(len(plain)))
send_data = base64.b64encode(plain)
return send_data, time_enc
def on_windowMain_destroy(self, *args):
Gtk.main_quit()
def on_close_window_clicked(self, button):
Gtk.main_quit()
def enc_m01(self, button):
print("Checkbox 1 Called")
if enc_401.get_active():
enc_593.set_sensitive(False)
else:
enc_593.set_sensitive(True)
# enc_593.set_active(True)
print("Release m02")
global enc_box
global cip_box
enc_box = "/tmp/401_encrypt.so"
cip_box = "cipher_EES401"
def enc_m02(self, button):
print("Checkbox 2 Called")
if enc_593.get_active():
enc_401.set_sensitive(False)
else:
enc_401.set_sensitive(True)
print("Release m01")
global enc_box
global cip_box
enc_box = "/tmp/593_encrypt.so"
cip_box = "cipher_EES593"
def on_msg_clicked(self, button):
bff_text = Gtk.TextBuffer()
bff_text.set_text("Loading . . . ")
res_text.set_buffer(bff_text)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher"
time_arr = []
plain_arr = []
cipher_arr = []
plain = str(msg.get_text())
# if enc_box == None:
# bff_text.set_text("Please Enter Message First !")
# res_text.set_buffer(bff_text)
plain_original = plain
print(plain)
x = 16 - (len(plain) % 16)
plain = plain+(" "*x)
plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
print(plain_arr)
print("Join List Test ", ''.join(plain_arr))
client.connect(broker, port)
for i in range(len(plain_arr)):
data_now, time_enc = self.payload_process(plain_arr[i])
cipher_arr.append(data_now)
time_arr.append(time_enc)
enc_time.set_text(str(sum(time_arr)))
enc_seq.set_text(str(i))
msg_arr.set_text(str(plain_arr))
msg_len.set_text(str(len(plain_original)))
print(time_arr)
print(msg.get_text())
print("Your File ", key_file.get_filename())
builder.connect_signals(mainWindowHandler())
Gtk.main()