You need to sign in or sign up before continuing.
Commit f0a71ff8 authored by Almouhannad Hafez's avatar Almouhannad Hafez

Implement text_to_binary, binary_to_text

parent fe864afd
......@@ -31,15 +31,37 @@
"name": "stdout",
"output_type": "stream",
"text": [
"9\n",
"2\n"
"9\n"
]
}
],
"source": [
"sdes = SDES()\n",
"print(sdes.key_length)\n",
"print(sdes.rou)"
"print(sdes.key_length)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"168\n",
"Almouhannad.Hafez + 1\n",
"True\n"
]
}
],
"source": [
"original_text = \"Almouhannad.Hafez + 1\"\n",
"bin = sdes.text_to_binary(original_text)\n",
"print(len(bin))\n",
"text = sdes.binary_to_text(bin)\n",
"print(text)\n",
"print(text == original_text)\n"
]
}
],
......
......@@ -11,6 +11,7 @@ class SDES:
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
# You can change this default value from here directly, and it'll applied to the whole code
self.block_size = 12
# SDED uses a fixed-length key (8bits- key) for each iteration
......@@ -72,4 +73,71 @@ class SDES:
"""
# Generate a random binary string by selecting '0' or '1' for each position
key = ''.join(random.choice('01') for _ in range(self.key_length))
return key
\ No newline at end of file
return key
def text_to_binary(self, text: str) -> str:
"""
Convert plain text to a binary string
with needed padding to be with length divisible by self.block_size
Parameters:
text (str): Text to be converted
Returns:
binary (str): Binary representation of the text
"""
# Type checking
if not isinstance(text, str):
raise TypeError(f"'text' must be str, got {type(text).__name__}")
# Validation rules
if len(text) == 0:
raise ValueError("'text' must not be empty")
# Convert to binary
binary = ''.join(format(ord(c), '08b') for c in text)
# Handle padding to be with length divisible by self.block_size
binary_length = len(binary)
# Calculate how much padding is needed to make it divisible by self.block_size
padding_needed = (self.block_size - (binary_length % self.block_size)) % self.block_size
# Add padding
if padding_needed > 0:
binary += '0' * padding_needed
return binary
def binary_to_text(self, binary: str) -> str:
"""
Convert binary string to a plain text
Parameters:
binary (str): Binary data to be converted
Returns:
text (str): Plain text of the binary data
"""
# Type checking
if not isinstance(binary, str):
raise TypeError(f"'binary' must be str, got {type(binary).__name__}")
# Validation rules
if len(binary) == 0:
raise ValueError("'binary' must not be empty")
if len(binary) % self.block_size != 0:
raise ValueError(f"'binary' length must be a multiple of {self.block_size}")
if not all(c in '01' for c in binary):
raise ValueError("'binary' must only contain '0' and '1'")
# Get chars (each char is 8 bit)
chars = []
for i in range(0, len(binary), 8):
chunk = binary[i:i+8]
# Chack end of string
if chunk == '00000000':
break
chars.append(chunk)
# Convert to string plain text
text = ''.join(chr(int(char, 2)) for char in chars)
return text
\ No newline at end of file
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