Esempio n. 1
0
        private void retrieve_Click(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists("success"))
                {
                    MessageBox.Show("No passwords to retrieve!");
                    return;
                }
                //decrypt the privatekey.xml
                Stream sm    = new FileStream("privatekey.xml", FileMode.Open);
                byte[] e_pvt = new byte[(int)sm.Length + 1];
                sm.Read(e_pvt, 0, (int)sm.Length);
                sm.Close();
                char[] cp_pk = new char[e_pvt.Length + 1];
                //convert byte array-e_pvt to char array and decrypt
                i = 0;
                while (i < e_pvt.Length)
                {
                    cp_pk[i] = Convert.ToChar(e_pvt[i]);
                    i++;
                }
                encrypt_double ed    = new encrypt_double();
                char[]         pt_pk = ed.decrypt(cp_pk, master_password.ToCharArray());
                //convert pt_pk to byte array and write it
                byte[] towrite = new byte[pt_pk.Length + 1];
                i = 0;
                while (i < pt_pk.Length)
                {
                    towrite[i] = Convert.ToByte(pt_pk[i]);
                    i++;
                }
                Stream sm1 = new FileStream("privatekey.xml", FileMode.Create);
                sm1.Write(towrite, 0, towrite.Length);
                sm1.Close();



                label5.Visible = true;
                label4.Visible = false;
                //code to deserialize the cipher class1
                Stream          stream     = File.Open("string", FileMode.Open);
                BinaryFormatter bformatter = new BinaryFormatter();
                ob = (cipher)bformatter.Deserialize(stream);
                stream.Close();
                //MessageBox.Show(cr.DecryptData(ob.getstring()));
                string   plain = cr.DecryptData(ob.getstring());
                string[] del   = { "%DELIMITER&" };
                string[] user  = plain.Split(del, StringSplitOptions.RemoveEmptyEntries);
                id.Text  = user[0];
                pwd.Text = user[1];


                //again encrypt privatekey.xml
                Stream sma = new FileStream("privatekey.xml", FileMode.Open);
                byte[] pvt = new byte[(int)sma.Length + 1];
                sma.Read(pvt, 0, (int)sma.Length);
                sma.Close();
                char[] plain_pka = new char[pvt.Length + 1];
                //convert byte array-pvt to char array and encrypt
                i = 0;
                while (i < pvt.Length)
                {
                    plain_pka[i] = Convert.ToChar(pvt[i]);
                    i++;
                }
                encrypt_double eda       = new encrypt_double();
                char[]         cipher_pk = ed.encrypt(plain_pka, master_password.ToCharArray());
                //convert cipher_pk to byte array and write it
                byte[] towritea = new byte[cipher_pk.Length + 1];
                i = 0;
                while (i < cipher_pk.Length)
                {
                    towritea[i] = Convert.ToByte(cipher_pk[i]);
                    i++;
                }
                Stream sm1a = new FileStream("privatekey.xml", FileMode.Create);
                sm1a.Write(towritea, 0, towritea.Length);
                sm1a.Close();
            }
            catch (Exception eb)
            {
                MessageBox.Show("An error occured while retrieving the password");
            }
        }
Esempio n. 2
0
        private void store_Click(object sender, EventArgs e)
        {
            try
            {
                string s1, s2;
                label4.Visible = true;
                s1             = id.Text;
                s2             = pwd.Text;
                if (s1 == "" || s2 == "")
                {
                    MessageBox.Show("Incorrect format. Empty values are not allowed!");
                    return;
                }
                string final = s1 + "%DELIMITER&" + s2;
                //decrypt the privatekey.xml
                Stream sm    = new FileStream("privatekey.xml", FileMode.Open);
                byte[] e_pvt = new byte[(int)sm.Length + 1];
                sm.Read(e_pvt, 0, (int)sm.Length);
                sm.Close();
                char[] cp_pk = new char[e_pvt.Length + 1];
                //convert byte array-e_pvt to char array and decrypt
                i = 0;
                while (i < e_pvt.Length)
                {
                    cp_pk[i] = Convert.ToChar(e_pvt[i]);
                    i++;
                }
                encrypt_double ed    = new encrypt_double();
                char[]         pt_pk = ed.decrypt(cp_pk, master_password.ToCharArray());
                //convert pt_pk to byte array and write it
                byte[] towrite = new byte[pt_pk.Length + 1];
                i = 0;
                while (i < pt_pk.Length)
                {
                    towrite[i] = Convert.ToByte(pt_pk[i]);
                    i++;
                }
                Stream sm1 = new FileStream("privatekey.xml", FileMode.Create);
                sm1.Write(towrite, 0, towrite.Length);
                sm1.Close();



                //code to encrypt and serialize the objects
                cr = new Crypography();
                string cipher = cr.crypt(final);
                if (cipher.CompareTo("Illegal Values") != 0)
                {
                    //code to deserialize the cipher class1
                    Stream          stream     = File.Open("string", FileMode.Open);
                    BinaryFormatter bformatter = new BinaryFormatter();
                    ob = (cipher)bformatter.Deserialize(stream);
                    stream.Close();
                    ob.assign(cipher);
                    //serialize the cipher object
                    Stream          streamc     = new FileStream("string", FileMode.Create);
                    BinaryFormatter bformatterc = new BinaryFormatter();
                    bformatterc.Serialize(streamc, ob);
                    streamc.Close();
                    id.Text  = "";
                    pwd.Text = "";
                }
                else
                {
                    MessageBox.Show("Illegal Values. Click 'Retrieve Passwords' to retrieve your old values.");
                }
                //again encrypt privatekey.xml
                Stream sma = new FileStream("privatekey.xml", FileMode.Open);
                byte[] pvt = new byte[(int)sma.Length + 1];
                sma.Read(pvt, 0, (int)sma.Length);
                sma.Close();
                char[] plain_pka = new char[pvt.Length + 1];
                //convert byte array-pvt to char array and encrypt
                i = 0;
                while (i < pvt.Length)
                {
                    plain_pka[i] = Convert.ToChar(pvt[i]);
                    i++;
                }
                encrypt_double eda       = new encrypt_double();
                char[]         cipher_pk = ed.encrypt(plain_pka, master_password.ToCharArray());
                //convert cipher_pk to byte array and write it
                byte[] towritea = new byte[cipher_pk.Length + 1];
                i = 0;
                while (i < cipher_pk.Length)
                {
                    towritea[i] = Convert.ToByte(cipher_pk[i]);
                    i++;
                }
                Stream sm1a = new FileStream("privatekey.xml", FileMode.Create);
                sm1a.Write(towritea, 0, towritea.Length);
                sm1a.Close();
                Stream sss = new FileStream("success", FileMode.Create);
                sss.Close();
            }
            catch (Exception ed)
            {
                MessageBox.Show("An error occured while storing the passwords");
            }
        }
Esempio n. 3
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;
     }
 }