Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

338 Part II Programming Fundamentals


If you open this file in Microsoft Word or another text editor, you’ll see the same
result—the characters in the file have been encrypted to prevent unauthorized reading.


  1. To restore the file to its original form, choose the Open Encrypted File command on the
    File menu, and then open the Padua .txt file in the C:\Vb10sbs\Chap13 folder.
    The file appears again in its original form, as shown here:

  2. On the File menu, click the Exit command to end the program.


Examine the Encrypt program code


  1. Open the mnuSaveAsItem_Click event procedure in the Code Editor to see the program
    code that produces the encryption that you observed when you ran the program.
    Although the effect you saw might have looked mysterious, it was a very
    straightforward encryption scheme. Using the Asc and Chr functions and a For loop,
    I simply added one number to the ASCII code for each character in the text box and
    then saved the encrypted string to the specified text file.
    The entire event procedure is listed here—in particular, note the items in bold:


Dim Encrypt As String = ""
Dim letter As Char
Dim i, charsInFile As Short

SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
'save text with encryption scheme (ASCII code + 1)
charsInFile = txtNote.Text.Length
Free download pdf