Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
maro5397 committed Jun 6, 2022
2 parents 699f9f1 + dfa4bdd commit 7685f9c
Show file tree
Hide file tree
Showing 16 changed files with 1,005 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/
test.p
report/*
20 changes: 20 additions & 0 deletions Documentation/RelNotes/v1.0.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
user_system v1.0.0 Release Note
=================================

This release user_system in cumulus v0.0.1

Introduction:
Darkweb monitoring service for Sejong univ. infosec major Capstone Design

Configuration:
python-smtp
pyqt5
socket
SimHash
Elgamal

Feature:
crypto and decrypt
compare simhash
sending email
create report file
416 changes: 416 additions & 0 deletions app/main.py

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
numpy==1.21.6
opencv-python==4.5.5.64
psutil==5.9.1
pycryptodome==3.14.1
PyQt5==5.15.6
PyQt5-Qt5==5.15.2
PyQt5-sip==12.10.1
7 changes: 7 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"SERVERIP": "20.214.226.173",
"SERVERPORT": 8080,
"MSGTO": "[email protected]",
"MSGFROM": "[email protected]",
"TOKEN": ""
}
84 changes: 84 additions & 0 deletions src/AdditiveElgamal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Crypto.PublicKey.ElGamal as elg
from Crypto.Util.number import getRandomRange
from os import urandom
from Crypto import Random
from Crypto.Math.Primality import ( generate_probable_safe_prime,
test_probable_prime, COMPOSITE )
from Crypto.Math.Numbers import Integer
import json


class AdditiveElgamalKey(elg.ElGamalKey):
"""Sub Class"""
def __init__(self,randfunc=None,N=64): # N: max bit of msg # added for additive HE
super().__init__(randfunc)
self.N=N

def _encrypt(self, M, K):
a=pow(self.g, K, self.p)
b=( pow(self.y, K, self.p) * pow(self.g, M, self.p) ) % self.p # added for additive HE
return [int(a), int(b)]

def _decrypt(self, M):
if (not hasattr(self,'lookup')):
self.lookup = {}
for i in range(self.N):
self.lookup[int(pow(self.g,i,self.p))] = i

if (not hasattr(self, 'x')):
raise TypeError('Private key not available in this object')

r = Integer.random_range(min_inclusive=2,
max_exclusive=self.p-1,
randfunc=self._randfunc)
a_blind = (pow(self.g, r, self.p) * M[0]) % self.p
ax=pow(a_blind, self.x, self.p)
plaintext_blind = (ax.inverse(self.p) * M[1] ) % self.p
plaintext = (plaintext_blind * pow(self.y, r, self.p)) % self.p
plaintext = self.lookup[int(plaintext)] # added for additive HE
return plaintext

def construct_additive(tup):
r"""Construct an ElGamal key from a tuple of valid ElGamal components.
The modulus *p* must be a prime.
The following conditions must apply:
.. math::
\begin{align}
&1 < g < p-1 \\
&g^{p-1} = 1 \text{ mod } 1 \\
&1 < x < p-1 \\
&g^x = y \text{ mod } p
\end{align}
Args:
tup (tuple):
A tuple with either 3 or 4 integers,
in the following order:
1. Modulus (*p*).
2. Generator (*g*).
3. Public key (*y*).
4. Private key (*x*). Optional.
Raises:
ValueError: when the key being imported fails the most basic ElGamal validity checks.
Returns:
an :class:`ElGamalKey` object
"""

obj=AdditiveElgamalKey()
if len(tup) not in [3,4]:
raise ValueError('argument for construct() wrong length')
for i in range(len(tup)):
field = obj._keydata[i]
setattr(obj, field, Integer(tup[i]))

fmt_error = test_probable_prime(obj.p) == COMPOSITE
fmt_error |= obj.g<=1 or obj.g>=obj.p
fmt_error |= pow(obj.g, obj.p-1, obj.p)!=1
fmt_error |= obj.y<1 or obj.y>=obj.p
if len(tup)==4:
fmt_error |= obj.x<=1 or obj.x>=obj.p
fmt_error |= pow(obj.g, obj.x, obj.p)!=obj.y

if fmt_error:
raise ValueError("Invalid ElGamal key components")

return obj
119 changes: 119 additions & 0 deletions src/compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from src import AdditiveElgamal as ae
import json
import pickle
from Crypto.Util.number import getRandomRange, long_to_bytes, bytes_to_long
from os import urandom
from sys import getsizeof


def int_to_binlist(x): #max bit num == 64
if x>=2**64:
raise ValueError('argument bigger than 2**64')
return [x >> i & 1 for i in range(0,64)]

def binlist_to_int(lst):
if len(lst)!=64:
raise ValueError('argument list length must be 64')
x = 0
for i in range(len(lst)):
x += 2**i * lst[i]
return x

def encrypt_simhash(pubkey, x): # x: 64bit simhash
if not isinstance(pubkey,ae.AdditiveElgamalKey):
raise ValueError('Argument pubkey must be ae.AdditiveElgamalKey instance')
if x>=2**64:
raise ValueError('argument x bigger than 2**64')

lst = int_to_binlist(x)
enc_lst = []
for i in range(64):
K = getRandomRange(0, pubkey.p-1, urandom)
enc_lst.append(pubkey._encrypt(lst[i],K))
return enc_lst

def get_enc_HD(pubkey, simhash, enc_server_simhash):
if not isinstance(pubkey,ae.AdditiveElgamalKey):
raise ValueError('Argument pubkey must be ae.AdditiveElgamalKey instance')

simhash_binlist = int_to_binlist(simhash)
enc_HD = [1,1]
for i in range(64):
if simhash_binlist[i]==0:
enc_HD = [ x*y for x,y in zip(enc_HD,enc_server_simhash[i])]
else:
K = getRandomRange(0, pubkey.p-1, urandom)
enc_1 = pubkey._encrypt(1,K)
inverse = [pow(x,-1,int(pubkey.p)) for x in enc_server_simhash[i]] #NEEDS TO BE PYTHON3.8+
enc_HD = [ x*y*z for x,y,z in zip(enc_HD,enc_1,inverse)]
enc_HD = [ x%int(pubkey.p) for x in enc_HD]

return enc_HD

def get_HD(simhash1,simhash2):
s1 = int_to_binlist(simhash1)
s2 = int_to_binlist(simhash2)
xor = [ x^y for x,y in zip(s1,s2) ]
return xor.count(1)

if __name__ == "__main__":
'''
privkey = ae.construct_additive((key_json['p'], key_json['g'], key_json['y'],key_json['x']))
pubkey = ae.construct_additive((key_json['p'], key_json['g'], key_json['y']))
server_simhash = getRandomRange(0, 2**64-1, urandom)
enc_server_simhash = encrypt_simhash(pubkey,server_simhash)
cli_simhash = getRandomRange(0, 2**64-1, urandom)
HD = get_HD(int_to_binlist(server_simhash),int_to_binlist(cli_simhash))
enc_HD = get_enc_HD(pubkey, cli_simhash, enc_server_simhash)
print(HD)
print(enc_HD)
print(privkey._decrypt(enc_HD))
'''

##### GET PUB KEY from server, .JSON from analyze.py
with open('key.json','r') as f: ## TO DO : get from server and store in pickle
key_json = json.load(f)

with open('../../system/test.p','rb') as f:
obj = pickle.load(f)

pubkey = ae.construct_additive((key_json['p'], key_json['g'], key_json['y']))


#GET ENCRYPTED SIMHASH AND INDEX FROM SERVER // in pickle!
server_idx = 2
server_simhash = getRandomRange(0, 2**64-1, urandom)
enc_server_simhash = encrypt_simhash(pubkey,server_simhash)



#Start Comparing process
enc_HD_dict = {} # k,v = reprisentative_id , enc_HD
for idx, val in obj.items():
if not (idx == server_idx): # JSON in STR , LATER USE PICKLE instead
continue
for reprisentative_cfid, cloneclass in val.items():
for cfid, code_fragment in cloneclass.items():
if not ( cfid == reprisentative_cfid):
continue
simhash = code_fragment[3]
enc_HD_dict[reprisentative_cfid] = get_enc_HD(pubkey,simhash,enc_server_simhash)
print('real HD :',get_HD(simhash,server_simhash))
print('enc HD added')
break

##### TEST
print(enc_HD_dict)

privkey = ae.construct_additive((key_json['p'], key_json['g'], key_json['y'],key_json['x']))

for _,enc_HD in enc_HD_dict.items():
print(privkey._decrypt(enc_HD))


##### TO DO : socket pickle : send enc_HD_dict to server
Binary file added src/found.mp4
Binary file not shown.
Binary file added src/highsimilarity.mp4
Binary file not shown.
17 changes: 17 additions & 0 deletions src/jsonmodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import json

secret_file = os.path.join("../", 'settings.json')
with open(secret_file, 'r') as f:
secrets = json.loads(f.read())

def get_secret(setting):
try:
return secrets[setting]
except KeyError:
error_msg = "Set the {} environment variable".format(setting)
raise ImproperlyConfigured(error_msg)

if __name__ == "__main__":
print(get_secret("STARTURL"))
print(get_secret("USERINFO")["ID"])
Binary file added src/lowsimilarity.mp4
Binary file not shown.
Loading

0 comments on commit 7685f9c

Please sign in to comment.