Hello Freedom

This commit is contained in:
infidel
2022-02-01 23:45:47 +07:00
commit 7009cb27c4
964 changed files with 513364 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import time
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
def rsa3072():
start = time.time()
key = RSA.generate(2048)
end = time.time()-start
with open('RSA3072_priv.pem','wb') as a:
a.write(key.exportKey('PEM'))
with open('RSA3072_pub.pem','wb') as b:
b.write(key.publickey().exportKey('PEM'))
print("Time spent RSA 3072 ", end)
def rsa7680():
start = time.time()
key = RSA.generate(7680)
end = time.time()-start
with open('RSA7680_priv.pem','wb') as c:
c.write(key.exportKey('PEM'))
with open('RSA7680_pub.pem','wb') as d:
d.write(key.publickey().exportKey('PEM'))
print("Time spent rsa7680 : ", end)
rsa3072()
rsa7680()

View File

@@ -0,0 +1,98 @@
import paho.mqtt.client as paho
# from simplecrypt import encrypt, decrypt
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
# Konfigurasi MQTT server
# broker = "192.168.43.134"
broker = "nnag.xyz"
client = paho.Client()
port = 1883
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log : C Return ", c_return)
_ctypes.dlclose(u_enc._handle)
def u_decrypt(arg):
# arg = base64.b64decode(arg)
so_file2 = './URG_decrypt.so'
arg_len = len(arg)
arg = c_char_p(arg)
print("Python Log : Current Cipher ", arg)
print("Python Log : Length to be Dec ", arg_len)
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)
return c_return
def file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
return f.read()
# Proses enkripsi
def payload_process(plain):
u_encrypt(plain)
plain = file_handler("cipher_EES401", 7)
print("Python LOG Cipher Length : ", len(plain))
send_data = base64.b64encode(plain)
return send_data
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
n = input("Enter number of plain : ")
n = int(n)
for i in range(1, n+1):
plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
x = 16 - (len(plain) % 4)
plain = plain+(" "*x)
plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
print("Join List Test ", ''.join(plain_arr))
client.connect(broker,port)
# plain_now = plain_arr[1]
for i in range(len(plain_arr)):
data_now = payload_process(plain_arr[i])
cipher_arr.append(data_now)
print("Plain now : ", plain)
msg_data = b''.join(cipher_arr)
# send_data = my_ip+identifier+msg_data.decode('ascii')
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
print("Python LOG Plain Length : ", len(plain))
print("Sending Cipher with length ", len(send_data))
print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
client.publish("device01/msg", send_data)
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,100 @@
import paho.mqtt.client as paho
#from simplecrypt import encrypt, decrypt
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
# Konfigurasi MQTT server
broker = "localhost"
client = paho.Client()
port = 1883
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log, C Return : ", c_return)
_ctypes.dlclose(u_enc._handle)
def u_decrypt(arg):
#arg = base64.b64decode(arg)
so_file2 = './URG_decrypt.so'
arg_len = len(arg)
arg = c_char_p(arg)
print("Python Log : Current Cipher ", arg)
print("Python Log : Length to be Dec ", arg_len)
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)
return c_return
def file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
return f.read()
#Proses enkripsi
def payload_process(plain):
u_encrypt(plain)
plain = file_handler("cipher_EES401", 7)
print("Cipher Length : ", len(plain))
send_data = base64.b64encode(plain)
return send_data
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
plain = input("Enter The Message : ")
plain_ori = plain
# plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
x = 16 - (len(plain)%16)
plain = plain+(" "*x)
plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
# print("Join List Test ", ''.join(plain_arr))
client.connect(broker,port)
# plain_now = plain_arr[1]
for i in range(len(plain_arr)):
print(" ")
print("===== [ Sequence "+str(i)+" ] "+"======================================================")
data_now = payload_process(plain_arr[i])
cipher_arr.append(data_now)
msg_data = b''.join(cipher_arr)
# send_data = my_ip+identifier+msg_data.decode('ascii')
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
print(" ")
print("===== Result ======================================================")
print("Python LOG Plain Length : ", len(plain_ori))
print("Python LOG Plain with Padding Length : ", len(plain))
print("Sending Cipher with length : ", len(send_data))
print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
client.publish("device01/msg", send_data)
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,156 @@
import paho.mqtt.client as paho
import paho.mqtt.publish as pahopub
#from simplecrypt import encrypt, decrypt
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
import time
import csv
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as Cipher
# Konfigurasi MQTT server
broker = "nnag.xyz"
client = paho.Client()
port = 1883
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log, C Return : ", c_return)
# time.sleep(0.01)
_ctypes.dlclose(u_enc._handle)
return c_return
def rsa_encrypt(plain):
rsa3072 = open("RSA3072_pub.pem", "r").read()
start=time.perf_counter()
rsa_pub = RSA.importKey(rsa3072)
rsa_pub = Cipher.new(rsa_pub)
RSA_enc = rsa_pub.encrypt(plain.encode('utf8'))
# RSA_enc = rsa_pub.encrypt(plain)
#RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
cipher = RSA_enc
# cipher = str(cipher).encode('utf-8')
# rsa_file_handler(base_name_RSA3, i, cipher)
print("RSA Encryption Time : ", end)
print("Cipher ", cipher)
print("Length ", len(cipher))
print(" ")
return end, cipher
def file_handler(f_name, num):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f = open("/tmp/cipher_07.dat", "rb")
ret = f.read()
f.close()
return ret
# def rsa_file_handler(f_name, num, cipher):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
# f.write(cipher)
#Proses enkripsi
def payload_process(plain):
dec_time = u_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
cipher = file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_RSA(plain):
dec_time, cipher = rsa_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
# cipher = rsa_file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
def main(msg):
# plain = input("Enter The Message : ")
plain = msg
plain_ori = plain
# plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
x = 16 - (len(plain)%16)
plain = plain+(" "*x)
plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
# print("Join List Test ", ''.join(plain_arr))
# client.tls_set()
# client.username_pw_set(username="aaa", password="pass")
status = client.connect(broker,port)
print("Connection status, ", status)
# input()
# plain_now = plain_arr[1]
time_tmp = []
for i in range(len(plain_arr)):
# print(" ")
# print("===== [ Sequence "+str(i)+" ] "+"======================================================")
# data_now, dec_time = payload_process(plain_arr[i])
data_now, dec_time = payload_RSA(plain_arr[i])
cipher_arr.append(data_now)
time_tmp.append(dec_time)
dec_acc = sum(map(float, time_tmp))
xxx = (base64.b64decode(cipher_arr[0]))
print(len(xxx))
print(len(data_now))
msg_data = b''.join(cipher_arr)
# send_data = my_ip+identifier+msg_data.decode('ascii')
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
# print(" ")
# print("===== Result ======================================================")
# print("Python LOG Plain Length : ", len(plain_ori))
# print("Python LOG Plain with Padding Length : ", len(plain))
# print("Sending Cipher with length : ", len(send_data))
# print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
# client.publish("device01/msg", "XXXXX")
print(len(send_data));
client.loop_start()
# client.connect("nnag.xyz", 1883)
# pahopub.single("device01/msg", "xxxx")
client.publish("device01/msg", msg_data)
return dec_acc
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,224 @@
import paho.mqtt.client as paho
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import paho.mqtt.publish as pahopub
#from simplecrypt import encrypt, decrypt
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
import time
import csv
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as Cipher
# Konfigurasi MQTT server
broker = "nnag.xyz"
client = paho.Client()
port = 1883
aes_key = "1234123412341234"
IV = 16*"\x00"
def fernet_keygen():
password="urg123"
pass_byte=password.encode()
salt=b'salt_'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(pass_byte))
return key
def fernet_encrypt(plain):
time_s = time.perf_counter()
fernet_key = fernet_keygen()
send_data = Fernet(fernet_key).encrypt(plain.encode())
time_e = time.perf_counter()
end = time_e - time_s
print("Fernet Plain Length : ",len(plain))
print("Fernet Key Length : ",len(fernet_key))
print("Fernet Encryption Time : ",end)
print(" ")
return end, send_data
def aes_encrypt(plain):
start = time.perf_counter()
key = AES.new(aes_key.encode("utf-8"), AES.MODE_CBC, IV=IV.encode("utf-8"))
# cipher = key.encrypt(plain.encode("utf-8"))
# plain = "x"*16
print(len(plain))
x = 16 - (len(plain) % 16)
plain = plain+" "*x
print(x)
# input()
cipher = key.encrypt(plain.encode("utf-8"))
end = time.perf_counter()-start
return end, cipher
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log, C Return : ", c_return)
# time.sleep(0.01)
_ctypes.dlclose(u_enc._handle)
return c_return
def rsa_encrypt(plain):
rsa3072 = open("RSA7680_pub.pem", "r").read()
start=time.perf_counter()
rsa_pub = RSA.importKey(rsa3072)
rsa_pub = Cipher.new(rsa_pub)
RSA_enc = rsa_pub.encrypt(plain.encode('utf8'))
# RSA_enc = rsa_pub.encrypt(plain)
#RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
cipher = RSA_enc
# cipher = str(cipher).encode('utf-8')
# rsa_file_handler(base_name_RSA3, i, cipher)
print("RSA Encryption Time : ", end)
print("Cipher ", cipher)
print("Length ", len(cipher))
print(" ")
return end, cipher
def file_handler(f_name, num):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f = open("/tmp/cipher_07.dat", "rb")
ret = f.read()
f.close()
return ret
# def rsa_file_handler(f_name, num, cipher):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
# f.write(cipher)
#Proses enkripsi
def payload_process(plain):
dec_time = u_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
cipher = file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_aes(plain):
dec_time, cipher = aes_encrypt(plain)
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_fernet(plain):
dec_time, cipher = fernet_encrypt(plain)
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_RSA(plain):
dec_time, cipher = rsa_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
# cipher = rsa_file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
def main(msg):
# plain = input("Enter The Message : ")
plain = msg
plain_ori = plain
# plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
# x = 16 - (len(plain)%16)
# plain = plain+(" "*x)
# plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
# print("Join List Test ", ''.join(plain_arr))
# client.tls_set()
# client.username_pw_set(username="aaa", password="pass")
status = client.connect(broker,port)
print("Connection status, ", status)
# input()
# plain_now = plain_arr[1]
time_tmp = []
# ******************************8
# for i in range(len(plain_arr)):
# # print(" ")
# # print("===== [ Sequence "+str(i)+" ] "+"======================================================")
# # data_now, dec_time = payload_process(plain_arr[i])
# # data_now, dec_time = payload_RSA(plain_arr[i])
# data_now, dec_time = payload_fernet(plain_arr[i])
# cipher_arr.append(data_now)
# time_tmp.append(dec_time)
# dec_acc = sum(map(float, time_tmp))
# xxx = (base64.b64decode(cipher_arr[0]))
# print("cipher len : ",len(xxx))
# print("base64 cipher len : ",len(data_now))
# msg_data = b''.join(cipher_arr)
# ******************************8
# send_data = my_ip+identifier+msg_data.decode('ascii')
# msg_data, dec_time = payload_fernet(plain)
msg_data, dec_time = payload_aes(plain)
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
# print(" ")
# print("===== Result ======================================================")
# print("Python LOG Plain Length : ", len(plain_ori))
# print("Python LOG Plain with Padding Length : ", len(plain))
# print("Sending Cipher with length : ", len(send_data))
# print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
# client.publish("device01/msg", "XXXXX")
client.loop_start()
print("Total sent data ", len(send_data));
# client.connect("nnag.xyz", 1883)
# pahopub.single("device01/msg", "xxxx")
client.publish("device01/msg", msg_data)
# input()
return dec_time
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,199 @@
import paho.mqtt.client as paho
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import paho.mqtt.publish as pahopub
#from simplecrypt import encrypt, decrypt
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
import time
import csv
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as Cipher
# Konfigurasi MQTT server
broker = "nnag.xyz"
client = paho.Client()
port = 1883
def fernet_keygen():
password="urg123"
pass_byte=password.encode()
salt=b'salt_'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(pass_byte))
return key
def fernet_encrypt(plain):
time_s = time.perf_counter()
fernet_key = fernet_keygen()
send_data = Fernet(fernet_key).encrypt(plain.encode())
time_e = time.perf_counter()
end = time_e - time_s
print("Fernet Plain Length : ",len(plain))
print("Fernet Key Length : ",len(fernet_key))
print("Fernet Encryption Time : ",end)
print(" ")
return end, send_data
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log, C Return : ", c_return)
# time.sleep(0.01)
_ctypes.dlclose(u_enc._handle)
return c_return
def rsa_encrypt(plain):
rsa3072 = open("RSA7680_pub.pem", "r").read()
start=time.perf_counter()
rsa_pub = RSA.importKey(rsa3072)
rsa_pub = Cipher.new(rsa_pub)
RSA_enc = rsa_pub.encrypt(plain.encode('utf8'))
# RSA_enc = rsa_pub.encrypt(plain)
#RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
cipher = RSA_enc
# cipher = str(cipher).encode('utf-8')
# rsa_file_handler(base_name_RSA3, i, cipher)
print("RSA Encryption Time : ", end)
print("Cipher ", cipher)
print("Length ", len(cipher))
print(" ")
return end, cipher
def file_handler(f_name, num):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f = open("/tmp/cipher_07.dat", "rb")
ret = f.read()
f.close()
return ret
# def rsa_file_handler(f_name, num, cipher):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
# f.write(cipher)
#Proses enkripsi
def payload_process(plain):
dec_time = u_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
cipher = file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_fernet(plain):
dec_time, cipher = fernet_encrypt(plain)
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_RSA(plain):
dec_time, cipher = rsa_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
# cipher = rsa_file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
def main(msg):
# plain = input("Enter The Message : ")
plain = msg
plain_ori = plain
# plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
# x = 16 - (len(plain)%16)
# plain = plain+(" "*x)
# plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
# print("Join List Test ", ''.join(plain_arr))
# client.tls_set()
# client.username_pw_set(username="aaa", password="pass")
status = client.connect(broker,port)
print("Connection status, ", status)
# input()
# plain_now = plain_arr[1]
time_tmp = []
# ******************************8
# for i in range(len(plain_arr)):
# # print(" ")
# # print("===== [ Sequence "+str(i)+" ] "+"======================================================")
# # data_now, dec_time = payload_process(plain_arr[i])
# # data_now, dec_time = payload_RSA(plain_arr[i])
# data_now, dec_time = payload_fernet(plain_arr[i])
# cipher_arr.append(data_now)
# time_tmp.append(dec_time)
# dec_acc = sum(map(float, time_tmp))
# xxx = (base64.b64decode(cipher_arr[0]))
# print("cipher len : ",len(xxx))
# print("base64 cipher len : ",len(data_now))
# msg_data = b''.join(cipher_arr)
# ******************************8
# send_data = my_ip+identifier+msg_data.decode('ascii')
msg_data, dec_time = payload_fernet(plain)
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
# print(" ")
# print("===== Result ======================================================")
# print("Python LOG Plain Length : ", len(plain_ori))
# print("Python LOG Plain with Padding Length : ", len(plain))
# print("Sending Cipher with length : ", len(send_data))
# print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
# client.publish("device01/msg", "XXXXX")
client.loop_start()
print("Total sent data ", len(send_data));
# client.connect("nnag.xyz", 1883)
# pahopub.single("device01/msg", "xxxx")
client.publish("device01/msg", msg_data)
# input()
return dec_time
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,161 @@
import paho.mqtt.client as paho
import paho.mqtt.publish as pahopub
#from simplecrypt import encrypt, decrypt
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
import time
import csv
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as Cipher
from SABER_KEM.test.so_handler import main as sec_pub
# Konfigurasi MQTT server
broker = "192.168.1.17"
client = paho.Client()
port = 1883
def u_encrypt(arg):
sce_pub(arg)
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log, C Return : ", c_return)
# time.sleep(0.01)
_ctypes.dlclose(u_enc._handle)
return c_return
def rsa_encrypt(plain):
rsa3072 = open("RSA7680_pub.pem", "r").read()
start=time.perf_counter()
rsa_pub = RSA.importKey(rsa3072)
rsa_pub = Cipher.new(rsa_pub)
RSA_enc = rsa_pub.encrypt(plain.encode('utf8'))
# RSA_enc = rsa_pub.encrypt(plain)
#RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
cipher = RSA_enc
# cipher = str(cipher).encode('utf-8')
# rsa_file_handler(base_name_RSA3, i, cipher)
print("RSA Encryption Time : ", end)
print("Cipher ", cipher)
print("Length ", len(cipher))
print(" ")
return end, cipher
def file_handler(f_name, num):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f = open("/tmp/cipher_07.dat", "rb")
ret = f.read()
f.close()
return ret
# def rsa_file_handler(f_name, num, cipher):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
# f.write(cipher)
#Proses enkripsi
def payload_process(plain):
dec_time = u_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
cipher = file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_RSA(plain):
dec_time, cipher = rsa_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
# cipher = rsa_file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
def main(msg):
# plain = input("Enter The Message : ")
plain = msg
plain_ori = plain
# plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
x = 16 - (len(plain)%16)
plain = plain+(" "*x)
plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
# print("Join List Test ", ''.join(plain_arr))
# client.tls_set()
# client.username_pw_set(username="aaa", password="pass")
status = client.connect(broker,port)
print("Connection status, ", status)
# input()
# plain_now = plain_arr[1]
time_tmp = []
for i in range(len(plain_arr)):
# print(" ")
# print("===== [ Sequence "+str(i)+" ] "+"======================================================")
data_now, dec_time = payload_process(plain_arr[i])
# data_now, dec_time = payload_RSA(plain_arr[i])
cipher_arr.append(data_now)
time_tmp.append(dec_time)
dec_acc = sum(map(float, time_tmp))
xxx = (base64.b64decode(cipher_arr[0]))
print(len(xxx))
print(len(data_now))
msg_data = b''.join(cipher_arr)
# send_data = my_ip+identifier+msg_data.decode('ascii')
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
# print(" ")
# print("===== Result ======================================================")
# print("Python LOG Plain Length : ", len(plain_ori))
# print("Python LOG Plain with Padding Length : ", len(plain))
# print("Sending Cipher with length : ", len(send_data))
# print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
# client.publish("device01/msg", "XXXXX")
client.loop_start()
print(len(send_data));
# client.connect("nnag.xyz", 1883)
# pahopub.single("device01/msg", "xxxx")
# client.publish("device01/msg", msg)
client.publish("device01/msg", msg_data)
return dec_acc
# #main("AAA")
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,155 @@
import paho.mqtt.client as paho
import paho.mqtt.publish as pahopub
#from simplecrypt import encrypt, decrypt
import base64
from ctypes import *
import _ctypes
from textwrap import wrap
import time
import csv
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as Cipher
# Konfigurasi MQTT server
broker = "nnag.xyz"
client = paho.Client()
port = 1883
def u_encrypt(arg):
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = c_char_p
str_temp = "07"
c_return = u_enc.main(arg.encode('utf-8'), str_temp.encode('utf-8'))
print("Python Log, C Return : ", c_return)
# time.sleep(0.01)
_ctypes.dlclose(u_enc._handle)
return c_return
def rsa_encrypt(plain):
rsa3072 = open("RSA7680_pub.pem", "r").read()
start=time.perf_counter()
rsa_pub = RSA.importKey(rsa3072)
rsa_pub = Cipher.new(rsa_pub)
RSA_enc = rsa_pub.encrypt(plain.encode('utf8'))
# RSA_enc = rsa_pub.encrypt(plain)
#RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
cipher = RSA_enc
# cipher = str(cipher).encode('utf-8')
# rsa_file_handler(base_name_RSA3, i, cipher)
print("RSA Encryption Time : ", end)
print("Cipher ", cipher)
print("Length ", len(cipher))
print(" ")
return end, cipher
def file_handler(f_name, num):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
f = open("/tmp/cipher_07.dat", "rb")
ret = f.read()
f.close()
return ret
# def rsa_file_handler(f_name, num, cipher):
# num = str(num).zfill(2)
# f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
# f.write(cipher)
#Proses enkripsi
def payload_process(plain):
dec_time = u_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
cipher = file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def payload_RSA(plain):
dec_time, cipher = rsa_encrypt(plain)
# cipher = u_encrypt(plain)
# time.sleep(0.05)
# cipher = rsa_file_handler("cipher_EES401", 7)
# print("Cipher Length : ", len(plain))
send_data = base64.b64encode(cipher)
return send_data, dec_time
def plain_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
# Add this to test the Blacklist feature
# blacklist_trigger = base64.b64encode(b"I'm gonna make this program crash!"*10)
# cipher_arr.append(blacklist_trigger)
# msg_data = blacklist_trigger
# ======================== || START FROM HERE BUDDY || ==================
def main(msg):
# plain = input("Enter The Message : ")
plain = msg
plain_ori = plain
# plain = plain_handler("plain", i)
i = 0
my_ip = "device01"
identifier = "&"
f_name = "cipher_EES401"
plain_arr = []
cipher_arr = []
# plain = input("Enter plain : ")
# plain = "This message is longer than 4 char"
x = 16 - (len(plain)%16)
plain = plain+(" "*x)
plain_arr = wrap(plain, 16, replace_whitespace=False, drop_whitespace=False)
# print("Join List Test ", ''.join(plain_arr))
# client.tls_set()
# client.username_pw_set(username="aaa", password="pass")
status = client.connect(broker,port)
print("Connection status, ", status)
# input()
# plain_now = plain_arr[1]
time_tmp = []
for i in range(len(plain_arr)):
# print(" ")
# print("===== [ Sequence "+str(i)+" ] "+"======================================================")
# data_now, dec_time = payload_process(plain_arr[i])
data_now, dec_time = payload_RSA(plain_arr[i])
cipher_arr.append(data_now)
time_tmp.append(dec_time)
dec_acc = sum(map(float, time_tmp))
xxx = (base64.b64decode(cipher_arr[0]))
print(len(xxx))
print(len(data_now))
msg_data = b''.join(cipher_arr)
# send_data = my_ip+identifier+msg_data.decode('ascii')
send_data = msg_data.decode('ascii')
# print("Python LOG Plain : ", plain)
# print(" ")
# print("===== Result ======================================================")
# print("Python LOG Plain Length : ", len(plain_ori))
# print("Python LOG Plain with Padding Length : ", len(plain))
# print("Sending Cipher with length : ", len(send_data))
# print("Python LOG Plain Array : ", plain_arr)
# print("Python LOG Cipher Length : ", len(send_data))
# client.publish("device01/msg", "XXXXX")
client.loop_start()
print(len(send_data));
# client.connect("nnag.xyz", 1883)
# pahopub.single("device01/msg", "xxxx")
client.publish("device01/msg", msg_data)
return dec_acc
# test_str = "Test String"
# client.publish("device01/msg", base64.b64encode(test_str.encode("utf-8")) )

View File

@@ -0,0 +1,448 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkFileFilter" id="filefilter1"/>
<object class="GtkWindow" id="windowMain">
<property name="can_focus">False</property>
<property name="vexpand">True</property>
<property name="default_width">400</property>
<property name="default_height">400</property>
<property name="icon_name">dialog-password</property>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">NTRU ENCRYPT</property>
<child>
<object class="GtkButton" id="close_window">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="on_close_window_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">dialog-close</property>
</object>
</child>
</object>
<packing>
<property name="pack_type">end</property>
</packing>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<property name="baseline_position">bottom</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="column_spacing">20</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Key File </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkFileChooserButton" id="openfile">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="filter">filefilter1</property>
<property name="title" translatable="yes"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkCheckButton" id="enc_401">
<property name="label" translatable="yes">NTRU EES 401</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="focus_on_click">False</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="enc_m01" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="enc_593">
<property name="label" translatable="yes">NTRU EES 593</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="focus_on_click">False</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="enc_m02" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Encryption Method</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkEntry" id="message">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Enter Message : </property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="vexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTextView" id="result_text">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">False</property>
<property name="wrap_mode">word-char</property>
<property name="indent">3</property>
<property name="cursor_visible">False</property>
<property name="accepts_tab">False</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Encrypted Message Content : </property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Message Length</property>
<property name="justify">fill</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="msg_arr">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="msg_len">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Message Array</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Message Details</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">6</property>
</packing>
</child>
<child>
<object class="GtkButton" id="msg">
<property name="label" translatable="yes">Encrypt</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_msg_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">7</property>
</packing>
</child>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Cipher Size</property>
<property name="justify">fill</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="enc_time">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="enc_size">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Encryption Sequence</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="enc_seq">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Encryption Time </property>
<property name="xalign">0</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Encryption Details</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">8</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkFileFilter" id="filefilter2"/>
<object class="GtkTextBuffer" id="textbuffer1"/>
</interface>

View File

@@ -0,0 +1,158 @@
#!/usr/bin/env python3
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
broker = "192.168.2.16"
client = paho.Client()
port = 1883
# print("")
builder = Gtk.Builder()
builder.add_from_file("py_wrappers/Publisher.glade")
# File filter
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")
filterfilter1 = Gtk.FileFilter()
filterfilter1.set_name("Keys")
filterfilter1.add_pattern("*.pub")
key_file.add_filter(filterfilter1)
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
tmp_num = "001"
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(3)
f = open(".cipher/"+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
global max_block
# enc_box = "/home/nino/Documents/Papers/MQTT_2.0/Crypto/NTRUEncrypt_Benchmark/EES401/URG_encrypt.so"
enc_box = "./bin/enc_401.so"
cip_box = "ees401/cipher"
max_block = 60
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
global max_block
# enc_box = "/home/nino/Documents/Papers/MQTT_2.0/Crypto/NTRUEncrypt_Benchmark/EES593/URG_encrypt.so"
enc_box = "./bin/enc_593.so"
cip_box = "ees593/cipher"
max_block = 86
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)
print(max_block)
x = max_block - (len(plain)%max_block)
plain = plain+(" "*x)
plain_arr = wrap(plain, max_block, 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())
cipher_arr = b''.join(cipher_arr)
cipher_arr = cipher_arr.decode('ascii')
client.loop_start()
client.publish("device01/msg", cipher_arr)
client.loop_stop()
# print("Your File ",key_file.get_filename())
builder.connect_signals(mainWindowHandler())
Gtk.main()

View File

@@ -0,0 +1,117 @@
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()

View File

@@ -0,0 +1,95 @@
import paho.mqtt.client as paho
# from simplecrypt import encrypt, decrypt
from cryptography.fernet import Fernet
import base64
import _ctypes
from ctypes import *
from textwrap import wrap
# Konfigurasi MQTT server
broker = "nnag.xyz"
client = paho.Client()
port = 1883
def u_decrypt(arg):
print("Cipher Length : ", len(arg))
so_file2 = './EES593/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):
if len(msg) > 736:
msg_arr = wrap(msg, 736)
dec_msg_arr = []
for i in range(len(msg_arr)):
msg = base64.b64decode(msg_arr[i])
# print("=====[ Decryption Sequence "+str(i+1)+" ]=================")
dec_msg = u_decrypt(msg)
dec_msg_arr.append(dec_msg)
# print("C Return String : ", dec_msg )
final_msg = ''.join(dec_msg_arr)
else:
msg = base64.b64decode(msg)
final_msg = u_decrypt(msg)
return final_msg
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("====[ Result ]=======================")
print("Incomming Message Length : ", len(msg))
# print("Incomming Message Type : ", type(msg))
real_data = payload_process(msg)
# 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"
client.connect(broker,port)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()

View File

@@ -0,0 +1,31 @@
import random
import string
plain_length = []
def randstring(n):
letters = string.digits
return ''.join(random.choice(letters) for i in range(n))
def rand_int(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return random.randint(range_start, range_end)
i = 1
while i <=16 :
plain_length.append(i*1)
i = i + 1
base_name = "plain"
def file_handler(f_name, num, plain):
plain = str(plain)
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "w")
f.write(plain)
for i in plain_length:
plain = rand_int(i)
file_handler(base_name, i, plain)

View File

View File

@@ -0,0 +1,52 @@
from Crypto.Cipher import AES
import os
import binascii
import time
import hashlib
def aes(key,PLAIN):
# print("=====================================AES128")
# print("Length Key AES: ",len(key))
# key = '12345678901234567890123456789012'
IV = 16 * '\x00'
cipher = AES.new(key, AES.MODE_CBC, IV=IV)
msg =cipher.encrypt(PLAIN)
# haha="Let the Silence be with us all, through all the hard time we embrace"
# haha=haha.zfill(96)
# print(len(haha))# #
decipher = AES.new(key, AES.MODE_CBC,IV=IV)
# print(decipher.decrypt(msg))
hal=(binascii.hexlify(msg).decode('utf8'))
print("Length AES Cipher: ",len(hal))
return msg
def rsa(key2,PLAIN):
# print("=====================================RSA128"
# from Crypto.PublicKey import RSA
# key2=RSA.generate(3072)
start=time.time()
publickey=key2.publickey()
# enc=publickey.encrypt("Attack the Fortress at midnight ",0)
print(PLAIN)
enc=publickey.encrypt(PLAIN.encode('utf-8'),0)
o=(binascii.hexlify(enc[0])).decode('utf8')
print("Length RSA Cipher: ",len(o))
end=time.time()-start
return o, end
# b=binascii.unhexlify(o)
# if b==enc[0]:
# print("TRUE")
def ntru(key,PLAIN):
# print("=====================================NTRU")
with open("fortress","rb") as n:
d=n.read()
nn=(binascii.hexlify(d).decode('utf8'))
print("Length: ",len(nn))
# with open("aes128.txt","w") as aes:
# aes.write(msg.encode("hex"))
# with open("rsa128.txt","w") as rsa:
# rsa.write(o)
# with open("ntru128.txt","w") as ntru:
# ntru.write(nn)

View File

@@ -0,0 +1,55 @@
#!/bin/env python3
import paho.mqtt.client as paho
import csv
import pandas as pd
# from Publish_rciot_NTRU import main as sec_pub
## from SABER_KEM.test.so_handler import main as sec_pub
import time
broker = "192.168.1.17"
client = paho.Client()
port = 1883
with open('data/rciot_data.csv') as f: # 1 Fasa
# with open('./file.csv') as f: # 3 Fasa
data=[tuple(line) for line in csv.reader(f)]
# data = pd.DataFrame()
print(data[0])
header = ["Message Length", "Encryption Time"]
with open("data/saber_enc_time.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(header)
def publish_enc():
x = 0
for i in data[10:500]:
print("===> : "+str(x))
dec_time = sec_pub(str(i))
with open("data/saber_enc_time.csv", "a+") as f:
writer = csv.writer(f)
writer.writerow([len(str(i)), dec_time])
x = x+1
time.sleep(0.5)
def publish_nonenc():
x = 0
for i in data[10:500]:
print("===> : "+str(x))
status = client.connect(broker,port)
client.loop_start()
client.publish("device01/msg", str(i))
# with open("saber_enc_time.csv", "a+") as f:
# writer = csv.writer(f)
# writer.writerow([len(str(i)), dec_time])
x = x+1
time.sleep(0.5)
# publish_enc()
for i in data[1:5]:
print(str(i))

View File

@@ -0,0 +1,68 @@
from ctypes import *
import time
import _ctypes
def u_encrypt(message, rec,x):
so_file = 'bin/enc_'+x+'.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
c_return = u_enc.main(message.encode('utf-8'), rec.encode('utf-8'))
_ctypes.dlclose(u_enc._handle)
return c_return
def u_decrypt(arg,x):
so_file2 = 'bin/dec_'+x+'.so'
arg_len = len(arg)
print("Length ", arg_len)
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("C Return ",c_return)
return c_return
def in_test():
plain = input("Enter plain text : ")
result = u_encrypt(plain)
# u_decrypt()
print(result)
def rapid_enc(x):
print("input len : %d" % len(plain))
u_encrypt(plain, x)
def rapid_dec(x):
u_decrypt(x)
def file_handler(f_name, num, std):
num = str(num).zfill(2)
f = open(".cipher/ees"+std+"/"+f_name+"_"+num+".dat", "rb")
cipher = f.read()
print("Python log : cipher len ",len(cipher))
return cipher
# print("C decrypt time : ", u_decrypt())
# print("C Enc : ", u_encrypt("ADADADADA"))
def dec_test(std, rpt):
for i in range(rpt):
recs = str(i).zfill(3)
# rapid_enc(recs)
args = file_handler("cipher", recs, std)
u_decrypt(args, std)
print("-"*100)
def enc_test(std, rpt):
for i in range(rpt):
recs = str(i).zfill(3)
plain = "This is Legit "+recs+" "+std
u_encrypt(plain, recs, std)
print("-"*100)
def enc_dec_test(x, rpt):
enc_test(x, rpt)
dec_test(x, rpt)

View File

@@ -0,0 +1,205 @@
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from matplotlib.pyplot import rcParams
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from Crypto.Cipher import AES
import base64
import csv
import struct
import time
import string
import random
import aes
import numpy as np
from ctypes import *
import _ctypes
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import os
#from multiprocessing import Pool
def u_decrypt(arg):
so_file2 = 'EES401/URG_decrypt.so'
arg_len = len(arg)
print("Length ", arg_len)
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("C Return ",c_return)
return c_return
def u_decrypt593(arg):
so_file2 = 'EES593/URG_decrypt.so'
arg_len = len(arg)
print("Length ", arg_len)
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("C Return ",c_return)
return c_return
def Fernet_process():
password="urg123"
pass_byte=password.encode()
salt=b'salt_'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(pass_byte))
return key
def file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./cipher_bak/"+f_name+"_"+num+".dat", "rb")
#file_val = list(f.read())
#file_val = bytearray(file_val)
file_val = f.read()
return file_val
def rsa_file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "rb")
return f.read()
NTRU_var = []
NTRU593_var = []
RSA_var = []
RSA2_var = []
AES_var = []
Fernet_var = []
plain_length = []
aes_key = "1234123412341234"
IV = 16 * '\x00'
base_name = "cipher_EES401"
base_name_593 = "cipher_EES593"
base_name_RSA3 = "RSA_3072"
base_name_RSA7 = "RSA_7680"
base_name_aes = "AES_128"
base_name_Fernet = "Fernet_128"
i = 1
while i <= 16:
plain_length.append(i)
i = i+1
for i in plain_length:
plain = rsa_file_handler(base_name, i)
plain2 = rsa_file_handler(base_name_593, i)
fernet_key = Fernet_process()
#print("Cip now : ", plain)
start = time.perf_counter()
print("NTRU PLAIN ",u_decrypt(plain))
end=time.perf_counter()-start
NTRU_var.append(end)
start=time.perf_counter()
print("NTRU PLAIN 593 ",u_decrypt593(plain2))
end=time.perf_counter()-start
NTRU593_var.append(end)
fernet_cipher = rsa_file_handler(base_name_Fernet, i)
time_s = time.perf_counter()
Fernet_data = Fernet(fernet_key).decrypt(fernet_cipher)
time_e = time.perf_counter()
end = time_e - time_s
Fernet_var.append(time_e - time_s)
print("Fernet Result : ", Fernet_data)
print("Fernet Cipher Length : ",len(fernet_cipher))
print("Fernet Decryption Time : ",end)
print(" ")
aes_cipher = rsa_file_handler(base_name_aes, i)
start=time.perf_counter()
AES128= AES.new(aes_key, AES.MODE_CBC, IV).decrypt(aes_cipher)
end=time.perf_counter()-start
AES_var.append(end)
print("AES Result : ", AES128.decode('ascii'))
print("AES128 Cipher Length : ",len(aes_cipher))
print("AES128 Decryption Time : ",end)
print(" ")
rsa3072 = open("RSA3072_priv.pem", "r").read()
start=time.perf_counter()
rsa_priv = RSA.importKey(rsa3072)
RSA_dec = rsa_priv.decrypt(rsa_file_handler(base_name_RSA3, i))
end=time.perf_counter()-start
RSA_var.append(end)
print("3072 Result : ", RSA_dec)
print("RSA Cipher length", len(plain))
print("RSA Decryption Time : ", end)
print(" ")
rsa7680 = open("RSA7680_priv.pem", "r").read()
start=time.perf_counter()
RSA2_priv = RSA.importKey(rsa7680)
RSA2_dec = RSA2_priv.decrypt(rsa_file_handler(base_name_RSA3, i))
end=time.perf_counter()-start
RSA2_var.append(end)
print("7680 Result : ", RSA_dec)
print("RSA2 Plain length", len(plain))
print("RSA2 Encryption Time : ", end)
print(" ")
NTRU_var = [ round(i * 1000, 3) for i in NTRU_var ]
NTRU593_var = [ round(i * 1000, 3) for i in NTRU593_var ]
RSA_var = [ round(i * 1000, 3) for i in RSA_var ]
RSA2_var = [ round(i * 1000, 3) for i in RSA2_var ]
AES_var = [ round(i * 1000, 3) for i in AES_var ]
Fernet_var = [ round(i * 1000, 3) for i in Fernet_var ]
header = ["NTRU 401","NTRU 593", "RSA_2048", "RSA_7680", "AES128", "Fernet128"]
all_time = zip(NTRU_var, NTRU593_var, RSA_var, RSA2_var, AES_var, Fernet_var)
with open("Benchmark_Decryption_Time.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(header)
for row in all_time:
writer.writerow(row)
print(NTRU_var)
print(AES_var)
y =np.array(plain_length)
plt.figure(figsize=(11,5))
rcParams['legend.fontsize'] = '50'
rcParams['font.family'] = 'sans-serif'
rcParams['font.size'] = '16'
plt.scatter(y ,AES_var ,marker="x" ,label='AES 128' )
plt.scatter(y ,Fernet_var ,marker="^" ,label='Fernet 128')
plt.scatter(y ,RSA_var ,marker="P" ,label='RSA 2048' )
plt.scatter(y ,RSA2_var ,marker="d" ,label='RSA 7680' )
plt.scatter(y ,NTRU_var ,marker="v" ,label='NTRU 401' )
plt.scatter(y ,NTRU593_var ,marker="o" ,label='NTRU 593' )
plt.plot(y, AES_var, linestyle=':')
plt.plot(y, Fernet_var, linestyle=':')
plt.plot(y, RSA_var, linestyle=':')
plt.plot(y, RSA2_var, linestyle=':')
plt.plot(y, NTRU_var, linestyle=':')
plt.plot(y, NTRU593_var, linestyle=':')
plt.xticks(list(range(1,17)))
plt.xlabel("Length Plain text")
plt.ylabel("Time (ms)")
plt.yscale('log')
plt.gca().set_yticks([0.03, 1.90], minor=True)
plt.legend(bbox_to_anchor=(1.04,1), loc="upper left", fontsize='14')
plt.subplots_adjust(right=0.8)
plt.tight_layout()
plt.savefig('time_comparsion_DEC.eps', format='eps', dpi=900)

View File

@@ -0,0 +1,221 @@
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from matplotlib.pyplot import rcParams
from matplotlib.ticker import FormatStrFormatter
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import numpy as np
import base64
import time
import string
import random
import aes
import numpy as np
from ctypes import *
import _ctypes
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import csv
#from multiprocessing import Pool
# C stuff
def u_encrypt(arg, num):
if len(str(num)) < 2:
num = str(num).zfill(2)
num = str(num)
so_file = 'EES401/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = [c_char_p, c_char_p]
c_return = u_enc.main(arg.encode('utf-8'), num.encode('utf-8'))
_ctypes.dlclose(u_enc._handle)
print("C Return ", c_return)
return c_return
def u_encrypt593(arg, num):
if len(str(num)) < 2:
num = str(num).zfill(2)
num = str(num)
so_file = 'EES593/URG_encrypt.so'
u_enc = CDLL(so_file)
u_enc.main.restype = c_double
u_enc.main.argtype = [c_char_p, c_char_p]
c_return = u_enc.main(arg.encode('utf-8'), num.encode('utf-8'))
_ctypes.dlclose(u_enc._handle)
print("C Return ", c_return)
return c_return
def Fernet_process():
password="urg123"
pass_byte=password.encode()
salt=b'salt_'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(pass_byte))
return key
def randString(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def file_handler(f_name, num):
num = str(num).zfill(2)
f = open("./plain/"+f_name+"_"+num+".txt", "r")
return f.read()
def rsa_file_handler(f_name, num, cipher):
num = str(num).zfill(2)
f = open("./cipher/"+f_name+"_"+num+".dat", "wb")
f.write(cipher)
NTRU_var = []
NTRU593_var = []
RSA_var = []
RSA2_var = []
AES_var = []
Fernet_var = []
aes_key = "1234123412341234"
base_name = "plain"
base_name_RSA3 = "RSA_3072"
base_name_RSA7 = "RSA_7680"
base_name_aes = "AES_128"
base_name_Fernet = "Fernet_128"
# i = 3
# while i <= 5 :
# plain_length.append(i*1)
# i = i + 1
n = input("Enter number of file ")
n = int(n)
for i in range(1, n+1):
plain = file_handler(base_name, i)
print("Plain now : ", plain)
fernet_key = Fernet_process()
x = 16 - len(plain)
aes_plain = plain + (" "*x)
NTRU_var.append(u_encrypt(plain, i))
print("NTRU EES401 Plain Length : ",len(plain))
print(" ")
NTRU593_var.append(u_encrypt593(plain, i))
print("NTRU EES593 Plain Length : ",len(plain))
print(" ")
time_s = time.perf_counter()
send_data = Fernet(fernet_key).encrypt(plain.encode())
rsa_file_handler(base_name_Fernet, i, send_data)
time_e = time.perf_counter()
end = time_e - time_s
Fernet_var.append(time_e - time_s)
print("Fernet Plain Length : ",len(plain))
print("Fernet Key Length : ",len(fernet_key))
print("Fernet Encryption Time : ",end)
print(" ")
start=time.perf_counter()
AES128=aes.aes(aes_key, aes_plain)
end=time.perf_counter()-start
rsa_file_handler(base_name_aes, i, AES128)
AES_var.append(end)
print("AES128 Plain Length : ",len(aes_plain))
print("AES128 Key Length : ",len(aes_key))
print("AES128 Encryption Time : ",end)
print(" ")
rsa3072 = open("RSA3072_pub.pem", "r").read()
start=time.perf_counter()
rsa_pub = RSA.importKey(rsa3072)
RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
#RSA_enc = rsa_pub.encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
cipher = RSA_enc[0]
#cipher = str(cipher).encode('utf-8')
rsa_file_handler(base_name_RSA3, i, cipher)
RSA_var.append(end)
print("RSA Plain length", len(plain))
print("RSA Key Length : ",len(str(rsa_pub.n)))
print("RSA Encryption Time : ", end)
print(" ")
rsa7680 = open("RSA7680_pub.pem", "r").read()
start=time.perf_counter()
RSA2_pub = RSA.importKey(rsa7680)
#RSA2_pub = RSA2_key.publickey().exportKey("PEM")
RSA2_enc = RSA2_pub.encrypt(plain.encode('utf8'), 32)
#RSA2_enc = PKCS1_OAEP.new(RSA2_pub).encrypt(plain.encode('utf8'), 32)
end=time.perf_counter()-start
#print("RSA Stufff ", RSA2_enc[0])
cipher = RSA2_enc[0]
#cipher = str(cipher).encode('utf-8')
rsa_file_handler(base_name_RSA7, i, cipher )
RSA2_var.append(end)
print("RSA2 Plain length", len(plain))
print("RSA2 Key Length : ",len(str(RSA2_pub.n.bit_length)))
print("RSA2 Encryption Time : ", end)
print(" ")
NTRU_var = [ round(i * 1000, 3) for i in NTRU_var ]
NTRU593_var = [ round(i * 1000, 3) for i in NTRU593_var ]
RSA_var = [ round(i * 1000, 3) for i in RSA_var ]
RSA2_var = [ round(i * 1000, 3) for i in RSA2_var ]
AES_var = [ round(i * 1000, 3) for i in AES_var ]
Fernet_var = [ round(i * 1000, 3) for i in Fernet_var ]
plain_length = range(1, n+1)
header = ["NTRU 401","NTRU 593", "RSA_2048", "RSA_7680", "AES128", "Fernet128"]
all_time = zip(NTRU_var, NTRU593_var, RSA_var, RSA2_var, AES_var, Fernet_var)
with open("Benchmark_Encryption_Time.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(header)
for row in all_time:
writer.writerow(row)
v = np.linspace(0, 0.5, 5)
y =np.array(plain_length)
print(NTRU_var)
print(AES_var)
plt.figure(figsize=(11,5))
rcParams['font.family'] = 'sans-serif'
rcParams['font.size'] = '16'
plt.scatter(y ,AES_var ,marker="x" ,label='AES 128' )
plt.scatter(y ,Fernet_var ,marker="^" ,label='Fernet 128')
plt.scatter(y ,RSA_var ,marker="P" ,label='RSA 2048' )
plt.scatter(y ,RSA2_var ,marker="d" ,label='RSA 7680' )
plt.scatter(y ,NTRU_var ,marker="v" ,label='NTRU 401' )
plt.scatter(y ,NTRU593_var ,marker="o" ,label='NTRU 593' )
plt.plot(y, AES_var, linestyle=':')
plt.plot(y, Fernet_var, linestyle=':')
plt.plot(y, RSA_var, linestyle=':')
plt.plot(y, RSA2_var, linestyle=':')
plt.plot(y, NTRU_var, linestyle=':')
plt.plot(y, NTRU593_var, linestyle=':')
plt.xticks(list(range(1,17)))
plt.xlabel("Length Plain text")
plt.ylabel("Time (ms)")
plt.yscale('log')
plt.gca().set_yticks([0.03, 1.90], minor=True)
plt.legend(bbox_to_anchor=(1.04,1), loc="upper left", fontsize='14')
plt.subplots_adjust(right=0.8)
plt.tight_layout()
plt.savefig('time_comparsion_ENC.eps', format='eps', dpi=900)
plt.show()