Click here to Skip to main content
15,891,951 members
Articles / Mobile Apps

Windows Mobile Password Safe

Rate me:
Please Sign up or sign in to vote.
4.87/5 (58 votes)
12 Jan 2009CPOL16 min read 160.7K   3.1K   139  
A password safe with a touch screen UI introducing Fluid Controls.
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PasswordSafe.Xml;
using System.Diagnostics;
using PasswordSafe.Classes;
using PasswordSafe.Themes;
using Fluid.Controls;
using Fluid.Classes;
using PasswordSafe.Encryption;
using PasswordSafe.Properties;

namespace PasswordSafe
{
    public partial class PasswordForm : Form
    {
        public PasswordForm()
        {
            Instance = this;
            Theme.Current = new DefaultTheme();
            InitializeComponent();
            Host.Bounds = Host.ClientBounds;
            Host.BackColor = Color.Empty;


            if (!DataReader.DataBaseExits())
            {
                WelcomeScreen welcome = new WelcomeScreen();
                welcome.Continue += new EventHandler(welcome_Continue);
                welcome.ShowMaximized(ShowTransition.None);
            }
            else
            {
                OpenLogin();
            }
        }


        void welcome_Continue(object sender, EventArgs e)
        {
            OpenLogin();
        }

        private void OpenLogin()
        {
            LoginPanel login = new LoginPanel(0, 0, 240, 400);
            this.Login = login;
            login.ConfirmPassword += new EventHandler<PasswordEventArgs>(login_PasswordVerify);
            login.ChangePassword += new EventHandler<HandledEventArgs>(login_ChangePassword);
            login.Enter += new EventHandler(login_Enter);
            login.EnsureEnterEnabled();
            login.ShowMaximized();
        }

        void login_ChangePassword(object sender, HandledEventArgs e)
        {
            bool match = false;
            string xml = Encryptor.Decrypt(EncryptedData, Login.Password);
            try
            {
                byte[] encrypted = Encryptor.Encrypt(xml, Login.NewPassword);
                string decrypted = Encryptor.Decrypt(encrypted, Login.NewPassword);
                match = decrypted == xml;
                if (match)
                {
                    DataReader.WriteData(encrypted);
                    EncryptedData = encrypted;
                }
            }
            catch
            {
            }
            e.handledEventArgs = match;
        }

        public byte[] EncryptedData
        {
            get
            {
                if (Encryptor.EncryptedData == null)
                {
                    Encryptor.EncryptedData = DataReader.ReadData();
                }
                return Encryptor.EncryptedData;
            }
            set { Encryptor.EncryptedData = value; }
        }

        private string xml = string.Empty;

        public LoginPanel Login;

        void login_Enter(object sender, EventArgs e)
        {
            Enter();
        }

        private void Enter()
        {
            Encryptor.Password = Login.Password;
            using (PasswordReader reader = new PasswordReader(xml))
            {
                ListBuilder.Instance = new ListBuilder(reader);
            }

            Browser = new Browser();
            Browser.Bounds = this.ClientRectangle;

            Browser.ShowMaximized(ShowTransition.FromBottom);
            Login.Text = "";
            Login.Visible = false;
        }

        void login_PasswordVerify(object sender, PasswordEventArgs e)
        {
            try
            {
                xml = Encryptor.Decrypt(EncryptedData, e.Password);
                e.handledEventArgs = xml.StartsWith("<?xml");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex);
                e.handledEventArgs = false;
            }
        }

        public Browser Browser;
        public MessageDialog OKCancel;

        public static PasswordForm Instance;


        /// <summary>
        /// Only close, when the passwords are saved:
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            if (Browser.Instance!=null && Browser.Instance.SavePasswords() == false) e.Cancel = true;
            base.OnClosing(e);
        }
    }
}

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 (Senior)
Germany Germany
MCPD
Enterprise Application Developer 3.5
Windows Developer 3.5
.ASP.NET Developer 3.5
.NET 2.0 Windows Developer
.NET 2.0 Web Developer
.NET 2.0 Enterprise Application Developer


MCTS
.NET 3.5 Windows Forms Applications
.NET 3.5 ASP.NET Applications
.NET 3.5, ADO.NET Application Development
.NET 3.5 WCF
.NET 3.5 WPF
.NET 3.5 WF
Microsoft SQL Server 2008, Database Development
.NET 2.0 Windows Applications
.NET 2.0 Web Applications
.NET 2.0 Distributed Applications
SQL Server 2005
Sharepoint Services 3.0 Application Development
Windows Vista Client Configuration

Comments and Discussions