Skip to content

Instantly share code, notes, and snippets.

View urwithajit9's full-sized avatar
🎯
Focusing on Blending AI & ML for Cyber Security and Malware Research

Ajit Kumar urwithajit9

🎯
Focusing on Blending AI & ML for Cyber Security and Malware Research
View GitHub Profile
def functionF(pre32bits, key48bits):
"""This is main function to perform function F """
result = ""
expanded_left_half = apply_Expansion(EXPANSION_TABLE,pre32bits)
xor_value = XOR(expanded_left_half,key48bits)
bits6list = split_in_6bits(xor_value)
for sboxcount, bits6 in enumerate(bits6list):
first_last = get_first_and_last_bit(bits6)
middle4 = get_middle_four_bit(bits6)
sboxvalue = sbox_lookup(sboxcount,first_last,middle4)
PERMUTATION_TABLE = [16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25]
def apply_Permutation(permutation_table,sbox_32bits):
""" It takes Sboxes output and a permutation table and return 32 bit binary string"""
final_32bits = ""
for index in permutation_table:
final_32bits += sbox_32bits[index-1]
return final_32bits
import textwrap
def split_in_6bits(XOR_48bits):
"""split 48 bits into 6 bits each """
list_of_6bits = textwrap.wrap(XOR_48bits,6)
return list_of_6bits
## Below are some supportive function used for smooth the process
def get_first_and_last_bit(bits6):
SBOX = [
# Box-1
[
[14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7],
[0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8],
[4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0],
[15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13]
],
# Box-2
def XOR(bits1,bits2):
"""perform a XOR operation and return the output"""
# Assuming both bit string of same length
xor_result = ""
for index in range(len(bits1)):
if bits1[index] == bits2[index]:
xor_result += '0'
else:
xor_result += '1'
return xor_result
EXPANSION_TABLE = [32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1]
def apply_Expansion(expansion_table,bits32):
""" This will take expansion table and 32-bit binary string as input and output a 48-bit binary stirng"""
bits48 = ""
for index in expansion_table:
bits48 += bits32[index-1]
return bits48
bits32 = '11110000101010101111000010101010'
def generate_keys(key_64bits):
round_keys = list()
pc1_out = apply_PC1(PC1,key_64bits)
L0,R0 = split_in_half(pc1_out)
for roundnumber in range(16):
newL = circular_left_shift(L0,round_shifts[roundnumber])
newR = circular_left_shift(R0,round_shifts[roundnumber])
roundkey = apply_PC2(PC2,newL+newR)
round_keys.append(roundkey)
L0 = newL
#def apply_compression(pc2_table,keys_56bits):
def apply_PC2(pc2_table,keys_56bits):
""" This will take Compression table and combined both half as input and return a 48-bits string as output"""
keys_48bits = ""
for index in pc2_table:
keys_48bits += keys_56bits[index-1]
return keys_48bits
PC2 = [14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2, 41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32]
#to call this function there must be left_half and right_half
#subkey = apply_compression(PC2,left_half + right_half)
def circular_left_shift(bits,numberofbits):
"""This method will circularly shift the given bit string according to the number of bits"""
shiftedbits = bits[numberofbits:] + bits[:numberofbits]
return shiftedbits
circularShiftedBits = circular_left_shift(bits,numberofbits)
def split_in_half(keys_56bits):
""" Split the 56 bits key into two equal half """
left_keys, right_keys = keys_56bits[:28],keys_56bits[28:]
return left_keys, right_keys
left56 , right56 = split_in_half(keys_56bits)