Question

Python program Use the provided shift function to create a caesar cipher program. Your program s...

python program

Use the provided shift function to create a caesar cipher program.

Your program should have a menu to offer the following options:
Read a file as current message
Save current message
Type in a new message
Display current message
"Encrypt" message
Change the shift value

For more details, see the comments in the provided code.

NO GLOBAL VARIABLES!
Complete the program found in assignment.py. You may not change any provided code. You may only complete the sections labeled:#YOUR CODE HERE
Submit source file and screenshot by the posted due date.

------------------------------------------------------

assignment.py:

shift takes a single character and an integer value as arguments
# Any character not == a-z or A-Z will be returned as is
# Any letter will be shifted by numPlaces in the alphabet
# shift('a',1) will return b
# shift('z',1) will return a
# shift('a',-1) will return z
def shift(letter, numPlaces):
digit = ord(letter)
numPlaces = numPlaces %26
if digit >= 65 and digit <= 122:
if digit <=90:
digit = (((digit - 65)+numPlaces)%26) + 65
elif digit >= 97:
digit = (((digit - 97)+numPlaces)%26) + 97
return chr(digit)

# Read a message from a file
# Read the entire contents into a single string
# Return the string
def readMessage():
  
#YOUR CODE HERE

return message

# Save the current message to a file
# Over write the file with the entire message string
# Do not return anything
def saveMessage(message):
  
#YOUR CODE HERE


# Print out the current message
# Nothing more
def displayMessage(message):
  
#YOUR CODE HERE


# Enter a new message from the keyboard
# Return the new message
def typeMessage():
  
#YOUR CODE HERE

# Change the shift value
# Prompt the user to enter a new shift value
# Return this new value as an int
def changeShift():
  
#YOUR CODE HERE

# "Encrypt" the message by shifting
# each character of the message by myShift
# Use the shift function here
# Return the new message
def encrypt(message,myShift):
newMsg = ""
  
#YOUR CODE HERE

return newMsg

# Create a menu for user
def menu(message,myShift):
  
#YOUR CODE HERE

# Basic setup stuff
def main():
message = ""
myShift = 5
menu(message,myShift)

main()

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

Screenshot:-

uick Launch (Ci+a Fie Feit w Pmjert Build Dehug Team Toele Tet Anaye Windew Help hen.py import os # Any character not a-z or

Program

import os
# Any character not == a-z or A-Z will be returned as is
# Any letter will be shifted by numPlaces in the alphabet
# shift('a',1) will return b
# shift('z',1) will return a
# shift('a',-1) will return z
def shift(letter, numPlaces):
digit = ord(letter)
numPlaces = numPlaces %26
if digit >= 65 and digit <= 122:
if digit <=90:
digit = (((digit - 65)+numPlaces)%26) + 65
elif digit >= 97:
digit = (((digit - 97)+numPlaces)%26) + 97
return chr(digit)

# Read a message from a file
# Read the entire contents into a single string
# Return the string
def readMessage():
while(True):
fName=input('Enter file to read: ')
message=''
if os.path.exists(fName):
with open(fName) as f:
while True:
c = f.read(1)
if not c:
break
message+=str(c)
break
else:
print('File not open')
return message

# Save the current message to a file
# Over write the file with the entire message string
# Do not return anything
def saveMessage(message):
while(True):
fName=input('Enter file to write: ')
if os.path.exists(fName):
f = open("demofile2.txt", "a")
f.write(message)
f.close()
break
else:
print('File not open')

# Print out the current message
# Nothing more
def displayMessage(message):
print('\nMessage=',message)


# Enter a new message from the keyboard
# Return the new message
def typeMessage():
message=input('Enter a message: ')
return message

# Change the shift value
# Prompt the user to enter a new shift value
# Return this new value as an int
def changeShift():
shift=int(input('Enter a shift value: '))
return shift


# "Encrypt" the message by shifting
# each character of the message by myShift
# Use the shift function here
# Return the new message
def encrypt(message,myShift):
newMsg = ""
for i in range(len(message)):
newMsg+=shift(message[i],myShift)
return newMsg

# Create a menu for user
def menu(message,myShift):
while(True):
print('\n1. Read a file as current message\n2. Save current message\n3. Type in a new message')
print('4. Display current message\n5. "Encrypt" message\n6. Change the shift value\n7. Quit')
opt=int(input("Enter your option(1-7): "));
while(opt<1 or opt>7):
print("\nError!!!Options from 1 to 7, retry\n")
print('1. Read a file as current message\n2. Save current message\n3. Type in a new message')
print('4. Display current message\n5. "Encrypt" message\n6. Change the shift value\n7. Quit')
opt=int(input("Enter your option(1-7): "));
if(opt!=7):
if(opt==1):
message=readMessage()
elif(opt==2):
saveMessage(message)
elif(opt==3):
message=typeMessage()
elif(opt==4):
displayMessage(message)
elif(opt==5):
message=encrypt(message,myShift)
elif(opt==6):
myShift=changeShift()
else:
print(' Good Bye')
break

# Basic setup stuff
def main():
message = ""
myShift = 5
menu(message,myShift)

main()

--------------------------------------------------------------------

Output


1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 8

Error!!!Options from 1 to 7, retry

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 1
Enter file to read: check.txt
File not open
Enter file to read: C:/Users/deept/Desktop/check.txt

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 4

Message= Hello world

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 2
Enter file to write: check.txt
File not open
Enter file to write: C:/Users/deept/Desktop/check.txt

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 3
Enter a message: Hello

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 5

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 4

Message= Mjqqt

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 6
Enter a shift value: 2

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 3
Enter a message: Hello

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 5

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 4

Message= Jgnnq

1. Read a file as current message
2. Save current message
3. Type in a new message
4. Display current message
5. "Encrypt" message
6. Change the shift value
7. Quit
Enter your option(1-7): 7

Add a comment
Know the answer?
Add Answer to:
Python program Use the provided shift function to create a caesar cipher program. Your program s...
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
  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or...

    In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. Given an arbitrary cipher text file, you need to write a C++ program to find out the value of the shift, and decrypt the...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Do this using the C language. show me the code being executed and also copy and...

    Do this using the C language. show me the code being executed and also copy and paste the code so i can try it out for myseld Instructions A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (ie. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Can someone help me with this? ## caesar.py def encode(msg, shift):    newmsg = ""   ...

    Can someone help me with this? ## caesar.py def encode(msg, shift):    newmsg = ""    #process each character in msg. #Hint: using a for loop    #A for loop to check each ch in msg: if ('A' <= ch and ch <= 'Z'): #if the character is an uppercase letter #encode the character based on the shift number    #Hint: the new character newch should be #chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))    elif ('a' <=...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

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