Password file manager - .NET encription!

13479 Users read it.
by Dan Glass (May 18, 2003)

I have too many passwords to remember. Not to mention all the creditcard numbers, bank accounts, urls, vpn settings... the list goes on.

I've had the need for a long time for an encrypted file then I could double click on and simply be prompted for a password to look at the file, make changes, copy a word or two, and close it. The closest I got was PGP, which I had to decrypt the file to disk, look at it, wipe it, or edit it, re-encrypt it, then wipe it. I JUST WANTED ONE WORD!!!

User Rating (15 votes)
0 reviews available
Author(s):Dan Glass
User Level:Intermediate
Language:C#
Platform:Windows
Product(s):VS.NET
Code Download:External link

Sample Image - CryptoViewer.jpg

Introduction

I have too many passwords to remember. Not to mention all the creditcard numbers, bank accounts, urls, vpn settings... the list goes on.

I've had the need for a long time for an encrypted file then I could double click on and simply be prompted for a password to look at the file, make changes, copy a word or two, and close it. The closest I got was PGP, which I had to decrypt the file to disk, look at it, wipe it, or edit it, re-encrypt it, then wipe it. I JUST WANTED ONE WORD!!!

Points of Interest

So I dragged a RichTextBox to my new project, added some menu items, and whalla! a simple UI to start. Two methods were needed, Encrypt() and Decrypt(), the rest was just wireup code - nothing exciting.

I used DES which uses a key and an IV. Both are 8 bytes long. To make the key and IV I use the password in a simple algorithum:

private String Password
{
    set
    {
        IV = value;
        while ( IV.Length < 8 ) IV += value;
        IV = IV.substring(0,8);

        ArrayList a = new ArrayList();

        foreach ( char c in value.ToCharArray() )
        {
            a.Add(c);
        }

        a.reverse();
        String output = new String((char[])a.ToArray(typeof(char)));

        KEY = output;
        while ( KEY.Length < 8 ) KEY += output;
        KEY = KEY.substring(0,8);
    }
}


Admitantly this is not the best way, and I will update it when I get a chance. Most the code is wireup and file reads/writes, but the Encrypt() and Decrypt() methods are shown below:

public static byte[] Encrypt(byte[] data, byte[] key, byte[] IV)
{
    MemoryStream writer = new MemoryStream();
    writer.SetLength(0);

    DESCryptoServiceProvider crypto = new DESCryptoServiceProvider();
    crypto.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

    CryptoStream encStream = new CryptoStream(writer, crypto.CreateEncryptor(key, IV), CryptoStreamMode.write);

    encStream.write(data,0, data.Length);
    encStream.FlushFinalBlock();

    byte[] b = new byte[writer.Length];
    writer.Position = 0;
    writer.Read( b, 0, (int)writer.Length );

    encStream.Close();
    writer.Close();

    return b;
}

public static Stream Decrypt(byte[] data, byte[] key, byte[] IV)
{
    MemoryStream writer = new MemoryStream();
    writer.SetLength(0);

    DESCryptoServiceProvider crypto = new DESCryptoServiceProvider();
    crypto.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

    CryptoStream encStream = new CryptoStream(writer, crypto.CreateDecryptor(key, IV), CryptoStreamMode.write);

    encStream.write(data,0, data.Length);
encStream.FlushFinalBlock();

writer.Position = 0;
return writer;
}


Notice the use of the Padding mode. You may run into alot of problems without it, incuding data loss. Also the keys need to be exactly the right size - 8 bytes long. I passed back the stream to make my code simpler, and I was planning to do it with the Encrypt() algorithum later.

Using the code

The demo is really all you need. Create a file association to your liking, so that you can double click on it to be prompted for a password to open it.

Here are the steps:

  1. Right hand click on the desktop -> New -> Text Document
  2. Rename the doc to anything with the file extension you want to use (i.e file.cpt)
  3. Right hand click on the file -> Open With -> Choose program
  4. Browse for the CryptoViewer and click Ok.

Dan Glass
www.olero.com
ORM.NET - Data access made easy

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency