Click here to Skip to main content
15,867,291 members
Articles / Programming Languages / C#
Article

Password file manager - simple double click to look at your password file

Rate me:
Please Sign up or sign in to vote.
3.08/5 (12 votes)
30 Mar 20032 min read 89K   3.1K   61   8
Password file manager - simple double click to look at your password file.

Sample Image - CryptoViewer.jpg

Introduction

I have too many passwords to remember. Not to mention all the credit card numbers, bank accounts, URLs, VPN settings... the list goes on.

I've had the need, for a long time, for an encrypted file that 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 wired up 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 algorithm:

C#
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);
    }
}

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

C#
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 PaddingMode. You may run into a lot of problems without it, including 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() algorithm 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 document 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 CryptoViewer and click OK.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect support.com
Australia Australia

Comments and Discussions

 
GeneralPassword Stopped Working Pin
Hollerith22-Jan-10 9:45
Hollerith22-Jan-10 9:45 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:05
Tony Selke27-Sep-07 7:05 
GeneralMust have NET software installed Pin
artstar23-Apr-06 11:49
artstar23-Apr-06 11:49 
GeneralRe: Must have NET software installed Pin
Jason Barry26-Aug-08 9:18
professionalJason Barry26-Aug-08 9:18 
Generalescape characters Pin
talljames110-Oct-04 16:45
talljames110-Oct-04 16:45 
GeneralDeja-vu Pin
Steven Campbell13-Apr-04 7:24
Steven Campbell13-Apr-04 7:24 
Good job. I wrote almost this exact same application. A rich text box and a password form + some simple encryption. I was thinking of putting my app on CodeProject, but I guess you beat me to it!

One "feature" you may want to add to your app: I decided that I did not need the overhead of file/save menus. When the application closes, it auto-saves the file. In fact, my version of the app has no menus at all! What can I say, it is a Zen-coding experiment Smile | :)
GeneralKey Length Question... Pin
mikasa27-Jun-03 9:12
mikasa27-Jun-03 9:12 
GeneralRe: Key Length Question... Pin
Dan Glass12-Jul-03 20:19
Dan Glass12-Jul-03 20:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.