示例#1
0
 private void ok_Click(object sender, EventArgs e)
 {
     if (master.Text == "")
     {
         MessageBox.Show("Enter a valid password");
         return;
     }
     if (cnt.count == 0)
     {
         char[] plain_pk;
         cnt.count++;
         Stream          streamc1     = new FileStream("count", FileMode.Create);
         BinaryFormatter bformatterc1 = new BinaryFormatter();
         bformatterc1.Serialize(streamc1, cnt);
         streamc1.Close();
         string ms   = master.Text;
         md5    ob   = new md5();
         Byte[] hash = ob.md(ms);
         Stream fi   = new FileStream("geeky", FileMode.Create);
         fi.Write(hash, 0, hash.Length);
         fi.Close();
         cr.AssignNewKey();
         //MessageBox.Show("wait!");
         Stream sm  = new FileStream("privatekey.xml", FileMode.Open);
         byte[] pvt = new byte[(int)sm.Length + 1];
         sm.Read(pvt, 0, (int)sm.Length);
         sm.Close();
         plain_pk = new char[pvt.Length + 1];
         //convert byte array-pvt to char array and encrypt
         i = 0;
         while (i < pvt.Length)
         {
             plain_pk[i] = Convert.ToChar(pvt[i]);
             i++;
         }
         encrypt_double ed        = new encrypt_double();
         char[]         cipher_pk = ed.encrypt(plain_pk, ms.ToCharArray());
         //convert cipher_pk to byte array and write it
         byte[] towrite = new byte[cipher_pk.Length + 1];
         i = 0;
         while (i < cipher_pk.Length)
         {
             towrite[i] = Convert.ToByte(cipher_pk[i]);
             i++;
         }
         Stream sm1 = new FileStream("privatekey.xml", FileMode.Create);
         sm1.Write(towrite, 0, towrite.Length);
         sm1.Close();
     }
     else
     {
         string ms      = master.Text;
         md5    ob      = new md5();
         Byte[] newhash = ob.md(ms);
         Byte[] oldhash = new Byte[newhash.Length + 1];
         Stream fi      = new FileStream("geeky", FileMode.Open);
         fi.Read(oldhash, 0, newhash.Length);
         fi.Close();
         int i = 0;
         t = 0;
         while (i < oldhash.Length && i < newhash.Length)
         {
             if (oldhash[i] != newhash[i])
             {
                 t = 1;
                 MessageBox.Show("Wrong Password!");
                 break;
             }
             i++;
         }
         if (t == 0)
         {
             MessageBox.Show("Welcome");
         }
     }
     if (t == 0)
     {
         ok.Visible        = false;
         master.Visible    = false;
         enter.Visible     = false;
         textBox1.Enabled  = true;
         insert.Enabled    = true;
         serialize.Enabled = true;
         password.Enabled  = true;
         id.Visible        = true;
         pwd.Visible       = true;
         label2.Visible    = true;
         label3.Visible    = true;
         label4.Visible    = true;
         store.Enabled     = true;
         retrieve.Enabled  = true;
         master_password   = master.Text;
     }
 }