Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / Windows Forms

Simple Password Manager Using System.Security

Rate me:
Please Sign up or sign in to vote.
4.81/5 (14 votes)
29 Sep 2006CPOL3 min read 107.5K   6.1K   84  
Password Manager is a System.Security usage example using SecureString and SymmetricAlgorithms.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Security;
using System.Runtime.InteropServices;

namespace Aleph.SSM
{
    [Serializable]
    public class Storage
    {
        [Serializable]
        public struct Entry
        {
            string name;
            public string Name
            {
                get { return this.name; }
                set { this.name = value; }
            }
            string user;
            public string User
            {
                get { return this.user; }
                set { this.user = value; }
            }
            string password;
            public string Password
            {
                get { return this.password; }
            }
            string site;
            public string Site
            {
                get { return this.site; }
                set { this.site = value; }
            }
            string comment;
            
            public string Comment
            {
                get { return this.comment; }
                set { this.comment = value; }
            }

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

            public void UpdatePassword(SecureString password)
            {
                using (CryptoCore cryptor = new CryptoCore())
                {
                    IntPtr ptr = Marshal.SecureStringToBSTR(password);
                    this.password = cryptor.EncryptString(Marshal.PtrToStringAuto(ptr));
                    Marshal.ZeroFreeBSTR(ptr);
                }
            }
        }

        public List<Entry> Entries;

        public Storage()
        {
            Entries = new List<Entry>();
        }

        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;
                }
            }
        }

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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO VeriTech.io
Romania Romania
Co-Founder at VeriTech.io. Passionate about software architecture, SOA, domain driven design, continuous integration, .NET and Javascript programming. I write on www.stefanprodan.com.

Comments and Discussions