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:
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:
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:
- Right hand click on the Desktop -> New -> Text Document.
- Rename the document to anything, with the file extension you want to use (i.e. file.cpt)
- Right hand click on the file -> Open With -> Choose program.
- Browse for CryptoViewer and click OK.