How to do it...

  1. Open Visual Studio 2017.
  2. Now open the solution we built from the previous recipe.
  3. The Solution Explorer should look like this:
  1. Now, let's select the solution name and right-click.
  2. From the menu, select Add | New Project.
  3. In the New Project dialog box, expand the Visual C# node and select Windows Classic Desktop in the left-hand pane.
  1. In the right-hand pane, select Windows Forms App (.NET Framework):
  1. Now, in the Name: textbox, type Chapter3.SecureFile.SecureWinApp, leave the Location: textbox as it is, and click OK.
  1. Now, the Solution Explorer should look like this:
  1. Let's rename Form1.cs as MainForm.cs, by selecting it and pressing F2.
  2. Answer Yes in the confirmation box to confirm the changing of the main class name as well.
  3. Now select the Windows form from the designer window.
  4. Drag and drop two Buttons in the form.
  5. Also drag and drop an OpenFileDialogBox control in the Windows form.
  6. Change the properties of the previous controls as per this table:
    Control Property Value
    Button Name EncryptButton
    Button Text Encrypt
    Button Name DecryptButton
    Button Text Decrypt
    OpenFileDialogBox Name OpenDialog
    OpenFileDialogBox Filter Text Files|*.txt
  1. Now, your designer area should look like this:
  1. Now select the Chapter3.SecureFile.SecureWinApp label in the Solution Explorer and expand it.
  2. Right-click on the References label and select Add Reference.
  3. Click on the Projects node in the Reference Manager dialog box. 
  1. Check the check box in front of the Chapter3.SecureFile.CryptLib label from the project list in the right-hand pane.
  1.  Click OK.
  2. Now switch back to the design window and double-click on the Encrypt button to open the code window.
  3. In the code window, scroll up to the top and add the following using directive as the last line of the using block:
      using Chapter3.SecureFile.CryptLib;
  1. Now scroll down to the Encrypt button, click, and add the following code in between the curly brackets:
      if (OpenDialog.ShowDialog() == DialogResult.OK)
{
var textFileName = OpenDialog.FileName;
var secureFile = new CryptFile(textFileName);

secureFile.EncryptFile();

MessageBox.Show("File encrypted", "Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
  1. Now switch back to the designer window by clicking on the MainForm.cs [designer] tab.
  2. Double-click on the Decrypt button to reach the code for that button click.
  3. Add the following code in between the curly brackets of the button click code:
      if (OpenDialog.ShowDialog() == DialogResult.OK)
{

var textFileName = OpenDialog.FileName;
var secureFile = new CryptFile(textFileName);

secureFile.DecryptFile();

MessageBox.Show("File decrypted", "Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
  1. Now press F5 to execute the code (make sure the classic Windows application project is the default project)
  2. Press the Encrypt button and select a file:
  1. Click Open and click OK in the information box.
  2. Now open Windows Explorer and navigate to the location of the file that you just encrypted.
  3. You will notice a lock on the file.
  1. Now switch back to your app, click on the Decrypt button, and follow the same steps as before.
  1. Now the lock is removed from the file.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.189.177