From 8cbd5d5c1fd2aca63f99d660e76c2b4e667757bf Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 17 Dec 2023 11:35:40 +0200 Subject: [PATCH] Add the cracking for S-DES. --- cryptography_s_des_exploring/cracking.py | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cryptography_s_des_exploring/cracking.py diff --git a/cryptography_s_des_exploring/cracking.py b/cryptography_s_des_exploring/cracking.py new file mode 100644 index 0000000..39ada3f --- /dev/null +++ b/cryptography_s_des_exploring/cracking.py @@ -0,0 +1,56 @@ +import binascii +import time +from rich.console import Console +from rich.prompt import Prompt + +from cryptography_s_des_exploring.sdes.decrypt import decrypt_block + +from cryptography_s_des_exploring.sdes.blocks import decode, decode_bytes + + +def cast_to_key(number: int): + binary = bin(number)[2:] + return "0"*(10-len(binary)) + binary + + +def cracking(ciphertext): + founded = [] + + start = time.monotonic() + + for n in range(0, 1024): + key = cast_to_key(n) + + decoded = decode_bytes(ciphertext) + decrypted = list(map(lambda b: decrypt_block(key, b), decoded)) + + try: + decoded = decode(decrypted) + founded.append((key, decoded)) + except UnicodeDecodeError as e: + continue + except binascii.Error as e: + continue + + end = time.monotonic() + + return start, end, founded + + +def cli_cracking(): + console = Console() + ciphertext = Prompt.ask(f"[b]Enter the [yellow]ciphertext[/]") + + console.print(f"\n[cyan b]Cracking is started.[/]") + start, end, founded = cracking(ciphertext) + + console.print(f"\n[yellow b]Time spent: [/]{end-start}[yellow b] \[s][/]\n") + + console.print(f"[cyan b]Found keys and messages:[/]") + for key, decoded in founded: + console.print(f"\t[cyan b]Key: [/][yellow b]{key}[/][cyan b], text:[/] \"{decoded}\"[cyan b].[/]") + + +if __name__ == "__main__": + cli_cracking() +