Make a notepad application that uses encryption in C#

encryption notepad

The example Make a password manager in C# shows how you can use encryption to manage passwords safely. This example uses a much simpler approach, although it provides fewer features. This program lets you save information in a RichTextBox into an encrypted file. When the program starts, it prompts you for a password, uses it to decrypt the file, and displays the file’s contents. When you close the program, it encrypts the contents of its RichTextBox and saves the result in the file. (If the file doesn’t exist when you start the program, the program uses the password you enter later when it saves the file.)

The following code shows how the program loads the saved data when it starts.

private const string NOTES_FILE = "Notes.dat";
private string Password;
private bool SaveChanges = false;

// Load the encrypted notes.
private void Form1_Load(object sender, EventArgs e)
{
    // Get the password.
    if (InputBox("Password", "", out Password)
        == DialogResult.Cancel)
    {
        Close();
        return;
    }

    // If the notes file exists, decrypt it.
    if (File.Exists(NOTES_FILE))
    {
        try
        {
            rchNotes.Rtf =
                File.ReadAllBytes(NOTES_FILE).Decrypt(Password);
        }
        catch
        {
            MessageBox.Show("Invalid password");
            Close();
            return;
        }
    }

    // We're all logged in. If we close after this, save changes.
    SaveChanges = true;
}

The code uses the InputBox method to ask you for the password. (The InputBox method displays a simple dialog. The dialog and the method are straightforward so they’re not shown here.) If you press the dialog’s Cancel button, the code unloads the program’s form and the program ends.

If you enter a password, the program determines whether the data file exists. If the file exists, the code uses the File.ReadAllBytes method to get the data in the file. It then calls the Decrypt extension method to decrypt the file, using the password you provided. (See the post Use the .NET cryptography library to make extension methods that encrypt and decrypt strings in C# for information about the Decrypt extension method.) The code assigns the decrypted data to the RichTextBox‘s Rtf property to display the formatted text.

If you entered the wrong password, the Decrypt method returns garbage. The garbage won’t contain properly formatted RTF (Rich Text Format) data, so the RichTextBox throws an exception. The program catches it, displays an error message, and ends.

The following code shows how the program saves the data in the RichTextBox when it ends.

// Save the encrypted notes.
private void Form1_FormClosing(object sender,
    FormClosingEventArgs e)
{
    if (SaveChanges)
        File.WriteAllBytes(NOTES_FILE,
            rchNotes.Rtf.Encrypt(Password));
}

This code gets the RichTextBox‘s Rtf property, encrypts it using the password you provided, and writes the encrypted bytes into the data file.

You could use this program to store passwords for web sites and other applications. It doesn’t provide all of the features of the password tracker program (such as generating random passwords), but it is extremely simple.

You could also enhance this application so it worked more like Notepad or WordPad. For example, instead of opening the same file every time, you could add a File menu with Open, Save, Save As, and New commands. You could also provide tools to let you format text, adjust indentation, set tabs, change colors, and so forth.

Note that you can paste formatted text and even images into the RichTextBox so you can do a lot with the program, although you may need to initially edit the data in some other program such a WordPad or Word.


Download Example   Follow me on Twitter   RSS feed




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, cryptography, mathematics, tools and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *