Skip to content
Snippets Groups Projects
Commit 4a9a05a0 authored by vincent.steinman's avatar vincent.steinman
Browse files

Clean code

parent c6f5e99b
Branches
No related tags found
No related merge requests found
......@@ -18,6 +18,19 @@ def Mult_mod(a, b):
mod_value = (2 ** 16) + 1
return Mod(a * b, mod_value)
def opp_mod(a, n): # Return the opposite for the addition modulo n
return Mod(n - a, n)
def Invert_Mod(a):
m = (2**16) + 1
g = gcd(a, m)
if (g != 1) :
print("Inverse doesn't exist")
else :
return Fast_exp(a, m - 2, m)
def Fast_exp(b, e, m):
'''
Function of fast exponation
......@@ -44,7 +57,7 @@ def XOR(a, b):
b = bin(b)
return int(a,2) ^ int(b,2)
def Sub_str(s, p):
def Split_str(s, p): #Split a string
r = list()
for i in range(0, p):
r.append(s[(p*i) : (p*i) + p])
......@@ -59,19 +72,6 @@ def gcd(a,b):
else:
return gcd(b, a % b)
def opp_mod(a, n): # Return the opposite for the addition modulo n
return Mod(n - a, n)
def Invert_Mult(a):
m = (2**16) + 1
g = gcd(a, m)
if (g != 1) :
print("Inverse doesn't exist")
else :
return Fast_exp(a, m - 2, m)
def Round(lst_msg,lst_k):
step1 = Mult_mod(lst_msg[0],lst_k[0])
step2 = Add_mod(lst_msg[1],lst_k[1])
......@@ -108,7 +108,7 @@ def Final_Round(lst_msg,lst_k):
def encrypt_sub_key(key, nb, size, subkey_shift):
subkeys = []
for _ in range(ceil(len(key)/float(size))): #Ceil return ceiling value
subkeys.extend(Sub_str(key, size))
subkeys.extend(Split_str(key, size))
key = Shift(key)
return subkeys[:nb]
......@@ -117,7 +117,7 @@ def decrypt_sub_key(subkeys, nb_round, nb_key_round, n):
for i in range(nb_round + 1):
for j in range(nb_key_round):
if j == 0 or j == 3:
k[i*nb_key_round+j] = Invert_Mult(subkeys[(4-i)*nb_key_round+j])
k[i*nb_key_round+j] = Invert_Mod(subkeys[(4-i)*nb_key_round+j])
elif j == 1 or j == 2:
k[i*nb_key_round+j] = opp_mod(subkeys[(4-i)*nb_key_round+j], n)
elif i < nb_round:
......@@ -134,7 +134,7 @@ def idea(msg, k, size, nb_round, subkey_shift, decrypt):
# Split the message and convert it into int
padding = 2**size - Mod(len(msg), 2**size)
blocks = [int(x, 2) for x in Sub_str(msg + (padding * '0'), size)]
blocks = [int(x, 2) for x in Split_str(msg + (padding * '0'), size)]
lst_k = [int(x, 2) for x in encrypt_sub_key(k, (KEY_ROUND*nb_round+KEY_HALF_ROUND), size, subkey_shift)]
if decrypt:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment