From e04222b7b9b66dfd8120400f3fe6dfc5869ac4d1 Mon Sep 17 00:00:00 2001 From: CSK <73425927+cr2007@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:29:25 +0400 Subject: [PATCH] Adds Streamlit interface for Task 1 --- cr2007-task1.py | 51 +++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/cr2007-task1.py b/cr2007-task1.py index 9a86f35..9c70698 100644 --- a/cr2007-task1.py +++ b/cr2007-task1.py @@ -13,6 +13,8 @@ Calculates the Index of Coincidence (IoC) for a given message. """ +import streamlit as st + # Source: https://inventwithpython.com/cracking/chapter19.html def get_letter_count(cipher_message: str) -> dict: """ @@ -121,10 +123,12 @@ def key_length_guess(cipher_message: str, keyLength_guess: int, debug: bool = Fa sub_message_iocs = [calculate_index_of_coincidence(sub_message) for sub_message in sub_messages] if debug: print(f"IoC for Key Length {keyLength_guess} = {sub_message_iocs}") + st.write(f"IoC for Key Length **{keyLength_guess}** = $${sub_message_iocs}$$") # Calculate the average IoC of the sub-messages average_ioc = sum(sub_message_iocs) / len(sub_message_iocs) print(f"Average IoC for Key Length {keyLength_guess} = {round(average_ioc, 4)}") + st.write(f"Average IoC for Key Length **{keyLength_guess}** = $${round(average_ioc, 4)}$$") ioc_english: float = 0.0686 # IoC of English text @@ -132,12 +136,15 @@ def key_length_guess(cipher_message: str, keyLength_guess: int, debug: bool = Fa ioc_difference: float = abs(average_ioc - ioc_english) if debug: print(f"IoC Difference for Key Length {keyLength_guess} = {round(ioc_difference, 4)}\n") + st.write(f"IoC Difference for Key Length **{keyLength_guess}** = $${round(ioc_difference, 4)}$$") # If the difference is less than 0.01, the key length is considered a possible key length if ioc_difference < 0.01: print(f"{keyLength_guess} is a possible key length.\n") + st.write(f"**{keyLength_guess}** is a possible key length.\n") else: print(f"{keyLength_guess} is not a possible key length.\n") + st.write(f"**{keyLength_guess}** is not a possible key length.\n") # ---------------------------- # @@ -158,26 +165,32 @@ def key_length_guess(cipher_message: str, keyLength_guess: int, debug: bool = Fa # Try block to catch KeyboardInterrupt (Ctrl+C) try: + st.header("F20CN Coursework 1", help="Task 1 of the F20CN Coursework 1") + + st.subheader("Task 1: Verifying Key Length Guesses: Vigenère Cipher") + # Prompt the user to enter the ciphertext and store it in the 'message' variable. - message: str = input("Enter your ciphertext: ") - - # Loops until the user enters 0 to exit - while True: - # Try block to catch ValueError (if the user enters a non-integer value) - try: - # Prompt the user to enter their key length guess - # and store it in the 'key_length' variable. - key_length = int(input("Enter your key length guess (0 to exit): ")) - print("") # Blank Line - - # Checks if the user input is complete - if key_length == 0: - print("Exiting...") # Print a message indicating that the program is exiting. - break # Exit the loop - - key_length_guess(message, key_length) - except ValueError: - print("Invalid Input. Please enter a number.") + message = st.text_input("Enter your ciphertext: ", + placeholder="Ciphertext", help="Encrypted message") + + # Try block to catch ValueError (if the user enters a non-integer value) + try: + # Prompt the user to enter their key length guess + # and store it in the 'key_length' variable. + key_length = st.number_input("Enter your key length", + help="Number of bits in a key", step=1, min_value=1) + + debug_mode = st.checkbox("Debug Mode", help="Enable debug mode to see the" + + "Index of Coincidence values") + + if st.button("Submit", type="primary"): + if message != "": + with st.spinner("Calculating..."): + key_length_guess(message, int(key_length), debug=debug_mode) + else: + st.info("Please enter a ciphertext", icon="⚠️") + except ValueError: + print("Invalid Input. Please enter a number.") except KeyboardInterrupt: # Enter a message when the user presses Ctrl+C and exits the program print("\nExiting Program...")