Commit 63c4992b authored by Almouhannad Hafez's avatar Almouhannad Hafez

Implement get_blocks

parent f0a71ff8
......@@ -63,6 +63,39 @@
"print(text)\n",
"print(text == original_text)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['010000010110',\n",
" '110001101101',\n",
" '011011110111',\n",
" '010101101000',\n",
" '011000010110',\n",
" '111001101110',\n",
" '011000010110',\n",
" '010000101110',\n",
" '010010000110',\n",
" '000101100110',\n",
" '011001010111',\n",
" '101000100000',\n",
" '001010110010',\n",
" '000000110001']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sdes.get_blocks(original_text)"
]
}
],
"metadata": {
......
......@@ -140,4 +140,27 @@ class SDES:
# Convert to string plain text
text = ''.join(chr(int(char, 2)) for char in chars)
return text
\ No newline at end of file
return text
def get_blocks(self, plain_text: str) -> list[str]:
"""
Convert plain text into a list of binary strings, each with length of self.block_size
Parameters:
plain_text (str): Text to be converted
Returns:
blocks (list[str]): Binary blocks of the original text
"""
# Type checking
if not isinstance(plain_text, str):
raise TypeError(f"'plain_text' must be str, got {type(plain_text).__name__}")
# Validation rules
if len(plain_text) == 0:
raise ValueError("'plain_text' must not be empty")
binary_text = self.text_to_binary(plain_text)
blocks = []
blocks = [binary_text[i:i+self.block_size] for i in range(0, len(binary_text), self.block_size)]
return blocks
\ 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