Problem Solving and Programming
Programming Assignment 1
Eynesbury Modifications
School of Computer and Information Science The University of South Australia
Copyright By PowCoder代写 加微信 powcoder
Contents ……………………………………………………………………………………………………………………………………………..2 Introduction …………………………………………………………………………………………………………………………………………3 Encryption …………………………………………………………………………………………………………………………………………..3 Menu……………………………………………………………………………………………………………………………………………………4 Requirements ………………………………………………………………………………………………………………………………………5 Stages …………………………………………………………………………………………………………………………………………………6
Stage 1 ………………………………………………………………………………………………………………………………………….6 Stage 2 ………………………………………………………………………………………………………………………………………….6 Stage 3 ………………………………………………………………………………………………………………………………………….7 Stage 4 ………………………………………………………………………………………………………………………………………….8 Stage 5 ………………………………………………………………………………………………………………………………………….8 Stage 6 ………………………………………………………………………………………………………………………………………….9 Stage 7 ………………………………………………………………………………………………………………………………………….9 Stage 8 ………………………………………………………………………………………………………………………………………..10 Stage 9 ………………………………………………………………………………………………………………………………………..11 Stage 10 ………………………………………………………………………………………………………………………………………12 Stage 11 ………………………………………………………………………………………………………………………………………13
Submission Details…………………………………………………………………………………………………………………………….13 Extensions and Late Submissions ……………………………………………………………………………………………………..14 Academic Misconduct………………………………………………………………………………………………………………………..14 Marking Criteria …………………………………………………………………………………………………………………………………15
INTRODUCTION
This document describes the first assignment for Problem Solving and Programming.
The assignment is intended to provide you with the opportunity to put into practice what you have learned in the course by
applying your knowledge and skills to the implementation of a simple encryption algorithm. This assignment is an individual task that will require an individual submission.
This document is a specification of the required programs and their output. Please ask your practical supervisor if you do not understand any part of this document or the assignment.
ENCRYPTION
A simple way to encrypt data is attributed to , the . (If you are interested, you may like to read the following… http://en.wikipedia.org/wiki/Caesar_cipher). This method takes each character in a message and replaces it with one which is a certain distance (offset) along the alphabet from it.
For example:
123456789….. ABCDEFGHIJKLM… +3 offset →
ABCDEFGHIJ…
Encrypting “ACE” with the offset of +3 will result in “DFH”. To decrypt “DFH”, offset by the same amount in the other direction (i.e. with the negative offset -3). Decrypting “DFH” with the offset -3 will result in “ACE”.
You will need to use the following two functions:
If c is a character (a string of length 1), ord(c) returns an integer representing the ASCII position of that character. For example: ord(‘a’) returns the integer 97, ord(‘b’) returns the integer 98, etc.
If i is an integer, chr(i) returns a string containing only one character with an ASCII code equal to the integer i. For example: chr(97) returns the string ‘a’, chr(98) returns the string ‘b’
Page 3 of 22
Instead of restricting the message to the alphabetic characters only, we will use all the printable ASCII characters i.e., all the characters from ASCII 32 (Space) to ASCII 126 (~).
Your task is to write a menu driven program called yourId_encryption.py that will allow the user to enter commands
and process these commands until the quit command is entered. The following commands should be allowed:
1. Enter Message:
Prompt for and read (from the keyboard) a string to be encrypted.
2. Encrypt Message:
Encrypts the previously entered message or displays an error message if no string was entered. The message will be encrypted using a randomly generated number between 32 and 126 as the offset/encryption key. This encryption key will be converted into a character using chr(i) and appended to the encrypted string.
3. Decrypt Message:
Decrypts the previously entered message or displays an error message if no string was entered. The message will be decrypted by converting the last letter in the string (the encryption key) to its ASCII value, and using this to offset all other characters in the negative direction. The encryption key should then be removed from the message.
Displays a goodbye message to the screen and quits the program.
Page 4 of 22
REQUIREMENTS
It is recommended that you develop this part of the assignment in the suggested stages. Each stage is worth a portion of the marks.
It is expected that your solution will include the use of:
• Your solution in a file called yourEmailId_encryption.py.
• Appropriate and well-constructed while and for loops.
• Appropriate if, if-else, if-elif-else statements.
• The use of the ord(c) and chr(i) functions.
• The use of the random.randint(a, b) function in order to generate an encryption key.
• Output that strictly adheres to the assignment specifications. If you are not sure about these details, you should
check with the provided file: ‘Sample Output_Encryption’.
• Good programming practice:
o Consistent commenting, layout, and indentation. You are to provide comments to describe: your details, program description, all variable definitions, and every significant section of code.
o Meaningful variable names.
Your solution MUST NOT use:
• break, or continue statements in your solution. Do not use the quit() or exit() functions or the break or return statements (or any other techniques) as a way to break out of loops. Doing so will result in a significant mark deduction.
Please ensure that you use Python 3.8.0 or later to complete your assignments. Your programs MUST run using Python 3.8.0.
Page 5 of 22
It is recommended that you develop this part of the assignment in the suggested stages. Many problems in later stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before continuing.
Write code to display your details to the screen. Your code should output your details to the screen in this format:
File : wayby001_petals.py
Author : Batman
Student ID : 0123456X
Description: Programming Assignment 1 – Encryption
This is my own work as defined by the Eynesbury Academic Misconduct Policy.
Ensure your output follows this format exactly. Ensure you change the file, author, and student ID to your own.
Write code to display the menu and to prompt for and read the user’s choice.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4):
Ensure this code works correctly before moving onto the next stage.
Page 6 of 22
Set up a loop to prompt for and read menu commands until the user enters 4 (to quit). You do not need to perform any encryption/decryption at this point; you may simply display appropriate messages to the screen.
Sample output:
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 1
Option 1: Enter Message
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 2
Option 2: Encrypt Message
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 3
Option 3: Decrypt Message
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 4
Test your program to ensure this is working before you move onto the next stage. Ensure the loop ends after the user enters option 4.
Once you have that working, back up your program. Note: When developing software, you should always have fixed points in your development where you know your software is bug free and runs correctly.
Page 7 of 22
Include code to detect if the user inputs a menu option less than 1 or greater than 4. If this happens print an error message and allow them to enter another number.
Sample output:
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 5
Invalid choice. Enter an option (1,2,3,4): 0 Invalid choice. Enter an option (1,2,3,4): 4
Make sure the program runs correctly.
Create a variable called message set to an empty string above your loop. It is important that all menu options use and modify this same variable.
Add code to implement option 1: Enter Message.
Prompt for and read a string from the keyboard. This string should be saved into the variable message. The same message variable will be used again when you select to encrypt or decrypt it. If the message is not an empty string, it should be displayed after executing each command.
Test your program to ensure it displays the output seen below:
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 1
Please enter a new message: The crow laughs loudest. Your message is: ‘The crow laughs loudest.’.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 4
Your message is: ‘The crow laughs loudest.’.
Page 8 of 22
Add code to implement command 2: Encrypt Message.
This command will use the message entered previously in option 1: Enter Message. If this message is currently empty, display the error message “Error: Cannot encrypt an empty message.“ Otherwise, the message should be encrypted.
You will need to use a loop and the ord(c) function to get the ASCII position of each character in the message. Test this with the message “abC” and ensure the ASCII positions are 97, 98, and 67.
You will need to add an offset to the ASCII position and then convert this number back to a character using the chr(i) function. The encrypted characters should be concatenated together to create your encrypted message. The encrypted message should then be put into the message variable.
To start with, choose 1 as your encryption offset. Test your program by first entering the message ‘abC’, then encrypt it. The result should be ‘bcD’.
Modify your code so that every time the message is encrypted the encryption offset is a different random integer from 32 to 126. You should use the random.randint(a, b) function to do this.
If you test this, you may notice some strange output as some of the characters will be unprintable e.g., characters beyond ASCII position 126. Your program must only work with the printable ASCII character set: all the characters from ASCII 32 (Space) to ASCII 126 (~). When the ASCII position of the encrypted character points to a character beyond 126 it should wrap around to the beginning of the set, to position 32.
For any character beyond 126, you should subtract 95 (the total number of characters in the set) to wrap back to the beginning. You may have to use a while loop to subtract 95 multiple times until it is within the set, for example, ‘}’ + 125 is 250, and minus 95 is 155. This is still out of bounds. Use a loop.
Test your code to ensure your message is encrypted using a random offset. Note that if the encrypted string contains characters that are not in the table below, then your ASCII values are not ‘wrapping’ correctly.
Note: It is very important to test your code at multiple points throughout development. Not only does this ensure there are no errors; it also ensures that you understand how your code works. You may like to use print statements to display your variables as they change within your loops. Remove these print statements when you are sure your code works.
Page 9 of 22
Your encryption key will need to be saved so you can decrypt the message in the future. Modify your code to convert the encryption offset (your encryption key) to a character. Convert the offset into a character using chr(i) and add it to the end of the encrypted message.
For example, if the offset was 33 (ASCII value ‘!’), 33 would be used to offset each character in the message, and ‘!’ (ASCII 33) would be appended to the end of the string. If the message was “ABC”, the encrypted string with offset 33 would be “bcd!”
Test this to ensure your message is becoming 1 character longer every time it is encrypted.
Sample output:
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 2
Error: Cannot encrypt an empty message.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 1
Please enter a new message: FULLmoon
Your message is: ‘FULLmoon’.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 2
Your message was successfully encrypted. Your message is: ‘n}tt6887(‘.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 4
Your message is: ‘n}tt6887(‘.
Note: The random offset encryption key was 40 (ASCII ‘(’). Every character in the message was offset by 40. The characters in “moon” were wrapped back by 95 because they were moved to positions outside the range 32 – 126. The ASCII character for 81 ‘(‘ was appended to the string, which made the string 1 character longer.
Page 10 of 22
Add code to implement option 3: Decrypt Message.
This command will use the same message entered previously in option 1: Enter Message, and then encrypted using option 2: Encrypt Message. If this message is currently empty, display the error message “Error: Cannot decrypt an empty message”. Otherwise, the message should be decrypted.
The encryption key will be found as the last character in the string. You will need to use the ord(c) function to get the ASCII value of this character. The number that is returned is the offset. You will need to subtract this offset from the ASCII position of each other character in the message variable. These ASCII values will then need to be converted back to characters using the chr(i) function. The decrypted characters should be concatenated together to create your decrypted message. The last character (the encryption key) should not be included in the decrypted message. The decrypted message should then be put into the message variable.
Your program must only work with the printable ASCII character set: all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset position points to a character that is less than 32, it should wrap around to the end of the set. You may have to add 95 multiple times until it is within 32 to 126. Use a loop.
Sample output:
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 3
Error: Cannot decrypt an empty message.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 1
Please enter a new message: Long hallways here.
Your message is: ‘Long hallways here.’.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 2
Your message was successfully encrypted. Your message is: ‘.QPIaJCNNYC[UaJGTGoA’.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 3
Your message was successfully decrypted. Your message is: ‘Long hallways here.’.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 4
Your message is: ‘Long hallways here.’.
Page 11 of 22
If you decrypt your message before it was encrypted or decrypt it more times than it was encrypted, you may lose your original message. For example, if you decrypt the message “ABC” three times, the message becomes an empty string.
Modify your code to count how many times the message is encrypted and subtract 1 from the counter whenever it is decrypted. If you attempt to decrypt the message when the counter is less than 1, display the warning, “Your message may already be fully decrypted…” and ask the user, “Are you sure you want to decrypt (y/n)? “. Only decrypt the message if the user enters “y”. Ensure you reset the counter whenever you enter a new message.
Include an input validation loop to ensure the user enters only ‘y’ or ‘n’.
——————-
——————-
1. Enter Message
2. Encrypt Message
3. Decrypt Message
Enter an option (1,2,3,4): 1
Please enter a new message: ABC
Your message is: ‘ABC’.
——————-
——————-
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com