MaximumPC 2004 09

(Dariusz) #1

SEPTEMBER 2004 MA XIMUMPC 


5 (continued)


the txtPlaintext control is a box the
user can type text into when he or she
runs the program. We can use one of
the TextBox class’ methods, Trim(), to
remove the leading and trailing spaces
from the Text property. Then we use
the Length property to fi gure out the
length of the text the user entered. If
after trimming the blank spaces from
the sides of the data in the txtPlaintext
control, the length is zero, then we
know the user hasn’t typed anything
into txtPlaintext at all and there’s
nothing to encrypt!
If this happens, we notify the user
that there’s a problem using the
MessageBox class, which displays a
popup message. Look for the line that
begins “MessageBox.Show(“Enter data
to be encrypted”). This line of code is
the Show method of the MessageBox
class in action. Here’s how it breaks
down: “Enter data to be encrypted” is
the error message displayed, “Error”
is the title of the popup window that
will be displayed, MessageBoxButtons.
OK is a constant that specifies that
only an OK button will be displayed,
and MessageBoxIcon.Error is the icon
that will be shown. Visual Studio’s
IntelliSense feature will show you the
syntax of the Show method as you
type the code.


If the user did enter some text,
control passes into the “txtPlaintext.
Text.Trim().Length > 0” If statement.
Once there, the line of code contain-
ing “If ValidateKey(txtKey.Text.Trim())”
is evaluated. If ValidateKey is false,
then we’re going to inform the user
that the data is invalid using another
MessageBox. However, if the value
returned from the ValidateKey func-
tion is true, then we’ll proceed with
encrypting the data that the user
entered into the txtPlaintext textbox.
To kick off the encryption pro-
cess we’ll instantiate an instance of
the Cryptography class—in simple
terms, we’ll tell the program to begin
encrypting the text in txtPlaintext—
using the code
“Dim Crypto As New Cryptography”
and then set the Crypto object’s key

property with the code
“Crypto.Key = CInt(txtKey.Text.Trim()).”
Notice that there is a call to a VB.NET
function named CInt. This data type con-
version function transforms input to an
integer value. We must do this because
the Text property of the txtKey TextBox
returns a string but the encryption key
must be an integer. If you tried to set
the Key property to txtKey.Text’s value,
you’d get an error when you tried to
compile your program.
Now that the encryption key is set,
we’re ready to encrypt something.
Using the value of txtPlaintext.Text
as a parameter, we call Crypto’s
Encrypt method and then assign the
value returned from that call to the
txtCiphertext textbox. The encoded text
can then be viewed in txtCiphertext. Tidy
up the form with a call to txtPlaintext.
Clear(), which removes the unencrypted
text from the Plaintext textbox. We’re
going to leave the text property of txtKey
unchanged so the user can just click the
Decrypt button and check that the just-
generated ciphertext is valid. The validity
of the ciphertext is confi rmed if the data
in txtCiphertext, when decrypted, is the
same data that was originally input.
The implementation of the
btnDecrypt_Click event handler is
similar to the btnEncrypt_Click handler.
Both functions work the same way, but
some names have been changed. In the
btnDecrypt_Click code, txtCiphertext
and txtPlaintext reverse functions;
txtCiphertext provides input and
txtPlaintext receives output rather
than the other way around. The only
other major change is a call to Crypto’s
Decrypt method rather than the
Encrypt method.

The MessageBox provides the means to
notify users of important events.

We’ve really just scratched the sur-
face of Windows Forms program-
ming in this How-To. There’s more
going on behind the scenes than
we’ve covered in this article, but you
should now have the skills to explore
further on your own. For example,
poke around Form1 and figure out
how to link btnExit to your Esc key.
Next time we’ll show you how to
use the .NET API to read text from
files, encrypt it, and output it as an
encrypted file. n

6 You’re done


SAMPLE CODE FROM FORM 1.VB
Here’s the important snippet of code that makes your Encrypt button work. Step
5 explains the function, but if you want a line-by-line breakdown explaining what
each line does, you should open Form1.vb and dig the comments.

Private Sub btnEncrypt_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnEncrypt.Click
If txtPlaintext.Text.Trim().Length > 0 Then
If ValidateKey(txtKey.Text.Trim()) Then
Dim Crypto As New Cryptography

Crypto.Key = CInt(txtKey.Text)
txtCiphertext.Text = Crypto.Encrypt(txtPlaintext.Text.
Trim())
txtPlaintext.Clear()
Else
MessageBox.Show(“You have entered an invalid key.”,
“Error!”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show(“Enter data to be encrypted.”, “Error!”,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Free download pdf