|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe main goal of this application is to show how easy is to work with Why do we need Using the codeThere are two main classes that I use in my application; one is the
For encryption and decryption, I am using a hard-coded Key and Initial Vector but you can change them by calling the
Let's take a look at the constructor of the public Entry(string name, string user, SecureString password,
string site, string comment)
{
this.name = name;
this.user = user;
this.comment = comment;
this.site = site;
using (CryptoCore cryptor = new CryptoCore())
{
IntPtr ptr = Marshal.SecureStringToBSTR(password);
this.password = cryptor.EncryptString(Marshal.PtrToStringAuto(ptr));
Marshal.ZeroFreeBSTR(ptr);
}
}
So I am receiving a password of type //ads a new entry in the Storage entry colection
Program.CurrentStorage.Entries.Add(
new Storage.Entry(txtName.Text, txtUser.Text,
txtPass.SecureText, txtSite.Text, txtComment.Text));
//updates a password with an Storage entry
entry.UpdatePassword(txtPass.SecureText);
Next, I want to show you how the application can save or load a .sps file. An SPS file is a serialized //decrypt and deserialize an object from disk
public Storage(string filePath)
{
Entries = new List<Entry>();
using (CryptoCore cryptor = new CryptoCore())
{
byte[] storage = cryptor.DecryptBuffer(File.ReadAllBytes(filePath));
using (MemoryStream ms = new MemoryStream(storage))
{
BinaryFormatter bf = new BinaryFormatter();
Entries = ((Storage)bf.Deserialize(ms)).Entries;
}
}
}
//save to disk an ecrypted serialized object
public void Serialize(string filePath)
{
byte[] storage;
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
storage = ms.ToArray();
}
using (CryptoCore cryptor = new CryptoCore())
{
File.WriteAllBytes(filePath, cryptor.EncryptBuffer(storage));
}
}
When we load an SPS file, the binding between the class and the listview control is very easy. Notice that the password is not shown here, there is another form that will help you change the password or view the credentials. Every time we open a new SPS file, it will load into a global private void FillListView()
{
listView.Items.Clear();
for (int i = 0; i < Program.CurrentStorage.Entries.Count; i++)
{
ListViewItem itemName = new ListViewItem(
Program.CurrentStorage.Entries[i].Name);
itemName.Tag = i;
itemName.SubItems.Add(Program.CurrentStorage.Entries[i].User);
itemName.SubItems.Add(Program.CurrentStorage.Entries[i].Site);
itemName.SubItems.Add(Program.CurrentStorage.Entries[i].Comment);
listView.Items.Add(itemName);
}
}
Points of interestYou have to download the code in order to understand the whole project; you can find examples of the new C# 2.0 features like generics and anonym delegates. This project does not implement a true password manager, because it lacks features like a main password for opening SPS files. It encrypts all SPS files with the same Rijndael key and so on. I will update the application with new features in time, if anyone finds it interesting. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||