Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / Windows Forms

Simple Password Manager with Google API validation

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
15 Apr 2013CPOL2 min read 18.6K   323   10  
This is an alternative for "Simple Password Manager Using System.Security"
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
Software Developer
India India
Vasudevan Deepak Kumar is from Chennai, India who has been in the programming career since 1994, when he was 15 years old. He has his Bachelors of Engineering (in Computer Science and Engineering) from Vellore Engineering College. He also has a MBA in Systems from Alagappa University, Karaikudi, India.
He started his programming career with GWBasic and then in his college was involved in developing programs in Fortran, Cobol, C++. He has been developing in Microsoft technologies like ASP, SQLServer 2000.
His current focus is ASP.NET, C#, VB.NET, PHP, SQL Server and MySQL. In his past-time, he listens to polite Carnatic Music. But the big question is that with his current Todolist backlog, does he get some past time?

Comments and Discussions