write a program called hw6.py that enables you to encrypt messages using a substitution cipher.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 12RQ
icon
Related questions
Question

In a substitution cipher, we “encrypt” (i.e., conceal in a reversible way) a message by replacing every letter with another letter. To do so, we use a key: in this case, a mapping of each of the letters of the alphabet to the letter it should correspond to when we encrypt it. To “decrypt” the message, the receiver of the message would need to know the key, so that they can reverse the process: translating the encrypt text (generally called ciphertext) back into the original message (generally called plaintext).

A key, for example, might be the string NQXPOMAFTRHLZGECYJIUWSKDVB. This 26-character key means that A (the first letter of the alphabet) should be converted into N (the first character of the key), B (the second letter of the alphabet) should be converted into Q (the second character of the key), and so forth.

A message like HELLO, then, would be encrypted as FOLLE, replacing each of the letters according to the mapping determined by the key.

Let’s write a program called hw6.py that enables you to encrypt messages using a substitution cipher.

def isValidKey(key):
Returns True if key is a string that has 26 characters and each of the letter
'a'/'A'-'z'/'Z' appeared once and only once in either lower case or upper case.
Returns False otherwise.
II II ||
return True
def replace(letter, key):
II II ||
Assume letter is a single characer string.
Replace a single letter with its corresponding key, returns letter if it is
not in the alphabet 'a'-'z' or 'A'-'Z'
II II ||
return letter
def substitution(plainText, key):
II II ||
Returns encrypted string for the plainText using substitution encryption, which
is to substitute 'a'/'A' with the lower/upper case of key[0], and substitute
'b'/'B' with the lower/upper case of key[1], and so on all the way to 'z'/'Z'.
Leave all other non-alpha characters as it is in plainText.
Your algorithm should be efficient so that it can run fairly quickly for very
large strings.
1. use replace() to convert each letter to its encrypted letter
2. append encrypted letter to a list
3. use ''.join(list_of_letters) to form the final string. Do not use + to do
string concatenation to form the encrypted string.
II II||
return plainText
def run():
1. Prompt user for a key;
2. Check if key is valid. If not, print an error message then return;
if valid, prompt for a plain text to be encrypted with the valid key,
3. Send the plain text and valid key to substitution function.
4. Print out the encrypted message.
Transcribed Image Text:def isValidKey(key): Returns True if key is a string that has 26 characters and each of the letter 'a'/'A'-'z'/'Z' appeared once and only once in either lower case or upper case. Returns False otherwise. II II || return True def replace(letter, key): II II || Assume letter is a single characer string. Replace a single letter with its corresponding key, returns letter if it is not in the alphabet 'a'-'z' or 'A'-'Z' II II || return letter def substitution(plainText, key): II II || Returns encrypted string for the plainText using substitution encryption, which is to substitute 'a'/'A' with the lower/upper case of key[0], and substitute 'b'/'B' with the lower/upper case of key[1], and so on all the way to 'z'/'Z'. Leave all other non-alpha characters as it is in plainText. Your algorithm should be efficient so that it can run fairly quickly for very large strings. 1. use replace() to convert each letter to its encrypted letter 2. append encrypted letter to a list 3. use ''.join(list_of_letters) to form the final string. Do not use + to do string concatenation to form the encrypted string. II II|| return plainText def run(): 1. Prompt user for a key; 2. Check if key is valid. If not, print an error message then return; if valid, prompt for a plain text to be encrypted with the valid key, 3. Send the plain text and valid key to substitution function. 4. Print out the encrypted message.
Here are a few examples of how the program might work. For example, if the user inputs a key of
YTNSHKVEFXRBAUQZCLWDMIPGJO and a plaintext of HELLO:
$ python3 hw6.py
Enter a 26 letter key: YTNSHKVEFXRBAUQZCLWDMIPGJO
Plain Text: HELLO
Cipher Text: EHBBQ
Here's how the program might work if the user provides a key of VCHPRZGJNTLSKFBDQWAXEUYMOI) and a plaintext of
hello, world :
$ python3 hw6.py
Enter a 26 letter key: VCHPRZGJNTLSKFBDQWAXEUYMOI
Plain Text: hello, world
Cipher Text: jrssb, ybwsp
$ python3 hw6.py
Enter a 26 letter key: fgRzPMBkJAOWYDieQhNStcULxv
Plain Text: I hope you find CMPSC131 a fun class, where you learn how to program in Python and C; where you le
arn to use version control system like git and GitHub and where you are having lots of fun!
Cipher Text: J kiep xit mjdz RYENR131 f mtd rwfnn, ukphp xit wpfhd kiu si ehibhfy jd Exskid fdz R; ukphp xit w
pfhd si tnp cphnjid ridshiw nxnspy wjop bjs fdz BjsKtg fdz ukphp xit fhp kfcjdb wisn im mtd!
Notice that neither the comma nor the space were substituted by the cipher. Only substitute alphabetical
characters! Notice, too, that the case of the original message has been preserved. Lowercase letters remain
lowercase, and uppercase letters remain uppercase.
Transcribed Image Text:Here are a few examples of how the program might work. For example, if the user inputs a key of YTNSHKVEFXRBAUQZCLWDMIPGJO and a plaintext of HELLO: $ python3 hw6.py Enter a 26 letter key: YTNSHKVEFXRBAUQZCLWDMIPGJO Plain Text: HELLO Cipher Text: EHBBQ Here's how the program might work if the user provides a key of VCHPRZGJNTLSKFBDQWAXEUYMOI) and a plaintext of hello, world : $ python3 hw6.py Enter a 26 letter key: VCHPRZGJNTLSKFBDQWAXEUYMOI Plain Text: hello, world Cipher Text: jrssb, ybwsp $ python3 hw6.py Enter a 26 letter key: fgRzPMBkJAOWYDieQhNStcULxv Plain Text: I hope you find CMPSC131 a fun class, where you learn how to program in Python and C; where you le arn to use version control system like git and GitHub and where you are having lots of fun! Cipher Text: J kiep xit mjdz RYENR131 f mtd rwfnn, ukphp xit wpfhd kiu si ehibhfy jd Exskid fdz R; ukphp xit w pfhd si tnp cphnjid ridshiw nxnspy wjop bjs fdz BjsKtg fdz ukphp xit fhp kfcjdb wisn im mtd! Notice that neither the comma nor the space were substituted by the cipher. Only substitute alphabetical characters! Notice, too, that the case of the original message has been preserved. Lowercase letters remain lowercase, and uppercase letters remain uppercase.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Public key encryption
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT