NTRU_IoT/infidel-code/py_wrappers/Sub_rciot.py

118 lines
3.6 KiB
Python
Raw Normal View History

2022-02-01 11:45:47 -05:00
import paho.mqtt.client as paho
# from simplecrypt import encrypt, decrypt
from cryptography.fernet import Fernet
# from memory_profiler import profile
import base64
import _ctypes
from ctypes import *
from textwrap import wrap
import csv
import time
# Konfigurasi MQTT server
broker = "tiplab.duckdns.org"
client = paho.Client()
port = 1883
def u_decrypt(arg):
# print("Cipher Length : ", len(arg))
so_file2 = './EES401/URG_decrypt.so'
u_dec = CDLL(so_file2)
u_dec.main.restype = c_char_p
u_dec.main.argtype = c_char_p
c_return = u_dec.main(arg)
_ctypes.dlclose(u_dec._handle)
# print("Python Log, C RETURN TYPE : ", type(c_return))
return c_return.decode('ascii')
def file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f_val = f.read()
f.close()
return f_val
def write_handler(f_name, num, arg):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
f.write(arg)
f.close()
def rsa_file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f_val = f.read()
return f.val
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("device01/msg")
#Proses enkripsi
def payload_process(msg):
msg_dec = base64.b64decode(msg)
print("Before decryption : "+str(msg_dec[:100])+"...")
dec_time = 0
if len(msg) > 736:
msg_arr = wrap(msg, 736)
dec_msg_arr = []
for i in range(len(msg_arr)):
time_tmp = []
msg = base64.b64decode(msg_arr[i])
# print("=====[ Decryption Sequence "+str(i+1)+" ]=================")
start = time.perf_counter()
dec_msg = u_decrypt(msg)
end = time.perf_counter()-start
print(end)
dec_msg_arr.append(dec_msg)
# print("C Return String : ", dec_msg )
time_tmp.append(end)
dec_time = sum(map(float, time_tmp))
final_msg = ''.join(dec_msg_arr)
else:
msg = base64.b64decode(msg)
final_msg = u_decrypt(msg)
return final_msg, dec_time
def on_message(client, userdata, msg):
# print("========================================")
# print("Topic : ", msg.topic)
# print("QOS : ", msg.qos)
# print("Payload : ", msg.payload)
msg = msg.payload.decode()
if len(msg) != 0:
# all_data = msg.split('&', 1)
# user_ip = all_data[0]
# msg = all_data[1]
# print("Publisher : ",user_ip)
print("====[ Incomming Msg ]=======================")
print("Incomming Message Length : ", len(msg))
# print("Incomming Message Type : ", type(msg))
real_data,dec_time = payload_process(msg)
with open("dec_report.csv", "a+") as f:
writer = csv.writer(f)
writer.writerow([len(msg), dec_time, real_data])
# real_data = real_data[1]
print("Decryption Result (Plaintext) : ", real_data)
# print("Result Length : ", len(real_data))
print(" ")
return real_data
base_name = "cipher"
base_name_RSA3 = "RSA_3072"
base_name_RSA7 = "RSA_7680"
header = ["Message Length", "Decryption Time", "Message"]
with open("dec_report.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(header)
# client.tls_set()
# client.username_pw_set(username="aaa", password="pass")
client.connect(broker,port)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()