Skip to content

Commit

Permalink
Adds Streamlit interface for Task 1
Browse files Browse the repository at this point in the history
  • Loading branch information
cr2007 committed Feb 1, 2024
1 parent 088849d commit e04222b
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions cr2007-task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -121,23 +123,28 @@ 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

# Calculate the difference between the average IoC and the IoC of English language text
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")

# ---------------------------- #

Expand All @@ -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...")
Expand Down

0 comments on commit e04222b

Please sign in to comment.