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