Question

Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

Assignment 7:

Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted.

A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key.

a b c d e f g h i j k l m n o p q r s t u v w x y z

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

For example, let’s say we are encrypting the text ‘hello world’.

If the key is 5, the encrypted text would be ‘mjqqt btwqi’.

This is calculated letter by letter, ie ‘h’ corresponds to element 7, and 7+5=12, and 12 corresponds to ‘m’, and so on for each letter. Note that values loop around the alphabet, ie ‘w’ corresponds to 22, and 22+5=27, which will then correspond to 1 or ‘b’.

This can be accomplished computationally with a modulus ( %26 ).

Your program should:

1. Ask the user for the Caesar cipher key -- give clear instructions; a. If the key is equal to 100, then randomly pick the key (between 0 and 25); i. Print out the randomly decided value; b. Check that the user input a number between 0 and 25, or 100; i. If they did not, request a new key.

2. Ask the user for the text to encrypt -- again give clear instructions; a. Take the lowercase of the text and check that it only contains letters and spaces; i. If the check fails, request a new text from the user; b. Check that no more than 250 characters were input by the user; i. If the check fails, request a new text from the user.

3. Calculate the encrypted text; a. Remember to leave spaces untouched; b. Print the encrypted text to the screen.

4. Ask if the user wants to perform another Caesar cipher; a. If Yes, begin again at (1). Submit your program script called cipher.py. Use functions as appropriate for optimal abstraction and encapsulation. You may find nested while or for loops useful, as well as the string method find().

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code used:

The output obtained:

The code used:

import random   # this is used for randomly generating a number.


def cipher(text, key):  # this function will convert our text to encrypted text.
    # the dictionary below store the integers and the appropriate character
    dictionary = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h',
                  8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q',
                  17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'}

    encryptedText = ""
    for char in text:
        if char != " ":
            dictKey = ord(char) - 97
            encryptedText += dictionary[(dictKey + key) % 26]
        else:
            encryptedText += char

    return encryptedText


def validateText(text):  # this will check if the text has only alphabets and spaces.
    for char in text:
        if (not char.isalpha()) and (not char.isspace()):
            return False
    if len(text) > 250:
        return False
    return True


def validateKey(key):  # this will check if the key is between 0 to 25 and 100.
    if (key < 0 or key > 25) and key != 100:
        return False
    return True


def main():  # this is the driver part of the code, we will keep asking for input from the user util he/she want to quit
    Quit = False

    while not Quit:
        while True:
            key = int(input("Enter Caesar cipher key: "))
            if validateKey(key):
                break
            else:
                print("Invalid, Key!!")

        while True:
            text = input("Enter the text to be encrypted: ").lower()
            if validateText(text):
                break
            else:
                print("Invalid, Text!!")

        if key == 100:  # generating a random key.
            key = random.randint(0, 26)
            print(key, " is randomly selected as the key.")

        encryptedText = cipher(text, key)
        print("The encrypted text:", encryptedText)

        choice = input("Enter yes if you want to continue with another text or Enter no to quit(yes/no): ")
        if choice == "no":
            Quit = True


if __name__ == '__main__':
    main()

I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Question 7 12.5 pts Recall that book ciphers do not necessarily require a full book to...

    Question 7 12.5 pts Recall that book ciphers do not necessarily require a full book to decode, but instead any written text, such as the Declaration of Independence. For the example discussed in class (the second Beale cipher), we created the key by numbering words and taking the first letter. But we can also number characters themselves instead, making sure to skip spaces. The following is a message encrypted with a book cipher, using the text of this question as...

  • Computer Security Question about the Caesar Cipher: I also don't know this part of the problem...

    Computer Security Question about the Caesar Cipher: I also don't know this part of the problem Hello I am not sure how to figure this out Hello so for question 3, I think its +23 "the password is qqzzqqz" choose the correct multiple choice Question1 2 pts The following cipher text was produced by the Caesar Cipher: The Caesar cipher cryptanalysis technique from lecture calculates the most likely keys. When the technique is applied in this case, which of the...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

  • Please answer this problem without using iostream and without strings or arrays. Just use stdio.h and...

    Please answer this problem without using iostream and without strings or arrays. Just use stdio.h and math.h. 57677 1 Caesar's Cipher (70 points) The Caesar's cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party with- out access to the cryptographic key. It is named after Julius Caesar, who allegedly used it to protect messages of military significance. The encryption and decryption operations are simple shifts of the alphabet letters in a cyclic fashion....

  • Can someone please help me for this assignment? Cryptography — the science of secret writing —...

    Can someone please help me for this assignment? Cryptography — the science of secret writing — is an old science; the first recorded use was well before 1900 B.C. An Egyptian writer used previously unknown hieroglyphs in an inscription. We will use a simple substitution cypher called rot13 to encode and decode our secret messages. ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in...

  • Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers...

    Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers (the "backwards" cipher, the Rail Fence cipher, and the Column Transposition cipher) where the ciphertext is created via an altered presentation of the plaintext. The algorithm for each is detailed in the function descriptions in this section. (13 points) def backwards_cipher(plaintext, key): • Parameter(s): plaintext ----- a string; the message to be encrypted key ----- an integer; the number to control this cipher •...

  • JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information...

    JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information being exposed, it is becoming more and more important to find ways to protect our sensitive data. In this program we will develop a simple tool that helps users generate strong passwords, encrypt and decrypt data using some cyphering techniques. You will need to create two classes. The first class is your driver for the application and contains the main method. Name this class...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT