Question

Your task is to create a Python module called mixed_cipher. You may have had some experience...

Your task is to create a Python module called mixed_cipher. You may have had some experience with writing programs that implement simple Caesar ciphers. In this assignment, we’ll be implementing a small program which takes in a filename as a command-line argument and then performs simple substitution on the file contents with a mixed alphabet. A mixed alphabet can be created by prompting the user for the keyword, removing repeated letters in the keyword, and then writing the remaining letters of the alphabet in the usual order. For example, if my keyword is “computer”, then I have the following:

Plaintext: ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ciphertext: COMPUTERABDFGHIJKLNQSVWXYZ

Your program should output the encrypted text to the screen. Here’s an example execution. Let’s say I have a file called mixed_test.txt which has the following contents:

This is a file. This file has some WORDS in it.

A sample run of mixed cipher.py with this test file would produce the following output. Note that capitalization and punctuation are kept as-is in the encrypted text.

$ python mixed_cipher.py mixed_test.txt

Please enter a keyword for the mixed cipher: Motherboard

Plaintext: abcdefghijklmnopqrstuvwxyz

Ciphertext: motherbadcfgijklnpqsuvwxyz

Sadq dq m rdge. Sadq rdge amq qkie WKPHQ dj ds.

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

Program:

Code:

#import sys for accessing command line arguments
import sys

#plain aplhabets string
plain_alphabets = 'abcdefghijklmnopqrstuvwxyz'

#read keyword from user
keyword = raw_input('Enter the keyword:')

#convert the keyword to lower case
keyword = keyword.lower()

"""
remove duplicate characters from the keyword
set(String) retruns a set of characters in the passed string
set can have only have unique elements, set does not follow the order
sorted is() used to reorder the elements of the set using the original string
join() is used to form a string from the list returned by the sorted() method
"""
keyword = ''.join(sorted(set(keyword),key = keyword.index))

#copy of plain alphabets
temp_alphabets = plain_alphabets

#removing alphabets which are already present in keyword
for char in keyword:
   temp_alphabets = temp_alphabets.replace(char,'')

#final cipher alphabets
cipher_alphabets = keyword+temp_alphabets;

#printing plain and cipher texts
print('Plaintext :'+plain_alphabets)
print('Ciphertext :'+cipher_alphabets)

#opening the file
input_file = open(sys.argv[1])

#reading text from the file
plain_text = input_file.read()

#cipher text
cipher_text = ''

#itearting on every character of plain text
for char in plain_text:
   #finding position of the current character in plain alphabets
   position = plain_alphabets.find(char)
  
   #if current character is not in plain text(incase of capitals, punctuals)
   if(position==-1):
       #adding original character to cipher as it is
       cipher_text = cipher_text+char
   else:
       #finding cipher alphabet for current character and adding it to cipher text
       cipher_text = cipher_text+cipher_alphabets[position]
      
#printing cipher text
print(cipher_text)

Output:

mixed_test.txt:

Note:

  • This code is for Python 2.x
  • In case of Python3.x please replace raw_input() with input() in line 8 and the code works fine.
Add a comment
Know the answer?
Add Answer to:
Your task is to create a Python module called mixed_cipher. You may have had some experience...
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
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