Question

Write a Python program that takes the following phrases and encrypts them using a substitution cipher....

Write a Python program that takes the following phrases and encrypts them using a substitution cipher. It should ask for a key and a phrase and then proceed to encrypt that phrase using the given shared key. Make sure that you have the key for the cipher that you use and the output of encrypted phrases.

1. He who fights with monsters should look to it that he himself does not become a monster . And if you gaze long into an abyss , the abyss also gazes into you .

2. There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here , it will instantly disappear and be replaced by something even more bizarre and inexplicable . There is another theory which states that this has already happened .

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

I have provided the code (well-commented) and screenshots of output and code for indentation issue.

The program runs until a q is entered.

The program asks for phrase and a key.

key must contain all alphabets and length 26. If incorrect key is entered , program prompts for another key.

Input any key. Here key = lshjdifktbwgxernmuozypaqvc

OUTPUT :

Code Screenshot for indentation:

CODE:

def encrypt(phrase,key):
   output_phrase=""
   for char in phrase: #iterate through every character      
      
       # substitute the phrase only if it is an alphabet
       if ord(char) >= ord('a') and ord(char) <= ord('z') :
           # map the alphabet to 0-26 respectively
           # 'a' maps to 0 and 'z' maps to 25
           idx = ord(char) - ord('a')
           #concatenate the character in key to output
           output_phrase = output_phrase + key[idx]
      
       else:
           #concatenate the character as it is to output
           output_phrase = output_phrase + char

   return output_phrase

def main():

   while True:

       phrase = input("Enter the phrase : 'q' to quit ").lower()

       if phrase[0] == 'q' :
           return

       #take input and convert phrase to lower case
       key = input("Please enter the key of length 26 : ")
       while len(key) != 26 : # promt for key until it is valid
           key = input("Please enter correct key of length 26 :")

       print(encrypt(phrase,key)) # function to encrypt

if __name__ == '__main__':
   main()

**Thank You. Please comment for any further clarification**

Add a comment
Know the answer?
Add Answer to:
Write a Python program that takes the following phrases and encrypts them using a substitution cipher....
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
  • Write a java program that takes the following phrases and encrypts them using a substitution cipher....

    Write a java program that takes the following phrases and encrypts them using a substitution cipher. The program should ask for a key and a phrase and then proceed to encrypt that phrase using the given shared key. Make sure that you have the key for the cipher and the output of encrypted phrases. Encrypt the phrase: He who fights with monsters should look to it that he himself does not become a monster. And if you gaze long into...

  • Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could...

    Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could you possibly make it as simple as you can without losing functionality. please include comments, that would help me better understand Example of PlayFair Cipher: https://en.wikipedia.org/wiki/Playfair_cipher The Playfair cipher uses a 5 by 5 table containing a key word or phrase. Memorization of the keyword and 4 simple rules was all that was required to create the 5 by 5 table and use the...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

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