Question

In: Computer Science

CODE IN PYHTON Write a program which DECRYPTS the secret messages which are created by the...

CODE IN PYHTON

Write a program which DECRYPTS the secret messages which are created by the ENCRYPT program which we went over in class.

It should first prompt the user for the scrambled alphabet which was created by ENCRYPT (which you should be able to copy & paste from the preceeding program's run). It then should ask for the secret message. Finally, it outputs the unscrambled version.

Note that there are exactly 26 characters input for the scrambled alphabet. All alphabetic characters are translated into their decoded equivalents (which will take a WHILE loop), and all other, non-alphabetic characters should be output exactly as they were without translation.

For extra credit, verify that the scrambled alphabet you input has only alphabetic characters and that they don't duplicate. Put out an error message if you find a duplicate letter.

Here is a possible sample run:

Please input the scrambled alphabet in order:
XQHAJDENKLTCBZGUYFWVMIPSOR

Now input the scrambled message:
VNKW KW BO 1WV WJHFJV BJWWXEJ!

Your unscrambled message reads:
THIS IS MY 1ST SECRET MESSAGE!

Solutions

Expert Solution

The following code of the program is explained with comments. Please let me know if you have any doubts regarding the code.

Language used is python2.

Code:

# This function returns the decrypted string based on the scrambled alphabets

def decrypt_string(scrambled_alphabets,encrypted_string):

    # This string stores the decrypted string

    result_string = ""

    # Getting the length of the encrypted string

    encrypted_string_length = len(encrypted_string)

    # This variable is used to keep track of the current index of the encrypted string

    i = 0

    # Iterating over the encrypted string

    while i < encrypted_string_length:

        # If the character at index i in the encrypted string is not an alphabet

        if not encrypted_string[i].isalpha():

            # Add it to the result string unchanged

            result_string += encrypted_string[i]

        # Else

        else:

            # Get the index of the character at index i in the encrypted string in the scrambled alphabets string

            # add 65 to it and find the character value of the resultant ascii value

            encrypted_character = chr( 65 + scrambled_alphabets.index(encrypted_string[i]))

            # Add the encrypted character to the result string

            result_string += encrypted_character

        # Increment the value of i by 1

        i += 1

    # Return the result string

    return result_string

# This is the main function

def main():

    # Reading the scrambled alphabets from the user

    scrambled_alphabets = raw_input("Enter the scrambled alphabet: ")

    # Reading the encrypted message from the user

    encrypted_string = raw_input("Enter the encrypted message: ")

    # This variable is used to keep track of the current index of the scrambled alphabets

    i = 0

    all_alphabets = True

    duplicate_found = False

    # Iterating over the scrambled alphabets

    while i < len(scrambled_alphabets):

        # If the character at index i in the scrambled alphabet is an duplicate

        if scrambled_alphabets[i] in scrambled_alphabets[:i]:

            # Set the duplicate found to True

            duplicate_found = True

            # Break the loop

            break

        # If the character is not an alphabet

        elif not scrambled_alphabets[i].isalpha():

            # Set the all_alphabets to False

            all_alphabets = False

            # Break the loop

            break

        # Else

        else:

            # Increment the value of i by 1

            i += 1

    # If duplicate character is found in the scrambled characters

    if duplicate_found:

        # Print a message to user stating duplicate has been found

        print("Duplicate alphabet found")

    # If not all characters are alphabets

    elif not all_alphabets:

        # Print a message to user stating not all characters are alphabets

        print("Not all characters in the scrambled alphabers are alphabets")

    # If the length of scrambled alphabets is not 26

    elif i != 26:

        # Print a message to user stating length of the scrambled alphabets is not 26

        print("Scrambled alphabets length is not 26")

    # Else

    else:

        # Decrypt the string and store the result

        decrypted_string = decrypt_string(scrambled_alphabets,encrypted_string)

        # Printing the decrypted string

        print("Decrypted string is " + decrypted_string)

# Calling the main function

main()

Code Screenshot:

Sample Output:


Related Solutions

Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your...
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they choose encrypt or decrypt, you...
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Steganography is the technique of hiding secret messages/information within other non-secret data/information. One popular way of...
Steganography is the technique of hiding secret messages/information within other non-secret data/information. One popular way of steganography is hiding text (plaintext) within images (cover text). Each image is a collection of pixels, where each pixel is typically represented by 3 values (RGB). Each of R, G, and B is a value between 0 and 255 and thus can be represented by 8 bits. The color of the pixel depends on these values. However, if the least significant bit (last bit...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Q1: Write a Java program that will display different messages depending on your age. Your program...
Q1: Write a Java program that will display different messages depending on your age. Your program should ask the user for his/her name and their age in years and give one or more answers from the following ones below: if the age of the user is less than 16, the program should print on the screen “You are not allowed to drive at the moment”. if the age of the user is less than 18, the program should print on...
Q1: Write a Java program that will display different messages depending on your age. Your program...
Q1: Write a Java program that will display different messages depending on your age. Your program should ask the user for his/her name and their age in years and give one or more answers from the following ones below: if the age of the user is less than 16, the program should print on the screen “You are not allowed to drive at the moment”. if the age of the user is less than 18, the program should print on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT