Ejemplo n.º 1
0
        private void MenuHelp_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Started MenuHelp_Click(...)");
            //Console.WriteLine(this.data.ToString());

            Cryptographer c    = new Cryptographer(PasswordStorage.TEMP_TEST_MASTERKEY);
            string        encr = "Test!?tseT";

            Console.WriteLine($"{encr} => {c.Encrypt(encr)} => {c.Decrypt(c.Encrypt(encr))}");

            string pp = "ppas|" + encr + "|ollse213";

            Console.WriteLine($"RemoveSubString(...) = {PasswordStorage.RemoveSubString(pp, encr)}");



            Console.WriteLine("Ended   MenuHelp_Click(...)");
        }
Ejemplo n.º 2
0
        public static PasswordStorage DeSerialize(string FilePathAndName)
        {
            bool   retry = false;
            string encrypted_data, decrypted_data;

            do
            {
                retry = false;
                try
                {
                    // Read
                    using (FileStream file = new FileStream(FilePathAndName, FileMode.Open))
                    {
                        using (StreamReader sr = new StreamReader(file))
                        {
                            encrypted_data = sr.ReadToEnd();
                        }
                    }

                    Cryptographer c = new Cryptographer(TEMP_TEST_MASTERKEY);

                    if (!encrypted_data.Contains(c.MasterKeyHashString))
                    {
                        throw new Exception("No CryptoClearance!"); // TODO: Change to good crypto-exception and explain to user that its the wrong password when this is thrown!
                    }
                    // Remove MasterKeyHash (and possible carriage returns, newlines, ...)
                    encrypted_data = RemoveSubString(encrypted_data, c.MasterKeyHashString).Trim(new char[] { '\r', '\n' });
                    decrypted_data = c.Decrypt(encrypted_data);

                    // Build tmp xml stuff
                    PasswordStorage tmp_storage = new PasswordStorage();
                    tmp_storage.ImportFromXmlString(decrypted_data);
                    return(tmp_storage);
                }
                catch (Exception)
                {
                    var response = MessageBox.Show("An error occured during reading the file", "Couldn't read file", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    retry = (response == DialogResult.Retry);
                }
            } while (retry);

            return(null);
        }
Ejemplo n.º 3
0
        public void Serialize(string FilePathAndName)
        {
            bool retry = false;

            byte[] xml_bytes;
            do
            {
                retry = false;
                try
                {
                    using (FileStream file = new FileStream(FilePathAndName, FileMode.Create))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            this.data.WriteXml(ms);
                            xml_bytes = ms.ToArray();
                        }

                        Cryptographer c = new Cryptographer(TEMP_TEST_MASTERKEY);
                        string        encrypted_data = c.Encrypt(xml_bytes);

                        using (StreamWriter sw = new StreamWriter(file))
                        {
                            var x = Encoding.UTF8.GetString(c.MasterKeyHash);
                            sw.WriteLine(x);
                            sw.WriteLine(encrypted_data);
                        }
                    }
                }
                catch (Exception)
                {
                    var response = MessageBox.Show("An error occured during writing the file", "Couldn't write file", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    retry = (response == DialogResult.Retry);
                }
            } while (retry);
        }