File - Joshua Hadabora

advertisement
Joshua Hadabora
2104
Task 2 – Keyword Cipher
Contents
REQUIREMENTS ............................................................................................................................................... 2
DESIGN ............................................................................................................................................................ 2
VARIABLES ............................................................................................................................................................. 2
Global Variables ............................................................................................................................................. 2
Local Variables ............................................................................................................................................... 3
PSEUDO CODE ........................................................................................................................................................ 3
FLOWCHART ........................................................................................................................................................... 4
DEVELOPMENT CHOICES .................................................................................................................................. 5
TESTING ........................................................................................................................................................... 6
PLANNING ............................................................................................................................................................. 6
DEVELOPMENT ....................................................................................................................................................... 7
Test 1.1: Using the input function to get a string from the user. ................................................................... 7
Test 1.2: Checking for a blank string. ............................................................................................................. 7
Test 1.3: Allow the user to reattempt their input........................................................................................... 8
Test 1.4: Checking if the string has a letter in it. .......................................................................................... 11
Test 1.5: Checking if the keyword has a forbidden character. ..................................................................... 12
Test 1.6: Creating the decryption prompt. ................................................................................................... 13
Test 2.1: Repeating the keyword to match the length of the string. ........................................................... 14
Test 2.2: Creating the function. .................................................................................................................... 15
Test 2.3: Only encrypting letters in the alphabet. ........................................................................................ 16
Test 3.1: Getting the character code of a letter. .......................................................................................... 17
Test 3.2: Adding two characters together. ................................................................................................... 18
Test 3.3: Adding decryption support. ........................................................................................................... 19
Test 3.4: Wrapping the resulting character around the alphabet. .............................................................. 19
FINAL SOLUTION ................................................................................................................................................... 21
Test 1.1: Does the program keep asking for a string if entered incorrectly? ............................................... 22
Test 1.2: Does the program keep asking for the keyword if entered incorrectly? ....................................... 23
Test 1.3: Does the program keep asking for encryption or decryption if entered incorrectly? .................... 23
Test 2.1: Putting in the alphabet with a set keyword. ................................................................................. 24
Test 2.2: Decrypting the encrypted alphabet with the same keyword. ....................................................... 24
Test 2.3: Encrypting a full sentence. ............................................................................................................ 24
Test 2.4: Decrypting the full sentence. ......................................................................................................... 25
Test 2.5: Double-layered encryption. ........................................................................................................... 25
RECAP ................................................................................................................................................................. 26
Development Tests ....................................................................................................................................... 26
Final Solution Tests ...................................................................................................................................... 28
EVALUATION.................................................................................................................................................. 29
MEETING REQUIREMENTS....................................................................................................................................... 29
CONCLUSION ........................................................................................................................................................ 30
Page 1
Joshua Hadabora
2104
Requirements
This second task requires proof of designing, development, testing and evaluation of a program that
encrypts strings, this time based on a keyword rather than an offset. This method of encryption is
more secure than the Caesar Cipher.






The string can include special characters like numbers and symbols, but these will not be
encrypted and will stay the same throughout the process of encryption.
Uppercase letters will remain uppercase and lowercase will remain lowercase. (The alphabet
must wrap around itself.)
The encrypted string will be displayed after this process.
The string must not be blank.
The keyword must not be blank or have any spaces, numbers or special characters.
The user must be allowed to reattempt any mistakes they have made inputting data.
Design
This program will also be run using the command line or a Python shell, and the strings will be
entered with a keyboard.
Variables
Variables are used to store data which can change while a program is being run. The variables used
in the program will be:
Global Variables
These variables are used throughout the whole program.
string
The string that the user will input that will be encrypted. We only have to test the length of this to
check if it was left blank by the user.
newstring
The final string given to the user when the encryption is done. The program will go through the
user’s string letter by letter, and adding the encrypted version of the letter to this final string.
decryption
A boolean set at the start of the program that determines whether the program will encrypt or
decrypt using the keyword.
This will be determined by if the user enters ‘decryption’ or ‘encryption’.
keyword
The string that the user enters that will be used to encrypt the user’s string.
keywordRepeated
The keyword repeated to fit the length of the string.
firstuppercase, lastuppercase, firstlowercase, lastlowercase
These variables aren’t really necessary, but are very useful. These variables act as constants (you
cannot create constants on Python) which are used near the end of the code for wrapping letters.
If you didn’t have these as constants we’d need to keep using numbers like ‘65’, ‘90’ and ‘122’
which would make our code harder to understand.
Page 2
Joshua Hadabora
2104
Local Variables
These variables are set more than once and are used only in the encryptedChar function.
charnum
The ASCII character code of the letter that the encryptedChar function is currently encrypting.
This letter comes from the user’s string.
modifier
The amount we have to add to charnum. This is based off of a letter from the keyword that is in
the same position as the letter from the user’s string. This number is made a negative if the user
wants decryption.
charnum2
This is the result of adding modifier to charnum. This number is then change to wrap around the
alphabet.
newstring will be printed to the user if the program is successful.
Pseudo Code
Unlike with the Caesar Cipher report, I now have an exact understanding of how to make character
wrap around the alphabet at this point and can include this knowledge in the psuedocode.
DO
INPUT encryption_string
UNTIL encryption_string != blank AND encryption_string has at least one letter
DO
INPUT keyword
UNTIL keyword != blank AND keyword has no special characters
DO
INPUT decryptionStr
UNTIL the first letter of decryptionStr is ‘d’ OR ‘e’
decryption = true if first letter of decryptionStr is ‘d’
newstring = “”
keyword = keyword repeated to the length of encryption_string
firstuppercase = character code of ‘A’
lastuppercase = character code of ‘Z’
firstlowercase = character code of ‘a’
lastlowercase = character code of ‘z’
FOR every letter of encryption_string DO
IF letter is a letter THEN
character_code = character code of letter
modifier = character code of letter in keyword – first letter in alphabet
modifier * -1 if encryption is true
character_code2 = character_code + modifier
IF decryption THEN
IF character_code <= lastuppercase AND character_code2 < firstuppercase THEN
character_code2 += 26
ELSEIF character_code > firstlowercase AND character_code2 < firstlowercase THEN
character_code2 += 26;
ENDIF
ELSE
IF character_code <= lastuppercase AND character_code2 > lastuppercase THEN
character_code2 -= 26
ELSEIF character_code >= firstlowercase AND character_code2 > lastlowercase THEN
character_code2 -= 26
ENDIF
ENDIF
newstring = newstring + character from character_code2
ELSE
Page 3
Joshua Hadabora
2104
newstring = newstring + letter
ENDIF
ENDIF
PRINT newstring
Flowchart
Page 4
Joshua Hadabora
2104
Development Choices
For this next task I used the same tools, Python 3.4.1 and the IDLE GUI because Python is a
programming language that is very easy to program for, with access to tons of built in functions and
is still a very powerful language. Built in functions I used include ord(), chr() and
string.isalpha(), which help make programming easier because these functions do not need
to be programmed as well as the actual task itself. Programming these would make the code much
less efficient, more bloated and would likely cause more errors in the code that could be avoided
with these built in functions.
Here are all the built-in features I took advantage of in Python:
if
An if statement tests a condition and executes a block of code if the condition is true.
elif
This is a variant of an if statement which only executes if its condition is true and the previous if
statement was false.
else
This is a variant of an if statement which only executes if the previous if statement was false.
for
A for loop executes a block of code multiple times, and creates a variable that goes through a list
such as a range or a string for each loop. Can be broken out of with a try-except or a break.
while
A for loop executes a block of code multiple times, however it isn’t based off lists and instead
keeps looping as long as the statement is true. Can be broken out of with a try-except or a break.
raise
Unlike with the previous task, we’re going to use raise Exception to break out of while
loops, instead of using it to raise actual errors.
break
Breaks the last loop created.
try-except
Try-except blocks will try to execute blocks of code, First, the try block
code will execute. If an exception is not raised, the except block is not run. However, when an
exception is raised the except block is run.
pass
Pass is a null function. It does nothing, but is used to negate syntax errors that would occur when
a statement leads to nothing.
input
The input function allows users to input their own data into the program. The program stops
processing lines of code and the user is allowed to type in data. The function’s first parameter is a
string which will be asked when the user is able to type.
Page 5
Joshua Hadabora
2104
print
The print function gives an output to the user in the console.
int
Converts a string into an integer, for example it could convert the string “2” to the number 2 or
the string “-7” to the number -7. int returns an error if the string does not look like it can be
converted to a whole number.
def
Defines a function in a variable. These functions can then be called and execute the code put
inside.
ord
Gets the ASCII character code of a given character code.
chr
Turns an ASCII character code back into its character.
len
Returns how many characters make up a string.
str.isalpha
Check if the string only contains characters that are letters. We can give it a single character and it
will check whether it is alphabetical or not.
str.upper
Creates an entirely uppercase version of the string.
Testing
Planning
This code will have two groups of testing. One for testing the program while it is being developed
and another for testing the program’s final solution.
Testing while developing will allow looking into interesting solutions that can be made to overcome
problems the task has given and then adapting the solution to fit our context. These tests would also
make sure one part of the code works before we continue any further.
Testing the final solution will make sure that every aspect of the code that needs to work functions
correctly and we have a finished program.
Here are the tests we are going to carry out:
Test No.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
Development Tests
Using the input function to get a string from the user.
Checking for a blank string.
Allow the user to reattempt their input.
Checking if the string has a letter in it.
Checking if the keyword has a forbidden character.
Creating the decryption prompt.
Repeating the keyword to match the length of the string.
Creating the function.
Page 6
Joshua Hadabora
2104
2.3
3.1
3.2
3.3
3.4
Only encrypting letters in the alphabet.
Getting the character code of a letter.
Adding two characters together.
Adding decryption support.
Wrapping the resulting character around the alphabet.
Test No.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Final Solution Tests
Does the program keep asking for a string if entered incorrectly?
Does the program keep asking for the keyword if entered incorrectly?
Does the program keep asking for encryption or decryption if entered incorrectly?
Putting in the alphabet with a set keyword.
Decrypting the encrypted alphabet with the same keyword.
Encrypting a full sentence.
Decrypting the full sentence.
Double-layered encryption.
Development
Test 1.1: Using the input function to get a string from the user.
The first thing we need to develop is a way to get inputs from the user. Without this, the program
would just be useless. In Python, we can get input from a user with the input function which
returns what the user entered as a string. This means that we can make a variable equal to what
input returns, like this:
We are expecting this piece of code to output to the console exactly what the user entered.
Which is what happens, meaning that we’ve explored and successfully tested a method to get input
from a user.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Test 1.2: Checking for a blank string.
We can’t encrypt a blank string. That would be pointless, and could lead to breaking the code. This is
why we have to validate the user’s input to make sure they’ve actually entered usable data.
Building on our code from test 1.1, we can now expect the console to return “You left the string
blank.” if the user didn’t enter anything at all and just hit enter.
Page 7
Joshua Hadabora
2104
As you can see, entering nothing has the program tell the user that they’ve done exactly that.
Meanwhile, if the user has entered something the program prints exactly what the user entered.
This means the program now actively checks for blank strings.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Test 1.3: Allow the user to reattempt their input.
As a way of fool-proofing the program, we have to allow the user to re-enter and correct themselves
if they accidentally go wrong. We can’t just shut them out of the program if they make a little
mistake.
We are going to use a do-until loop to keep giving the prompt to the user while they’re inputting
data incorrectly. We are using a do-until loop because we want the user to enter something before
checking if it’s right. Do-until loops check an expression after performing an action, unlike do-while
loops which check the expression before-hand.
Python unfortunately does not have its own native do-until statement, which means we will have to
explore methods of implementing a do-until structure.
WET While Solution
A ‘WET’ program is a program that violates the ‘DRY’ rule, which stands for ‘Don’t repeat yourself’.
It is a rule that many programmers try to stick to. Unnecessary repetition makes code bigger in file
size and inefficient. However, since do-untils don’t exist in Python repeating yourself could be an
alternative.
It does indeed work, but repeating yourself in code is highly inefficient, as the program has to do
things multiple times when it is not necessary.
Page 8
Joshua Hadabora
2104
While True Loop Breaking
You can break any loop one layer at a time in Python with a break function.
This works by stopping the while loop the break is placed in. However, what you can’t do is break
multiple loops. Breaking more than one loop will be useful for later.
This program will never end, because we cannot break two loops.
Let’s look into other methods of a do-until loop.
While Flag is True
This creates a flag in the beginning of the while loop that is initially set to false. If the string is not
empty, we set the flag to true inside a while loop that only continues if the flag is false.
Page 9
Joshua Hadabora
2104
This method works. However, should we really be setting a new variable for each time we include
one of these user input while loops? We will be using a new flag variable or reusing the same one
three times.
Try-Except Blocks
We could technically use the try and except methods from Python to create a do-until loop. Tryexcept blocks try a piece of code in the try block and once an error has been detected, it goes to the
except block. If an error hasn’t been detected, it goes past the except block and continues the rest of
the code.
We can manipulate this to create a do-until loop.
When the string is not empty, the program raises an error manually to Python. Since we are
protected by the try block, we cancel out of the while true loop and go to the except block. The pass
function does nothing and lets us run through the rest of the code.
This method does not have the drawbacks of the other three. It is a DRY solution as it does not
repeat itself, it doesn’t create unnecessary variables and you can even break all current loops by
raising only one exception.
Page 10
Joshua Hadabora
2104
However, this method would take up a lot of space in the code. Especially since this must be done 3
times for all three variables. There’s arguments that can be made on whether the amount of lines in
a program matter or not. I personally think it does not matter because I believe it makes the code
much more readable by humans and it all gets compiled into machine code as efficiently as possible
regardless of the amount of lines anyway.
Chosen Method
I have chosen to go with the try-except method as it allows me to discover new ways of
implementing workarounds into programming. I have never used this method to create do-until
loops in programming language that don’t have their own. This will develop my skills as a
programmer.
As you have seen, the try-except method works perfectly with the only downside of taking up
slightly more file space due to the use of more lines.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Test 1.4: Checking if the string has a letter in it.
As well as encrypting a blank string, encrypting a string with only numbers, spaces and other special
characters would also be pointless. This is because the program is not going to encrypt these
characters and will keep these the same instead. This means we are also going to validate to check
whether the string only contains these kinds of characters.
This is why we need to be able to escape two loops, because we will be adding a for loop to check
whether the string contains no alphabetical characters.
The new for loop will check through each letter in the string, and will break both the for loop and the
while loop when it finds an alphabetical character. This continues the program without an error.
However, if no letters are found in the string. You see the printed error message and the while loop
continues.
As you can see, an error is now sent to the user if they either enter a blank string or don’t enter a
letter from the alphabet.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
Page 11
2.2
2.3
3.1
3.2
3.3
3.4
Joshua Hadabora
2104
Test 1.5: Checking if the keyword has a forbidden character.
Since we have already made a way to check for an empty string, we will use the same method to
check for an empty keyword.
However, we do have to program a method to make sure the keyword chosen by the user does not
have a space, number or special character at all.
This prints errors to the user if either the keyword is an empty string or the keyword contains a
number, space or special character.
As you can see, every string given in this test was handled correctly. No spaces, numbers or special
characters were allowed in the keyword.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
Page 12
2.2
2.3
3.1
3.2
3.3
3.4
Joshua Hadabora
2104
Test 1.6: Creating the decryption prompt.
Now we are tasked with interpreting a boolean from the user. The user will be asked whether they
want to encrypt or decrypt their string using the keyword. We are going to tackle this problem by
giving the user a bit of leniency.
When we ask the user whether they want to encrypt or decrypt, entering anything that begins with
the letter ‘e’ will be interpreted as the user wanting to encrypt the string. Meanwhile, entering
something that begins with the letter ‘d’ will denote decryption. Entering anything else will ask the
user to try again.
If the user has capitals in their answer, it makes it all lowercase to make checking for the first letter
easier.
This means that the user could enter the following strings to choose encryption or decryption:
Encryption
e
en
encrypt
encryption
Decryption
d
de
decrypt
decryption
This allows the user to have many ways to choose whether they want to encrypt or decrypt without
us needing to program validation for each individual choice.
As you can see, the user can enter anything relevant to tell the program to encrypt the string.
Page 13
Joshua Hadabora
2104
And these are examples of what can be entered for decryption.
The user can enter these strings in any case, as it all gets put into lowercase by the program.
Entering anything without a ‘d’ or an ‘e’ at the beginning will show an error message and ask the
user to try again.
This means we have successfully created a way for the user to input a boolean variable to the
computer.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Test 2.1: Repeating the keyword to match the length of the string.
We have to find a way to repeat the keyword to match the length of the user’s string, which will
make programming adding a letter from the string to one from the keyword much simpler.
Python is very special because it allows you to times a string to repeat it, which I have seen nowhere
else and I think is a very good idea.
First, we import the math module that comes with Python. This allows us to make a ceiling division
between the length of the user’s string and the keyword.
Then, we times the keyword string by the ceiling division. (In this case we do 16 divided by 7 which is
2.2857… and then that is turned into an integer of 3 by math.ceil.)
This means that the keyword should be repeated 3 times.
This means now if we get any position in the above string, we will be able to get a letter from both
that string and the keyword if needed.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
Page 14
2.2
2.3
3.1
3.2
3.3
3.4
Joshua Hadabora
2104
Test 2.2: Creating the function.
We are going to put our main encryption code in a function with this task, unlike in the Caesar
Cipher. The reason for this is to make an easier way to return letters to put in the final string and
quit processing in an easy way with the return function.
Functions are created with the def statement followed by a name. You can add local variables that
are required to be given to the function each time it is called. These variables can then be
manipulated and then even returned from the function.
This example function will be given a value from 1-10, and should return a value of 6-15 since the
number 5 is being added in the function.
Now we know how to pass variables to the function and also give back a variable, we can make our
own for the program.
However, we need to add code into the function first.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
Page 15
2.2
2.3
3.1
3.2
3.3
3.4
Joshua Hadabora
2104
Test 2.3: Only encrypting letters in the alphabet.
We want to leave characters that are spaces, numbers or special characters just as they are.
Encrypting them will just leave a lot of unsightly garbage. We need to test if characters are not in the
alphabet.
This code goes through each letter in the given string, and uses str.isalpha to determine
whether the character is alphabetical or not.
Here it has detected that the space in the middle of the string “Computer Science” is not an
alphabetical letter.
And here it has detected that the only letter in this string is the first character, ‘A’.
Page 16
Joshua Hadabora
2104
So now in our function, we can add:
Which will return the character straight away to be added to the final string.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Test 3.1: Getting the character code of a letter.
To encrypt a string using offset, we’ll need the help of the ASCII table and character codes.
In computers, each letter has its own character code. This is because computers only understand
either 0 or 1 and don’t have a concept of letters and writing. So we give letters their own numbers
so the computers can represent these codes as text.
We can use this system to our advantage, because we can get the character code of a letter, add or
take away from it, and turn it back into a letter. This will be explored next test.
We are expecting this chunk of code to ask the user for a string, and give the user back a list of every
character code in that string. The ord function turns a chracter into a character code.
The final outcome is a list of every character code in this string, which is what we need in order to
encrypt.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
Page 17
2.2
2.3
3.1
3.2
3.3
3.4
Joshua Hadabora
2104
Test 3.2: Adding two characters together.
Next we are going to have to use a function that turns a character code back into a character. This is
done with the chr function.
In the Caesar Cipher, it was simple to add a number to a character code and turn the resulting
number back into a character. However, this time we need to add two characters together. This
comes with a huge problem, a character code for a letter doesn’t start from 1. This means that
adding 65 (‘A’) to 69 (‘E’) isn’t going to give us ‘F’ like we expect. It will instead give us ‘å’ which is not
a proper English character and we don’t want it shown.
The way to fix this is by taking away 65 from the keyword’s character code. This means that 69 (‘E’)
will become 4. However, 65 (‘A’) + 4 = 69 (‘E’). We want ‘A’ + ‘E’ to become ‘F’. So in our code we will
add one to the keyword’s character code as well.
This little snippet of code runs through every character in the string and keyword, and creates a
modifier from the keyword character code. The keyword character is made uppercase because
otherwise I would have to implement more code to subtract from firstlowercase. This modifier is
then added to the character code, and turned back into a character using chr.
This test program runs no problem with most letters, however starts breaking when your letters go
out of the bounds of the alphabet.
This is not ideal, as entering a string with letters towards the end of the alphabet will make garbage
symbols which the task does not allow. It is clear that this task only wants letters to be encrypted in
this program.
The last development test will fix the issue.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
Page 18
2.2
2.3
3.1
3.2
3.3
3.4
Joshua Hadabora
2104
Test 3.3: Adding decryption support.
Until now, asking for decryption would just encrypt the string.
But now we can very easily add decryption support to our program. All we need to do is times the
modifier by -1. This makes the number negative and means that adding the modifier will be like
subtraction instead.
This is a ternary operation which are supported in Python. It only multiplies the modifier by -1 if the
user wanted the program to decrypt. This keeps the operation on one line instead of using lots of if
and else statements which aren’t needed.
As you can see, the string returned has definietly changed. Although it’s very hard to tell if it’s
decrypting properly at this state. The next test should confirm this for us.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Test 3.4: Wrapping the resulting character around the alphabet.
This final test in development is the biggest problem. Thankfully, we can recycle the code from the
Caesar Cipher.
The problem is as follows, currently decrypting the letter ‘W’ which has a character code of 87 and
adding 5 (‘E’) to it will end up to be an unwanted character.
1
2
3
4
5
The task requires that the alphabet wraps around itself. Uppercase letters must stay uppercase
letters and lowercase letters must stay lowercase.
Page 19
Joshua Hadabora
2104
This last addition to our code may seem quite complicated at a first glance, but I will now explain
how it works.
The first four variables are not meant to be changed, similar to constants. These variables are purely
to make the code easier to read and easier for me to program with. Without them, I’d have to put it
the specific character codes for ‘A’, ‘a’, ‘Z’ and ‘z’, which would be quite confusing for anybody to
look at as they wouldn’t know what that number is or why it was used.
Finally, this block of code is the most important. It checks if the letter has gone outside the alphabet
during encryption. These if statements will return true if the character was in the alphabet range but
strayed away from it during encryption.
charnum
lastuppercase
charnum2
charnum is less than or equal to lastuppercase.
charnum2 is greater than lastuppercase.
Therefore the if statement returns true.
1
2
3
4
5
When one of the if statements return true, all we do is simply takeaway the amount of letters in the
alphabet from the character code.
Page 20
Joshua Hadabora
2104
So the ‘\’ character from the example above has a character code of 92 (5C on the grid above). When
we take away 26 from it, we get 66. Converting 66 to a character gives us the capital letter B.
This test and the previous test has fixed the bugs in test number 3.2 and 3.3. It has been explained
how in this test and it will be proven using screenshots during the final solution tests. We have
created the full program and we can now test the final solution to see if it all works.
1.1
1.2
1.3
1.4
1.5
1.6
2.1
2.2
2.3
3.1
3.2
3.3
3.4
Final Solution
This is the final solution for a Caesar Cipher program, written in Python. The code is even heavily
commented so it is all explained.
Page 21
Joshua Hadabora
2104
We are going to test if the program is error proof, and can perform every single requirement without
fail.
Test 1.1: Does the program keep asking for a string if entered incorrectly?
This test will check to see whether the user is asked to enter the string again if they left it blank or
didn’t add any letters.
We need to validate this because otherwise it would be completely pointless running this program
since it would not change anything about the final string.
Nothing has changed, so the program shouldn’t be wasting the time and computer resources to run
through a loop and process these characters.
Page 22
Joshua Hadabora
2104
As you can see, entering nothing, a space, a number, a special character and a combination of the
last three results in getting an error message.
The error messages are even different depending on if you leave the space blank or don’t enter a
letter.
Of course as soon as a letter is given, the program works as intended.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 1.2: Does the program keep asking for the keyword if entered incorrectly?
Next, this test will check to see whether the user is asked to enter the keyword again if they left it
blank or added any number, space or special character. This is done to make sure the user’s string
encrypts correctly and does not end up showing any odd characters.
Shown above, the program allows the user to enter the keyword again if they make a mistake.
And a blank keyword gives a different error message.
When we enter a valid keyword, the program accepts it like it should.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 1.3: Does the program keep asking for encryption or decryption if entered
incorrectly?
The next variable to test is the variable that determines whether the program should encrypt or
decrypt the string. It is the most complicated variable of the three that the user has to input into the
program.
The user has to enter a word which starts with ‘e’ to encrypt the string and a word beginning with ‘d’
to decrypt the string.
Page 23
Joshua Hadabora
2104
Entering anything that doesn’t begin with ‘e’ or ‘d’ asks the user to try again.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 2.1: Putting in the alphabet with a set keyword.
These final 5 tests will check to see if the encryption works correctly. Since the engine is derived
from the Caesar Cipher solution, it should work fine.
For the first test we are going to enter the full alphabet with the keyword ‘GCSE’.
It works, every letter was shifted – including the letters that broke before development test 3.4.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 2.2: Decrypting the encrypted alphabet with the same keyword.
Although a method for decryption isn’t required, it is still a thing that most people would want the
program to achieve. If we couldn’t decrypt an encrypted message, what would be the point of
encrypting it? Decryption was easy to include, so it did not take up too much development time and
has the benefit of adding a whole new functionality to the program.
By telling the program to decrypt the message using the keyword provided, we’ve allowed the user
to decrypt a message created using this program or from an outside source that used the same
encryption method.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 2.3: Encrypting a full sentence.
Practically, nobody is going to want to encrypt the alphabet all the time. We will now test to see if a
full sentence will encrypt. Like with the Caesar Cipher test that also encrypted a full sentence, this
test will use another pangram to test the encryption engine fully. We will use the sentence
“We promptly judged antique ivory buckles for the next prize.” because it also has all the letters of
the alphabet, a capital letter, many spaces and a full stop. This will test if the special characters and
spaces are able to make the program crack.
We will use the keyword “pangram”.
Page 24
Joshua Hadabora
2104
The program flawlessly passes the test. It keeps the capital letter at the start of the sentence a
capital letter, correctly shifts all of the letters and perfectly deals with spaces and the full stop at the
end.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 2.4: Decrypting the full sentence.
We need to make sure the program is capable of decrypting a full sentence it originally encrypted.
We’re expecting “We promptly judged antique ivory buckles for the next prize.”
This is the last test that checks for errors within the program. The Keyword Cipher passed all of these
tests without fail.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Test 2.5: Double-layered encryption.
This test is an extra test. It’s here to give some insight and evaluation on how we could accomplish
the third and final task – the Double-Keyword File Cipher. It requires two keywords to encrypt the
program. This test can also check whether we can encrypt the string more than once, which is not
required by the task but is useful to have.
Page 25
Joshua Hadabora
2104
Even two different keywords work. With the decryption done out of order.
This test shows that the user can encrypt their string as many times as they want, which gives the
string an added layer of protection. Plus, it lets me know that I can just work out the same thing
twice in the same engine for task 3 instead of making drastic changes to the engine.
1.1
1.2
1.3
2.1
2.2
2.3
2.4
2.5
Recap
To recap, here are all the tests that were performed in a final table.
Development Tests
Development of this program was not challenging, although the code can be seen as quite long and
complicated. The comments help reduce the complication.
Test No.
1.1
1.2
1.3
Test
Using the
input function
to get a string
from the user.
Checking for a
blank string.
Allow the user
to reattempt
their input.
Inputs
“Computer
Science”
Expected Outcome
“Computer
Science” as an
output.
Actual Outcome
“Computer
Science” as an
output.
“”
“You left the string
blank.”
“Computer
Science”
“You need to enter
a string. Please try
again.” – allow
reattempt
“Computer
Science” - finish
“You left the string
blank.”
“Computer
Science”
“You need to enter
a string. Please try
again.” – allow
reattempt
“Computer
Science” - finish
“Computer
Science”
“”
“Computer
Science”
Page 26
Passed?
Joshua Hadabora
2104
1.4
Checking if the
string has a
letter in it.
1.5
Checking if the
keyword has a
forbidden
character.
1.6
Creating the
decryption
prompt.
2.1
Repeating the
keyword to
match the
length of the
string.
Creating the
function.
Only
encrypting
letters in the
alphabet.
2.2
2.3
“”
“You need to enter
a string.”
“5 ;”
“You did not enter
a character from
the alphabet.”
“Computer
“Computer
Science”
Science”
“”
Denied.
“123”
Denied.
“ABC123”
Denied.
“123ABC”
Denied.
“; 5”
Denied.
“ABC”
“ABC”
“e”
False.
“en”
False.
“encode”
False.
“encrypt”
False.
“encryption”
False.
“d”
True.
“de”
True.
“decode”
True.
“decrypt”
True.
“decryption”
True.
“no”
“You can only
choose encryption
or decryption.”
“CompSci” and “CompSciCompSci
“Computer
CompSci”
Science”
“You need to enter
a string.”
“You did not enter
a character from
the alphabet.”
“Computer
Science”
Denied.
Denied.
Denied.
Denied.
Denied.
“ABC”
False.
False.
False.
False.
False.
True.
True.
True.
True.
True.
“You can only
choose encryption
or decryption.”
“CompSciCompSci
CompSci”
N/A
N/A
N/A
“Computer
Science”
A list of whether
each letter in
Computer Science
is a letter or not.
A list of whether
each letter in
Computer Science
is a letter or not.
(e.g. True, true,
true, true, true,
true, true, true,
false, etc.)
A comma
separated list of
ASCII character
codes:
(e.g.
67,111,109,112,
117,116,101, etc.)
“Jru|wxw Vvnlqvj”
3.1
Getting the
character code
of a letter.
“Computer
Science”
A comma
separated list of
ASCII character
codes.
3.2
Adding two
characters
together.
“Computer
Science” and
“GCSE”
“Jrfubwxw Vvnlqvj”
Page 27
Fixed in
Test 3.4
Joshua Hadabora
2104
3.3
3.4
Adding
decryption
support.
Wrapping the
resulting
character
around the
alphabet.
“Computer
Science” and
“GCSE”
N/A
“Vltknqlm Pjdxkjz”
“<lZknqRm
PPd^Kp`’”
N/A
N/A
Inputs
“”
Expected Outcome
“You need to enter
a string.”
“You did not enter
a character from
the alphabet.”
Same as above.
Same as above.
Same as above.
Asks to try again.
Asks to try again.
Asks to try again.
Asks to try again.
Actual Outcome
“You need to enter
a string.”
“You did not enter
a character from
the alphabet.”
Same as above.
Same as above.
Same as above.
Asks to try again.
Asks to try again.
Asks to try again.
Asks to try again.
Asks to try again.
Asks to try again.
Denied.
Denied.
Denied.
Denied.
Denied.
Denied.
“ABCDEFGHIJ
KLMNOPQRST
UVWXYZ” and
“GCSE”
“HEVILIZMPM
DQTQHUXULY
BYPCFC” and
“GCSE”
“HEVILIZMPMDQT
QHUXULYBYPCFC”
“HEVILIZMPMDQT
QHUXULYBYPCFC”
“ABCDEFGHIJKLMN
OPQRSTUVWXYZ”
“ABCDEFGHIJKLMN
OPQRSTUVWXYZ”
“We promptly
judged
antique ivory
buckles for the
next prize.”
and
“pangram”
“Mf wjpzfuzf
khthsk bajjebw
vlpff chslzlk ses azf
dfla qeyas.”
Encrypted
sentence with
spaces and full stop
intact.
“Mf wjpzfuzf
khthsk bajjebw
vlpff chslzlk ses azf
dfla qeyas.”
Fixed in
Test 3.4
Final Solution Tests
Test No.
1.1
1.2
1.3
2.1
2.2
2.3
Test
Does the
program keep
asking for a
string if
entered
incorrectly?
Does the
program keep
asking for the
keyword if
entered
incorrectly?
Does the
program keep
asking for
encryption or
decryption if
entered
incorrectly?
Putting in the
alphabet with
a set keyword.
Decrypting the
encrypted
alphabet with
the same
keyword.
Encrypting a
full sentence.
“5”
““
“;”
“5 ;”
“123”
“abc123”
“5 ;”
“Computer
Science”
“@@@@@@
@@”
“”
“5”
“;”
Page 28
Passed?
Joshua Hadabora
2104
2.4
Decrypting the
full sentence.
2.5
Doublelayered
encryption.
“Mf wjpzfuzf
khthsk
bajjebw vlpff
chslzlk ses azf
dfla qeyas.”
and
“pangram”
“DoubleKeyword File
Cipher” and
“taskthree”
“DoubleKeyword File
Cipher” and
“taskthree”,
“GCSE”
“We promptly
judged antique
ivory buckles for
the next prize.”
“We promptly
judged antique
ivory buckles for
the next prize.”
“Xpnmfm-Pjsxhcx
Xnqy Vtjpww”
“Rqgxzu-Uomyanr
Psvs Oedxob”
“Xpnmfm-Pjsxhcx
Xnqy Vtjpww”
“Double-Keyword
File Cipher”
“Xpnmfm-Pjsxhcx
Xnqy Vtjpww”
“Esgrmp-Uqvqmja
Cutr Cwcudz”
“Krngsh-Plbptyg
Kpox Jlimlu”
“Double-Keyword
File Cipher”
“Xpnmfm-Pjsxhcx
Xnqy Vtjpww”
“Rqgxzu-Uomyanr
Psvs Oedxob”
“Xpnmfm-Pjsxhcx
Xnqy Vtjpww”
“Double-Keyword
File Cipher”
“Xpnmfm-Pjsxhcx
Xnqy Vtjpww”
“Esgrmp-Uqvqmja
Cutr Cwcudz”
“Krngsh-Plbptyg
Kpox Jlimlu”
“Double-Keyword
File Cipher”
Evaluation
Meeting Requirements
The program has been designed to fulfil the task requirements as well as test my skills in developing
features that I felt were essential such as validation and decryption.
Screenshots were created throughout the extensive testing to provide evidence that the program
works as intended. All these tests, roughly based on the requirements, eventually passed.
Below is a table to show every requirement and the tests that fulfilled it.
Requirement
The string can include special characters like
numbers and symbols, but these will not be
encrypted and will stay the same throughout
the process of encryption.
Uppercase letters will remain uppercase and
lowercase will remain lowercase. (The
alphabet must wrap around itself.)
The encrypted string will be displayed after
this process.
The string must not be blank.
The keyword must not be blank or have any
spaces, numbers or special characters.
The user must be allowed to reattempt any
mistakes they have made inputting data.
Tests
Development, Test 2.3.
Development, Test 3.4.
Final Solution, Test 2.1.
Development, Test 1.2.
Development, Test 1.5.
Development, Test 1.3.
Page 29
Joshua Hadabora
2104
Conclusion
In conclusion, this report has proved that this program has met all the requirements made by the
task, even throwing in extras such as validation and decryption. This program could easily be used by
anybody. I’ve also learned that I should not create errors in my program which I did in the last task.
This is not user friendly.
Page 30
Download