示例#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");
            }
        }