Commit 0e050e1e authored by Almouhannad Hafez's avatar Almouhannad Hafez

Implement process_block, binary_strings_xor

parent 63c4992b
...@@ -11,7 +11,6 @@ class SDES: ...@@ -11,7 +11,6 @@ class SDES:
key_length (int): Length of keys that will be used for encryption/decryption key_length (int): Length of keys that will be used for encryption/decryption
""" """
# SDES is a block-cipher algorithm, uses blocks of size 12 bits # SDES is a block-cipher algorithm, uses blocks of size 12 bits
# You can change this default value from here directly, and it'll applied to the whole code
self.block_size = 12 self.block_size = 12
# SDED uses a fixed-length key (8bits- key) for each iteration # SDED uses a fixed-length key (8bits- key) for each iteration
...@@ -164,3 +163,22 @@ class SDES: ...@@ -164,3 +163,22 @@ class SDES:
blocks = [] blocks = []
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:
L_old, R_old = old_block[0:self.block_size/2, self.block_size/2, self.block_size]
L_new = R_old
round_key = self.get_round_key(key, round_number)
R_new = self.f(round_key, R_old)
R_new = self.binary_strings_xor(R_new, L_old)
new_block = ''
new_block += L_new
new_block += R_new
return new_block
def binary_strings_xor(self, binary_string_1: str, binary_string_2: str) -> str:
num1 = int(binary_string_1, 2)
num2 = int(binary_string_2, 2)
xor_result = num1 ^ num2
xor_result_str = bin(xor_result)[2:]
xor_result_str = xor_result_str.zfill(len(binary_string_1))
return xor_result_str
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