byrne_olivia_58243_3422_text_encryption_CA

advertisement
Olivia byrne
AQA Computer science 4512/1
Practical Programming scenario
4 Olivia byrne 3422 58243
Olivia byrne
4512 text encryption
wildern school
Contents page
Design of solutionSolution developmentProgramming techniquesTesting and evaluation-
1|Page
Document1
Olivia byrne
4512 text encryption
wildern school
Design of solution
What does the problem involve?
 The program is asking me to include encryption and decryption
from plain text and then using offset to change it into cipher text by
using a key to help.
User needs:
Task 1:
 The user needs a main menu.
 The user needs to see the 3 options.
Task 2:
 Enter the name of the file.
 Load the contents page.
Task 3:
 Generate an 8 character key.
Task 4:
 Create the offset factor.
Task 5:
 Turn encrypt text into cipher text.
Task 6:
 Ask the user to enter the file name.
2|Page
Document1
Olivia byrne
4512 text encryption
wildern school
 Save it.
Task 7:
 Enter the name of the file.
 Load the file.
 Enter the key.
Task 8:
 Decrypt it.
Task 9:
 Print the results.
Task 10:
3|Page
Document1
Olivia byrne
4512 text encryption
wildern school
Flowcharts:
4|Page
Document1
Olivia byrne
4512 text encryption
wildern school
Solution development
Task1
print("---------------------------------------- ")
print("|
main menu
|")
print("|
1... encryption
|")
print("|
2... decryption
|")
print("|
3... Quit
|")
print("---------------------------------------- ")
print("\n")
print("please enter your choice")
choice = int (input())
if choice == 1:
print ("encryption")
elif choice == 2:
print ("decryption")
else:
print ("you must choose 1, 2 or 3")
Task2
#Task2
#
print ("enter file name")
filename = input()
#
file = open (filename, "r")
5|Page
Document1
Olivia byrne
4512 text encryption
wildern school
plaintext = file.read()
#
print (plaintext)
Task3
#Task3
import random
char_key=""
#
for x in range(8):
#
random_int = random.randint (33,126)
char_key = char_key + chr(random_int)
#printing key.
print("\n")
print ("here's your random 8 character key")
print (char_key)
Task4
#Task4
offset = 0
for x in char_key:
offset = offset+ord(x)
offset = offset/8
offset = int(offset)
offset = offset-32
print("\n")
print (offset)
6|Page
Document1
Olivia byrne
4512 text encryption
wildern school
Task 5
#task 5
ciphertext=""
for x in plaintext:
if ord (x) == 32:
ciphertext = ciphertext + x
elif ord (x) + offset > 126:
ciphertext = ciphertext + chr((ord(x)+offset)-94)
else:
ciphertext = ciphertext + chr(ord(x)+offset)
print (ciphertext)
Task 6
#task 6
print ("enter the file name to save to")
filename = input ()
file = open (filename, "w")
file.write (ciphertext)
file.close ()
Task 7
#task7
print("enter the ciphertext filename")
cipherfile=input()
file=open (cipherfile,"r")
ciphertext=file.read()
file.close()
print ("enter 8 character key")
7|Page
Document1
Olivia byrne
4512 text encryption
wildern school
key = input()
#print quit if choice 3 is choosen.
elif choice == 3:
print ("quit")
#this is how to ask them again for an option if they havent
already picked one.
else:
print ("you must choose 1, 2 or 3")
Task 8
offset = 0
for x in key:
offset = offset+ord(x)
offset = offset/8
offset = int(offset)
offset = offset-32
print("\n")
print (offset)
plaintext=""
for x in ciphertext:
8|Page
Document1
Olivia byrne
4512 text encryption
wildern school
if ord (x) == 32:
plaintext = plaintext + x
elif ord (x) + offset < 33:
plaintext = plaintext + chr((ord(x)-offset)+94)
else:
plaintext = plaintext +chr(ord(x)-offset)
Task 9
print (plaintext)
#print quit if choice 3 is chosen
elif choice ==3:
print ("quit")
#This is how to ask them again for an option if they haven't
already picked one
else:
print("you must choose 1, 2 or 3")
Programming techniques
Task1
9|Page
Document1
Olivia byrne
4512 text encryption
wildern school
technique
Where used, and why?
Input ()
So the users can enter their choice.
If ()
Used to see if the user has chosen number 1.
Elif ()
Print ()
If they haven’t chosen 1 this checks if they have chosen 2 or 3
then it runs that option.
Used to cover anything other than choice 1, 2, or 3, this is used if
the user enters an incorrect option.
To print the different options.
while
To make sure the user keeps choosing an option.
Else ()
Task2
technique
Where used, and why?
Print()
Used to ask the user a question
Input()
Used to let the users enter an answer
Open()
Used to open the files
Read()
Used to read what’s in the file
Task3
technique
Where used, and why?
Print()
Used to tell the user what the code is
Import
Used to import the random library
10 | P a g e
Document1
Olivia byrne
4512 text encryption
wildern school
Random_int
Generates the random integer between 33 and 136.
Chr()
Turning a number into its letter
for
To make sure we have 8 characters in our code instead of one
Task4
technique
Where used, and why?
Ord()
Turning each character key into its ASCII values
for
Make sure we had the char_key
print
Print the offset on the screen
Int()
Offset integer
11 | P a g e
Document1
Olivia byrne
4512 text encryption
wildern school
Task5
technique
Where used, and why?
for
Make sure we cheeked plain text
if
Make space stays the same
elif
If ASCII code is greater than 126 then we would
have to go back to 33 and carry on counting
If greater than 126 and if there wasn’t a space
there
Use ASCII in the plaintext into its ASCII character
else
Ord()
Chr()
Use each of the ASCII value In the plaintext into its
ASCII character
Task6
technique
Where used, and why?
Print()
Print file name it needs to save on the screen
Input()
Allows user to input filename that’s been saved
Open()
Then open the file write document
Write()
Write ciphertext given in the file
Close()
Close file to shut it down
12 | P a g e
Document1
Olivia byrne
4512 text encryption
wildern school
Task7
technique
Where used, and why?
Print()
Print, enter cipher texts file names
Input()
Slows user to input cipherfile
open()
Allows user to open cipherfile
Read()
Allows user to read the file
Close()
Being able to close the file
Task8
technique
Where used, and why?
for
Makes it to range so there is an 8 character key
if
This is used to check if the user has entered the
number 32 as their choice. This will make the
program run
If the user has not entered choice 1 then I have
used an elif() to check if thr user has entered 2 or
3if true then that part of the program will run
I have used this to cover anything apart from 1, 2,
or 3 that the user I need this so that my program
will be able to do something if the user enters a
wrong choice
Turning each of the character keys in the ASCII
value.
elif
else
Ord()
Chr()
13 | P a g e
Document1
Olivia byrne
4512 text encryption
wildern school
Task9
task
1
technique
Where used, and why?
Print()
Print plaintext back on the screen
Data
Data
structure
structure
name
type
choice
variable
Data type
purpose
Integer
Use to store the choice that
the user entered the menu.
2
filename
variable
string
Used to store the name of
the file.
2
file
file
file
Used to store documents.
14 | P a g e
Document1
Olivia byrne
2
plaintext
4512 text encryption
variable
string
wildern school
To store text in file, which the
user asks to open.
3
Char_key
variable
string
Used to store the character
key.
3
Random_int variable
integer
Store random integers.
4
Offset
ineger
Store offset when text is
variable
shifted.
5
ciphertext
variable
string
15 | P a g e
Document1
Olivia byrne
4512 text encryption
wildern school
Testing and evaluation
Test
Test
Valid/
What you
What
Further
No.
Data
Extreme/
expect to
actually
action
entered
Invalid/
happen
happened
required
erroneus
TASK 1
1.1
1
extreme
encrypt
encrypt
none
1.2
2
valid
decrypt
decrypt
none
1.3
3
extreme
quit
Stopped
none
the
program
1.4
1.5
4
b
invalid
erroneus
Ask
Asked
question
question
again
again
Ask
question
again
none
Get
crashed
program to
register
letters
16 | P a g e
Document1
Olivia byrne
4512 text encryption
Task
User
Did I
number:
need:
meet this
wildern school
How was improvements:
it met:
need:
1
The user
 Yes
needs a main
menu.
By giving
Make the menu look
them a main
nicer
menu by
using “print”
1
The user
By letting
Give more detailed
needs to see
them have 3
instructions
the 3 options
choices by
 Yes
using “print”
2
2
Enter the
 Yes
By asking
name of the
the user for
file.
a filename
Load the
 Yes
contents page.
3
Generate an 8
 Yes
character key.
4
Create the
 Yes
offset factor.
5
Turn encrypt
 Yes
text into cipher
text.
17 | P a g e
Document1
Olivia byrne
6
4512 text encryption
Ask the user
wildern school
 Yes
to enter the file
name.
6
Save it.
 Yes
7
Enter the
yes
name of the
file.
7
Load the file.
yes
7
Enter the key.
yes
8
Decrypt it.
yes
9
Print the
yes
results.
18 | P a g e
Document1
Download