Add the simple CLI interface.

main
KKlochko 1 year ago
parent 74fc05e62b
commit 2c13980f4c

@ -0,0 +1,95 @@
import random
from rich.console import Console
from rich.prompt import Confirm, Prompt, IntPrompt
from rich.markdown import Markdown
from cryptography_s_des_exploring.sdes.decrypt import decrypt
from cryptography_s_des_exploring.sdes.encrypt import encrypt
def yes_no_prompt(question: str) -> bool:
return Confirm.ask(question)
def cli_generate_keys(console):
KEY_LENGTH = 10
key = ''.join(random.choice('01') for _ in range(KEY_LENGTH))
console.print(f"[yellow b]The key:[/] {key}\n")
def cli_key_input(console):
while True:
key = Prompt.ask(f"[b]Enter the [yellow]key[/]", password=True)
if len(key) == 10 and len(key.replace('0', '').replace('1', '')) == 0:
return key
console.print(
"[red b][Error] THE KEY MUST BE 10 CHARACTERS LONG AND CONTAIN ONLY 1 OR 0 SYMBOLS!!![/]\n"
)
def cli_encrypt_message(console):
key = cli_key_input(console)
message = Prompt.ask(f"[b]Enter the [yellow]message[/]")
ciphertext = encrypt(key, message)
console.print(f"[yellow b]The ciphertext:[/] \"{ciphertext.decode('utf-8')}\"\n")
def cli_decrypt_message(console):
key = cli_key_input(console)
ciphertext = Prompt.ask(f"[b]Enter the [yellow]ciphertext[/]")
message = decrypt(key, ciphertext)
console.print(f"[yellow b]The message:[/] \"{message}\"\n")
COMMANDS = """
0. Help
1. Generate a key.
2. Encrypt a message
3. Decrypt a message
4. Exit
"""
HELP = """
# cryptography-s-des-exploring
cryptography-s-des-exploring is the application that will help you to learn about the S-DES cipher.
## Commands
"""
def help(console):
console.print(Markdown(HELP + COMMANDS))
console.print("\n")
def cli():
console = Console()
choices = list(map(str, range(0, 5)))
while True:
console.print(f"Commands:\n{COMMANDS}")
option = Prompt.ask("Choose the command:", choices=choices, default="0")
match option:
case "0":
help(console)
case "1":
cli_generate_keys(console)
case "2":
cli_encrypt_message(console)
case "3":
cli_decrypt_message(console)
case "4":
break
Loading…
Cancel
Save