Add the cracking for S-DES.
continuous-integration/drone/push Build is passing Details

main
KKlochko 1 year ago
parent 919c28c301
commit 8cbd5d5c1f

@ -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()
Loading…
Cancel
Save