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 encode, 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() decoded_ciphertext = list(decode_bytes(ciphertext)) for n in range(0, 1024): key = cast_to_key(n) decrypted = list(map(lambda b: decrypt_block(key, b), decoded_ciphertext)) try: decoded_text = decode(decrypted) if len(decrypted) == len(list(encode(decoded_text))): founded.append((key, decoded_text)) 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()