ENGR/CS 101 CS Session Lecture 7

advertisement
ENGR/CS 101 CS Session
Lecture 7

Log into Windows/ACENET (reboot if in Linux)

Start Microsoft Visual Studio 2010

Load project from last class

Questions?
Lecture 7
ENGR/CS 101 Computer Science Session
1
Outline



Problem: Use a GUI to enter input from a file
and save results to a file. Also prevent the
program from trying to do an encipherment if
any input is missing or incorrect.
C# programming language

MessageBox

File Dialogs

File Streams
Deciphering the Caesar shift cipher
Lecture 7
ENGR/CS 101 Computer Science Session
2
Problem Specification

Today we will extend the program from the last
class do the following:




Prevent the program from trying to do an
encipherment when the input is missing.
Allow the user to load a plaintext message from a
file by clicking a button.
Allow the user to save the ciphertext message to a
file by clicking a button.
Bonus: extend the program to decipher
ciphertext back to plaintext.
Lecture 7
ENGR/CS 101 Computer Science Session
3
Interface Mockup


Same as last time
Add buttons to load
plaintext from a file,
and save ciphertext to
a file
Lecture 7
ENGR/CS 101 Computer Science Session
4
MessageBox


Often want to notify user
when something is missing.
E.g., not filling in shift key
textbox before enciphering.
Use MessageBox to pop up
a message, then return to go
back to waiting.
if (keyBox.Text == "")
{
MessageBox.Show ("Missing shift key!");
return; // go back to waiting
}
Lecture 7
ENGR/CS 101 Computer Science Session
5
In-class Exercise, Part 1



Add the code from the previous slide to the
Encipher button handler so that the program
does not try to do an encipherment when the
shift key box is empty.
The code goes at the beginning of the handler
function before any other code.
Bonus: add a second check that the user has
entered an uppercase letter in the key box. If
not, the program should pop up a message box
stating the error and go back to waiting.
Lecture 7
ENGR/CS 101 Computer Science Session
6
Reading from a File


Typing in messages by hand is tedious. One
can imagine that messages are already in a
textfile, so we would like to be able to load a
textfile into our application by clicking a button.
The design of the Click handler for this button
would be:
1. Create an OpenFileDialog object
2. Make the dialog pop up and wait for the user to respond
3. Check if user canceled. If so, go back to waiting.
4. Attach the chosen file to a StreamReader object
5. Read the contents of the file and store it into the plaintext
box.
Lecture 7
ENGR/CS 101 Computer Science Session
7
OpenFileDialog


An OpenFileDialog is
the window that pops
up when a user wants
to open a file.
Declare a variable
and create a new
object using the C#
new command
OpenFileDialog fileChooser = new OpenFileDialog();
Lecture 7
ENGR/CS 101 Computer Science Session
8
OpenFileDialog

To make the dialog pop up and get the file
name from the user, call the ShowDialog
function and save the result in a DialogResult
variable
DialogResult result = fileChooser.ShowDialog();

Check if the user clicked "Cancel" and if so, go
back to waiting
// Check if user clicked Cancel
if (result == DialogResult.Cancel)
{ return; // Go back to waiting
}
Lecture 7
ENGR/CS 101 Computer Science Session
9
File Streams

A file stream is a program object that is opened
(i.e., attached to a physical file) when it is
created. StreamReader objects are used to
read data from a file:
// Open (attach) the file chosen in the dialog
StreamReader fileInput = new StreamReader (fileChooser.FileName);
// Read entire file contents into plaintext box
plaintextBox.Text = fileInput.ReadToEnd();
fileInput.Close(); // Detach the physical file
ciphertextBox.Text = ""; // Clear ciphertext box
Lecture 7
ENGR/CS 101 Computer Science Session
10
In-class Exercise, Part 2


Add a Load button to your program's interface.
Change its Name property to "loadButton".
Double-click on the button to get its Click
handler function stub.
Between the { }'s, type in all of the code from
the previous 3 slides. I.e., starting with
OpenFileDialog fileChooser = ...
at the bottom of Slide 8.
Lecture 7
ENGR/CS 101 Computer Science Session
11
In-class Exercise, Part 2


The squiggly red lines under "StreamReader",
means that the program needs to import the
library that defines StreamReader. Do this by
right-clicking on "StreamReader", selecting
"Resolve", then "using System.IO".
Run and test your program until it works
correctly.
Lecture 7
ENGR/CS 101 Computer Science Session
12
In-class Exercise, Part 3


Add a Save button to your program. Change its
Name property to "saveButton". Double-click on
this button to get its Click handler function stub.
The Save button handler should save the
ciphertext to a file.

Lecture 7
Getting a file name for saving a file is done with a
SaveFileDialog. It works exactly the same way as
the OpenFileDialog for getting a file name from the
user. (I.e., it is the same code with SaveFileDialog
instead of OpenFileDialog.)
ENGR/CS 101 Computer Science Session
13
In-class Exercise, Part 3

To write to a file, we use a StreamWriter object.
Again the code to open the file is essentially the
same as in the Load button handler with
StreamWriter instead of StreamReader
// Open (attach) the file chosen in the dialog
StreamWriter fileOutput = new StreamWriter (fileChooser.FileName);

We can write the Text property of the ciphertext box
to the StreamWriter object using the WriteLine
function (as we did to the console)
// Write ciphertext to file
fileOutput.WriteLine (ciphertextBox.Text);
fileOutput.Close(); // Detach the physical file
Lecture 7
ENGR/CS 101 Computer Science Session
14
Deciphering



One of the reasons the Caesar shift cipher is
not a good method for keeping secrets is that it
is easy to decipher when you know the key.
We could decipher by writing a ShiftLetterBack
function that is just like the ShiftLetter function
except that it subtracts the shift number rather
than adding it.
Unfortunately, the % (modulus) operator only
works reliably on positive numbers.
Lecture 7
ENGR/CS 101 Computer Science Session
15
Deciphering



But in modular arithmetic, subtracting is the
same as adding its complementary number
(modulus base minus the number). In our
case, this will be 26 minus the shift number.
For example, shift key 'I', gives a shift number
of 8. The complementary shift number for
deciphering is 26 - 8 = 14. (This is equivalent
to a shift key of 'S'.)
This means that we can use the ShiftLetter
function to decipher by giving it the
complementary shift number for the second
argument.
Lecture 7
ENGR/CS 101 Computer Science Session
16
Deciphering


Bonus exercise: Add a Decipher button and handler to
your program that deciphers the text in the plaintext
box and put the plaintext result in the ciphertext box.
That is, any input is provided in plaintextBox and any
output is displayed in ciphertextBox. (You might want
to change the labels.)
The Decipher button handler will be the same as the
Encipher button handler except

Lecture 7
it computes the complementary shift number and
passes this as the second argument to the
ShiftLetter function to compute the plaintext letter
equivalent
ENGR/CS 101 Computer Science Session
17
Download