Practice answer the following questions and complete the activity:

1. What are some improvements that can be made to our program so that it is even more useful.
2. What are some challenges that could follow from adding this additional functionality?
3. What types of things do you need to be aware of when testing new functionality?
4. Activity: Using the encoding process as a model, create a function that will decipher an encrypted into a plaintext message or encrypt a plaintext message into a ciphertext string.

Second briefly one or two sentences answer question 1-3 from the assignment below are some thoughts for answering each question:
Question 1: Could the program be coded so that a given letter isn't always the same encrypted letter? (Ex. 'A' isn't always encrypted to 'E')?
Question 2: Are there challenges in coding when sometimes 'A' is an 'E' and sometimes 'A' is a different letter? Do you think writing code for 'A' to be any random letter is possible?
Question 3: As a soon to be programmer (We'll start writing our own code in Unit 4), you'll find out that you always need to be aware of whether your program is producing the correct output. Why do you think correct output is important? You can use an example to explain your answer. My example is that I am glad that when I choose a song from my playlist to listen to, that I will hear the song I chose and not some other song that I didn't want to hear.

1 answer

Answers to Questions

  1. Improvements to the Program: One potential improvement could be implementing a dynamic encryption algorithm, where letters are mapped to different encrypted letters each time the program runs. This would enhance security by making it harder for potential interceptors to decipher messages, as the same plaintext letter would not consistently map to the same ciphertext character.

  2. Challenges of Additional Functionality: Introducing a dynamic encryption method can present challenges, such as ensuring that the mapping remains consistent within a single message to avoid confusion. Additionally, implementing randomness requires a secure random number generator, and careful management of key distribution if keys need to be shared between communicating parties.

  3. Awareness When Testing New Functionality: When testing new functionality, it’s crucial to verify that the program produces correct outputs consistently; otherwise, it could lead to significant issues. For instance, if an encryption program mistakenly outputs different ciphertexts for the same plaintext letter without intention, users could misinterpret messages, leading to misunderstandings or security breaches.

Activity: Encryption and Deciphering Function

Here’s a basic Python function that encrypts a message using a simple letter substitution method and includes a corresponding function to decrypt it:

import random
import string

def generate_key():
    letters = list(string.ascii_uppercase)
    shuffled = letters.copy()
    random.shuffle(shuffled)
    return dict(zip(letters, shuffled)), dict(zip(shuffled, letters))

def encrypt(message, key):
    encrypted_message = []
    for char in message.upper():
        if char in key:
            encrypted_message.append(key[char])
        else:
            encrypted_message.append(char)  # Non-letter characters remain the same
    return ''.join(encrypted_message)

def decrypt(encrypted_message, decryption_key):
    decrypted_message = []
    for char in encrypted_message:
        if char in decryption_key:
            decrypted_message.append(decryption_key[char])
        else:
            decrypted_message.append(char)  # Non-letter characters remain the same
    return ''.join(decrypted_message)

# Example use case
encryption_key, decryption_key = generate_key()
plaintext = "HELLO WORLD"
ciphertext = encrypt(plaintext, encryption_key)
print("Ciphertext:", ciphertext)
print("Decrypted:", decrypt(ciphertext, decryption_key))

This code generates a random encryption key and provides functions to encrypt and decrypt messages. Each letter is substituted based on the randomly generated mapping, enhancing the security of the message.