Commit eb75bf88 authored by Almouhannad Hafez's avatar Almouhannad Hafez

Implement f, E, S

parent 0e050e1e
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -96,6 +96,79 @@ ...@@ -96,6 +96,79 @@
"source": [ "source": [
"sdes.get_blocks(original_text)" "sdes.get_blocks(original_text)"
] ]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12434356\n"
]
}
],
"source": [
"print(sdes.E(\"123456\"))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"010\n"
]
}
],
"source": [
"print(sdes.apply_S(\"1101\", 2))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"011100100\n"
]
}
],
"source": [
"key = sdes.generate_random_key()\n",
"print(key)\n",
"##OK"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"000001111010\n"
]
}
],
"source": [
"block = \"111111000001\"\n",
"processed_block = sdes.process_block(key, 0, block)\n",
"print (processed_block)\n",
"##OK"
]
} }
], ],
"metadata": { "metadata": {
......
...@@ -16,6 +16,15 @@ class SDES: ...@@ -16,6 +16,15 @@ class SDES:
# SDED uses a fixed-length key (8bits- key) for each iteration # SDED uses a fixed-length key (8bits- key) for each iteration
self.round_key_length = 8 self.round_key_length = 8
# S1, S2 boxes:
self.S1 = [["101", "010", "001", "110", "011", "100", "111", "000"],
["001", "100", "110", "010", "000", "111", "101", "011"]]
self.S2 = [["100", "000", "110", "101", "111", "001", "011", "010"],
["101", "011", "000", "111", "110", "010", "001", "100"]]
# Type checking # Type checking
if not isinstance(rounds_count, int): if not isinstance(rounds_count, int):
raise TypeError(f"'rounds_count' must be int, got {type(rounds_count).__name__}") raise TypeError(f"'rounds_count' must be int, got {type(rounds_count).__name__}")
...@@ -164,8 +173,10 @@ class SDES: ...@@ -164,8 +173,10 @@ class SDES:
blocks = [binary_text[i:i+self.block_size] for i in range(0, len(binary_text), self.block_size)] blocks = [binary_text[i:i+self.block_size] for i in range(0, len(binary_text), self.block_size)]
return blocks return blocks
###############################################################################################
def process_block(self, key: str, round_number: int, old_block: str) -> str: def process_block(self, key: str, round_number: int, old_block: str) -> str:
L_old, R_old = old_block[0:self.block_size/2, self.block_size/2, self.block_size] L_old, R_old = old_block[0:self.block_size//2] , old_block[self.block_size//2 : self.block_size]
L_new = R_old L_new = R_old
round_key = self.get_round_key(key, round_number) round_key = self.get_round_key(key, round_number)
R_new = self.f(round_key, R_old) R_new = self.f(round_key, R_old)
...@@ -174,6 +185,35 @@ class SDES: ...@@ -174,6 +185,35 @@ class SDES:
new_block += L_new new_block += L_new
new_block += R_new new_block += R_new
return new_block return new_block
def f(self, key: str, R: str) -> str:
new_R = self.E(R)
new_R = self.binary_strings_xor(new_R, key)
new_R_L, new_R_R = new_R[0:4], new_R[4:8]
new_R_L = self.apply_S(new_R_L, 1)
new_R_R = self.apply_S(new_R_R, 2)
new_R = ''
new_R += new_R_L
new_R += new_R_R
return new_R
def E(self, R: str) -> str:
extended_R = ''
extended_R += (R[0] + R[1])
extended_R += R[3]
extended_R += (R[2] + R[3])
extended_R += R[2]
extended_R += (R[4] + R[5])
return extended_R
def apply_S(self, R: str, S_number: int) -> str:
row_index = int(R[0], 2)
col_index = int(R[1:4], 2)
if (S_number == 1):
return self.S1[row_index][col_index]
else:
return self.S2[row_index][col_index]
def binary_strings_xor(self, binary_string_1: str, binary_string_2: str) -> str: def binary_strings_xor(self, binary_string_1: str, binary_string_2: str) -> str:
num1 = int(binary_string_1, 2) num1 = int(binary_string_1, 2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment