# A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Skeleton code/2013 Exam/Section D
## Potential Questions

1. You could be asked to add an "r." function to reset the Plaintext and the Ciphertext variables.
This is the VB.net Solution
Answer:
This is the Python Solution
Answer:
2. There is unnecessary code in the EveryNthCharacterSteganography function and GetTextFromFile. There is no need to pass the CurrentPosition twice from the EveryNthCharacterSteganography function. In the GetTextFromFile function this means the second loop is not required as it means the start and end position are the same thing. Of course it could mean that you will be asked to add a variable in which the user will set how many characters should be grabbed from the file each time. The loop, although not needed right now would then be needed and current position, currentposition+numberofcharacters would be passed through instead
This is the Pascal/Delphi solution:
Answer:
Amend the call to GetTextFromFile from EveryNthCharacterSteganography function so that it only passes the current position once. Then amend GetTextFromFile to this:
This is the Vb.Net solution:
Answer:
Amend the call to GetTextFromFile from EveryNthCharacterSteganography function so that it only passes the current position once. Then amend GetTextFromFile to this:
3. Currently the menu option will not accept capital letters. Modify the code to allow this.
This is the Visual Basic solution:
Answer:
Alternative answer
This is the Pascal/Delphi solution:
Answer:
Alternate Solution
This is the Python solution:
Answer:
4. You may be asked to validate the users choice to make sure it is valid.
This is the Visual Basic Solution:
Answer:
Or, if you don't want to waste a load of time doing a pointless loop where the code needs to be changed every time if a new menu choice is added, look below:
To validate the user's choice:
However, the above choice is better because it constantly prompts the user to enter a new choice until a valid one is entered. Using the example below means you will have to close the program and start it up again if you enter an invalid choice which is stupid and will not get you any marks as it only tells the user their choice is invalid and does not prompt them to enter a valid one!
Actually, you are wrong. The menu loops until;
Therefore it does actually work as the menu will just loop again. Actually try it!
To validate the user's choice:
This is the Pascal/Delphi solution:
Answer:
Alternatively, using a set:
5. The Caesar Cipher Encryption does not work for keys lower than -26. Fix this. Nor of course for keys over +26
This is the Visual Basic Solution:
Answer:
This is the Pascal/Delphi Solution
Answer:
6. If a Zero or negative value of N is used the program will get stuck in an infinite loop - preventing this by requiring a positive value to be entered is easy and quite possible they will ask.
This is the Pascal/Delphi Solution
Answer:
This is the Visual Basic Solution
Answer:
7. There are several other examples where the range of an integer input is not checked but ought to be: StartPosition should be positive, EndPosition should be greater than StartPosition, the Caesar key should be between 1 and 25. These could all be straightforward questions. Perhaps write a generic range-checking function?
This is the Pascal/Delphi solution. It is a general function that can be called anywhere you need to check a number. It returns 0 if the number is not valid. This means it returns 0 if it is not a number and 0 if it is the number 0. This is because 0 is invalid even it if is a number as all of the numbers required in the system should be greater than 0.
Answer:
An alternate form uses the Val procedure to avoid creating an exception
You can then call this anywhere eg as part of GetSizeOfRailFence. GetSizeOfRailFence has also been amended here to ensure that either plaintext or ciphertext exist, depending on what option the user has selected from the menu. I deemed the rail fence size invalid if no ciperh/plaint text existed or if the size was less than or equal to 1 as could see no point in having a rail fence of 1 as would just give it as it was originally.
This is the VB.Net solution. It is a general function that can be called anywhere you need to check a number. It returns 0 if the number is not valid. This means it returns 0 if it is not a number and 0 if it is the number 0. This is because 0 is invalid even it if is a number as all of the numbers required in the system should be greater than 0.
Answer:
An alternate form uses the TryParse procedure to avoid creating an exception
8. Cannot see this being something the students need to do but if anyone is interested here is the code to crack diary.txt.
Answer:
9. The Caesar Cipher Key (N) could also be validated using a "Type check" (signed integer), a "Range check"(-25 to +25), and a "Presence check"
Answer:
This is the Pascal/Delphi solution.
Answer:
10. The current menu has some missing letters/options (c, f, i, l and m). Maybe they could ask the following ...
Answer:
11. Add a new PLAINTEXT option (c) to read the plaintext from a text file. The user should choose the filename.
Answer:
Pascal/Delphi answer::
12. Add a new CIPHERTEXT option (f) to read the ciphertext from a text file. The user should choose the filename.
Answer:
13. Add a new ENCRYPT option (i) to write the ciphertext to a text file. The user should choose the filename.
Answer:
14. Add a new DECRYPT option (l) to write the plaintext to a text file. The user should choose the filename.
Answer:
15. Option (m). What could this be used for?
Answer:
16. Possibly add a new cipher altogether; some of the letters may also have been omitted for the purpose of confusing students.(e.g. vignere – but this requires a lot of work to set up).
Answer:
17. the steganography function can crash when you dont put sensible data in – so I guess we need to check file sizes and stuff.
Answer:
18. Maybe the option to multiple encrypt for added security . I.e. encrypt the encrypted text or combining different algorithms?
Answer:
19. You can improve the caesar algorithm by randomizing the transposition (instead of it jumping by the same amount. If you ‘randomize(encryption_key)’ before generating random values both sender and receiver will get the same series of random transpositions)
Answer:
20. Add a new section to the menu called 'CRACK' with the options to crack either the 'ceaser' or 'rail fence'. Get some encrypted text from a fellow student and see (without knowing the encryption key) what the message is. In the case of the ceaser cipher you need to try 52 different keys (-26 to 26). In the case of the rail fence you need to try each value up to the number of characters in the cyphertext string. Make sure you utilise the existing decryption functions!
Answer:
This Code will crack RailFence
To edit the code to crack the ceaser cipher: (By Quoc-Hau Pham. Student of CTKSFC Lewisham)
Or you can do a function to crack the Caesar cipher.:
'p' is the case I used for the menu, which can be changed. Just enter an encoded rail fence and it'll run all the possible rail numbers. Remember to add the option in your menu so a user can use it. NOTE: There is currently a bug where the program has trouble working with codes that haven't been encrypted in the same session. I'm working on a fix. If you fix it yourself, feel free to overwrite this. Fixed - it was decrypting the cyphertext variable whist assigning user input into plaintext
For cracking the Caesar cipher:
Caesar crack in pascal/delphi:
Rail fence crack in pascal/delphi:
21. You can give a negative starting position and it returns the first character of the file repeatedly example: MMMMMMMMMMMMMMMaia rolled her eyes
Answer:
22. For the Caesar Cipher, the Rail Fence and the input for StartPosition, EndPosition and N, it doesn't check that it is an integer.
Answer:
The "userInput" variable can be used to validate the StartPosition and EndPosition parameters in the GetPositionToUse procedure as demonstrated above.
23. The EndPosition of the steganography section can be greater than the end of the text file.
Answer:
Pascal solution
24. If the number of lines in the rail text entered is greater than the number of characters in plain text then the encryption doesn't work.
Answer:
VB.NET Solution
25. Add a parameter to the GetTextFromFile function to replace the hard-coded 'diary.txt" file name. Pass the file name to the function when it is called. Ask the user for the file name when option n is chosen.
Answer:
26. Combine the functions UseCaesarCipher, GetTypeOfCharacter and ApplyShiftToASCIICodeForCharacter into one function, that can also apply the cipher to numbers. Furthermore this function should also include any modifications to allow the entry of minus numbers under -26.
Answer:
Credit to Ashley Tucker

## Code provided by AQA is linked here

link on gmshaw's site
The file path issue should be able to be corrected by your teacher and they can agree the change with AQA and provide code that will find the file. This happens most years in VB6 and the simple addition of App.path & "\ to the front of the filename solves the problems as long as the file is in the same location as the program - AQA will agree to this change to be made.

### Solutions

Stop program from freezing with N option - by iPhoneK1LLA (Like A Sir on Student Room)
If you run the raw code in Visual Studio, select option n, type 1, enter key, 208, enter key, 9, enter key, the application will crash. The solution to this problem is to view the 'GetTextFromFile' function
You need to look at this line
If you try and run this it will freeze. This needs to be changed to the FULL file location, so, for mine it would be
Run the program again and you'll see that the program now gives you 'MeeT ME at the coLD room'

### Comments

1. Who put this all into 1 file? I was the first one to put it onto the site and it was all split up into its different subs, then someone changed it into one big chunk of text :( - iPhoneK1LLA
    You can split it up using <syntaxhighlight lang="vbnet"> and </syntaxhighlight>
       Also note that the code is probably copyrighted by the AQA and may not be allowed on wikibooks. Discussion around the code and potential questions are fine, for the moment use the link above to access it for all languages. Pluke (discuss • contribs) 08:52, 5 March 2013 (UTC)
2. This wasn't a problem running under Delphi/Pascal, I've just loaded the code and entered n, 1, 208 etc and it printed the meet me in the cold room, no crash??? CB
3. iPhoneK1LLA can you absolutely confirm the crash because "N=9 Start=1 End=208" works fine in python 2.5.6 OP
4. I'll upload a video in a second running the raw code for VB.Net - iPhoneK1LLA
5. It's not really a crash - it just can't find the file. Either alter the file path as has been suggested or place the diary.txt file into the debug/bin folder and it will work fine. There have been 'problems' like this in previous years. NK
    or you can set the copy to output setting in the propertys to copy allways 31.222.208.187 (discuss)
6. In relation to above, the program only crashes if the file is not present, or the file contains no text, which isn't a problem for the exam, but if you would like to make the program more robust than you need to add validation to check if the file exists and that the file contains more than 0 characters, such as a Catch and Try statement, or the following:
7. There needs to be solutions for python.