In: Computer Science
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!
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: