Saturday, November 6, 2010

Ultimate Password Protection [C# Tutorial]

Here is the code

using Microsoft.Win32;
//////////////////////////////////////////////////

// Form1
//////////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP",true);
int fails = 0; bool passEntered = false;
private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text != PasswordEncrypter.Decrypt(key.GetValue("Pass").ToString()))
    { 
        ++fails; MessageBox.Show("Wrong Password!!! Fail: " + fails.ToString());
        if (fails > 4)
        {
            MessageBox.Show("This application is locked for 15 minutes!");
            key.SetValue("T", PasswordEncrypter.Encrypt(DateTime.Now.AddMinutes(15).ToString())); 
            Application.Exit();
        }
    }
    else { passEntered = true; this.Close(); }
}

private void button2_Click(object sender, EventArgs e)
{
    Form3 frm3 = new Form3();
    frm3.ShowDialog(); 
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!passEntered)
        Application.Exit(); 
}

// Form2
//////////////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP");
//////// Inside Form2() before InitializeComponent(); //////////////
if (key == null)
{
      Form3 frm3 = new Form3();
      frm3.ShowDialog();
}
else
{
      Form1 frm1 = new Form1(); 
      frm1.ShowDialog();
}

// Form3
/////////////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP",true);   
public Form3()
{
    InitializeComponent();
    if (key == null)
    {
        label1.Enabled = false;
        textBox1.Enabled = false;
        button1.Enabled = false;
    }
    else
        button2.Enabled = false;
}
bool passChanged = false;
private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text != key.GetValue("Pass").ToString())
    {
        MessageBox.Show("Old Password is incorrect!");
    }
    else
    {
        if (textBox2.Text == textBox3.Text && textBox2.Text != "" && textBox3.Text != "")
        { key.SetValue("Pass", PasswordEncrypter.Encrypt(textBox3.Text)); passChanged = true; this.Close(); }
        else MessageBox.Show("Password did not match!");  
    }
}

private void button2_Click(object sender, EventArgs e)
{
    if (textBox2.Text == textBox3.Text && textBox2.Text != "" && textBox3.Text != "")
    {
        key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\UPP");
        key.SetValue("Pass", PasswordEncrypter.Encrypt(textBox3.Text)); passChanged = true; this.Close(); 
    }
    else MessageBox.Show("Password did not match!");
}

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!passChanged)
        Application.Exit(); 
}

// Form4
/////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP",true);   
public Form4()
{
    InitializeComponent();
    if (DateTime.Now >= DateTime.Parse(PasswordEncrypter.Decrypt(key.GetValue("T").ToString())))
    {
        label1.Text = "Application Unlocked! Restart it!";
        key.DeleteValue("T"); 
    }
}
int c = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    Opacity -= 0.01; c++;
    if (c >= 90) Application.Exit(); 
}

// Program.cs
////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP");

if (key.GetValue("T") == null)
    Application.Run(new Form2());
else Application.Run(new Form4());

// PasswordEncrypter
///////////////////////////////////////////
public static string Encrypt(string password)
{
    char[] pass = password.ToCharArray(); string[] symb = { "@", "#", "$", "%", "&" }; Random rnd = new Random();
    string newpass = "";
    for (int i = pass.Length - 1; i >= 0; i--)
    {
        newpass += ((int)pass[i]).ToString() + symb[rnd.Next(0, 5)];  
    }
    return newpass;
}

public static string Decrypt(string password)
{
    string[] tokens = password.Split('@', '#', '$', '%', '&'); string pass = "";
    for (int i = tokens.Length - 2; i >= 0; i--)
    {
        pass += ((char)Convert.ToInt32(tokens[i])).ToString();  
    }
    return pass;
}

Or download the solution

No comments:

Post a Comment