private void NewAcc(string name, string pin, string key)//create new acc { string res, textToEncrypt; string cryptoAccKey = null; string cryptoBinKey = null; DialogResult dialogResult = MessageBox.Show("Jméno: " + name + Environment.NewLine + "PIN: " + pin + Environment.NewLine + "Správně?", "Ověření", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { string accsPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\accs"; string binPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\bin"; if (!Directory.Exists(binPath)) // jestli existuje slozka bin { Directory.CreateDirectory(binPath); //vytvorit } if (!Directory.Exists(accsPath)) // jestli existuje slozka s uzivateli { Directory.CreateDirectory(accsPath); //vytvorit } if (!File.Exists(accsPath + "\\key.xml"))//vytvori klic pro spravu uctu { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); StreamWriter SaveKey = new StreamWriter(accsPath + "\\key.xml"); SaveKey.Write(cryptoAccKey = (RSA.ToXmlString(true))); SaveKey.Close(); } else//nebo precte pokud ma { try { RSACryptoServiceProvider RSAacc = new RSACryptoServiceProvider(); StreamReader OpenKey = new StreamReader(accsPath + "\\key.xml"); cryptoAccKey = OpenKey.ReadToEnd(); OpenKey.Close(); RSAacc.FromXmlString(cryptoAccKey); } catch//pokud precte a bude v key.xml chyba { MessageBox.Show("Chyba v souboru: " + accsPath + "\\key.xml"); Application.OpenForms[0].Show(); this.Hide(); } } if (!File.Exists(accsPath + "\\" + name + ".dll"))//zda neexistuje soubor s takovym uctem { res = ""; textToEncrypt = name + ";" + pin + ";" + key; res = Crypter.Encrypt(textToEncrypt, cryptoAccKey);//zasifruje StreamWriter strmAccWrite = new StreamWriter(accsPath + "\\" + name + ".dll"); strmAccWrite.Write(res); strmAccWrite.Close(); while (true) { if (!Directory.Exists(binPath + "\\" + key + "\\")) //zepta se jestli je slozka a soubor pro ulozeni dat { Directory.CreateDirectory(binPath + "\\" + key + "\\"); //vytvori se direktorie #region ziskani klice cryptoBinKey if (!File.Exists(binPath + "\\" + key + "\\" + "key.xml"))//vytvori se unikatni klic pro slozku s hesly { Directory.CreateDirectory(binPath + "\\" + key); RSACryptoServiceProvider RSAbin = new RSACryptoServiceProvider(); StreamWriter SaveKey = new StreamWriter(binPath + "\\" + key + "\\" + "key.xml"); SaveKey.Write(cryptoBinKey = (RSAbin.ToXmlString(true))); SaveKey.Close(); } else { try { RSACryptoServiceProvider RSAbin = new RSACryptoServiceProvider(); StreamReader OpenKey = new StreamReader(binPath + "\\" + key + "\\" + "key.xml"); cryptoBinKey = OpenKey.ReadToEnd(); OpenKey.Close(); RSAbin.FromXmlString(cryptoAccKey); } catch//pokud precte a bude v key.xml chyba { MessageBox.Show("Chyba v souboru: " + binPath + "\\" + key + "\\" + "key.xml"); Application.OpenForms[0].Show(); this.Hide(); } } #endregion Directory.CreateDirectory(binPath + "\\" + key + "\\" + "1");//vytvori se direktorie string[] data = new string[] { " ", " ", " ", " " }; for (int i = 0; i < 4; i++) { StreamWriter strmBinWrite = new StreamWriter(binPath + "\\" + key + "\\1\\" + (i + 1).ToString() + ".dll"); strmBinWrite.Write(Crypter.Encrypt(data[i], cryptoBinKey)); strmBinWrite.Close(); } StreamWriter strmAccWrite2 = new StreamWriter(accsPath + "\\" + name + ".dll"); res = ""; textToEncrypt = name + ";" + pin + ";" + key; res = Crypter.Encrypt(textToEncrypt, cryptoAccKey); strmAccWrite2.Write(res); strmAccWrite2.Close(); MessageBox.Show("Účet je vytvořen :)"); if (Application.OpenForms[0].Name == "Form1") { Application.OpenForms[0].Show(); } this.Hide(); break; } else { k = new generate(); key = k.gen(true, true, true, true, 25); } } } else { MessageBox.Show("Účet jíž existuje, vytvořte jiný"); } } else if (dialogResult == DialogResult.No) { txtPIN.Text = ""; txtUsername.Text = ""; txtVerifyPIN.Text = ""; txtPIN.ForeColor = Color.Red; txtVerifyPIN.ForeColor = Color.Red; txtUsername.ForeColor = Color.Red; } }
private void DataGridView_Upload() { if (txtWebPageName.ForeColor == Color.Gray && txtUsername.ForeColor == Color.Gray && txtPass.ForeColor == Color.Gray && txtComments.ForeColor == Color.Gray) { MessageBox.Show("Zadej všechny údaje."); } else { if (txtWebPageName.ForeColor != Color.Gray || txtUsername.ForeColor != Color.Gray || txtPass.ForeColor != Color.Gray || txtComments.ForeColor != Color.Gray) { dtGridView.Rows.Add(txtWebPageName.Text, txtUsername.Text, txtPass.Text, txtComments.Text); //////////////////////////////////////////////////////////// ulozeni z datagrid do souboru string binPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\bin"; for (int i = 0; i < dtGridView.RowCount; i++) { for (int j = 0; j < 4; j++) { switch (j) { case 0: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis1 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis1.Write(Crypter.Encrypt(dtGridView.Rows[i].Cells[0].Value.ToString(), cryptoBinKey)); strmZapis1.Close(); break; case 1: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis2 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis2.Write(Crypter.Encrypt(dtGridView.Rows[i].Cells[1].Value.ToString(), cryptoBinKey)); strmZapis2.Close(); break; case 2: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis3 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis3.Write(Crypter.Encrypt(dtGridView.Rows[i].Cells[2].Value.ToString(), cryptoBinKey)); strmZapis3.Close(); break; case 3: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis4 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis4.Write(Crypter.Encrypt(dtGridView.Rows[i].Cells[3].Value.ToString(), cryptoBinKey)); strmZapis4.Close(); break; } } } DisplayMode = "view"; AfterButtonClick(); } else { MessageBox.Show("Napíš všechny údaje"); } } }
private void BtnDelete_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dtGridView.SelectedRows) { DialogResult msgbx = MessageBox.Show("Chcete odstránít řádek?", "", MessageBoxButtons.YesNo); if (msgbx == DialogResult.Yes) { string binPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\bin"; DirectoryInfo di = new DirectoryInfo(binPath + "\\" + key); //vezme data z direktorie DirectoryInfo[] folders = di.GetDirectories(); for (int i = 0; i < folders.Count(); i++) { if (i == row.Index) { DirectoryInfo dd = new DirectoryInfo(binPath + "\\" + key + "\\" + folders[i]); foreach (FileInfo file in dd.GetFiles()) { file.Delete(); } folders[i].Delete(true); if (folders.Count() <= 1) { for (int j = 0; j < 4; j++) { switch (j) { case 0: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis1 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis1.Write(Crypter.Encrypt((" "), cryptoBinKey)); strmZapis1.Close(); break; case 1: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis2 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis2.Write(Crypter.Encrypt((" "), cryptoBinKey)); strmZapis2.Close(); break; case 2: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis3 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis3.Write(Crypter.Encrypt((" "), cryptoBinKey)); strmZapis3.Close(); break; case 3: if (!Directory.Exists(binPath + "\\" + key + "\\" + (i + 1).ToString())) { Directory.CreateDirectory(binPath + "\\" + key + "\\" + (i + 1).ToString()); //jestli neexistuje slozka } StreamWriter strmZapis4 = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (j + 1).ToString() + ".dll"); strmZapis4.Write(Crypter.Encrypt((" "), cryptoBinKey)); strmZapis4.Close(); break; } } } } } dtGridView.Refresh(); DataGridView_Refresh(); TxtName_SetText(); TxtPass_SetText(); TxtUsername_SetText(); TxtComments_SetText(); } } }
private void DataGridView_Edit() { string binPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\bin"; if (dtGridView.SelectedRows == null) { MessageBox.Show("Nelze nic upravit"); } else { foreach (DataGridViewRow row in dtGridView.SelectedRows) { DirectoryInfo di = new DirectoryInfo(binPath + "\\" + key); //vezme data z direktorie DirectoryInfo[] folders = di.GetDirectories(); if (txtWebPageName.Text == "") { txtWebPageName.Text = " "; } for (int i = 0; i <= dtGridView.RowCount; i++) { if (row.Index == i) { StreamWriter strmUprava = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (1).ToString() + ".dll"); strmUprava.Write(Crypter.Encrypt(txtWebPageName.Text, cryptoBinKey)); strmUprava.Close(); } } if (txtUsername.Text == "") { txtUsername.Text = " "; } for (int i = 0; i <= dtGridView.RowCount; i++) { if (row.Index == i) { StreamWriter strmUprava = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (2).ToString() + ".dll"); strmUprava.Write(Crypter.Encrypt(txtUsername.Text, cryptoBinKey)); strmUprava.Close(); } } if (txtPass.Text == "") { txtPass.Text = " "; } for (int i = 0; i <= dtGridView.RowCount; i++) { if (row.Index == i) { StreamWriter strmUprava = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (3).ToString() + ".dll"); strmUprava.Write(Crypter.Encrypt(txtPass.Text, cryptoBinKey)); strmUprava.Close(); } } if (txtComments.Text == "") { txtComments.Text = " "; } for (int i = 0; i <= dtGridView.RowCount; i++) { if (row.Index == i) { StreamWriter strmUprava = new StreamWriter(binPath + "\\" + key + "\\" + (i + 1).ToString() + "\\" + (4).ToString() + ".dll"); strmUprava.Write(Crypter.Encrypt(txtComments.Text, cryptoBinKey)); strmUprava.Close(); } } } DisplayMode = "view"; AfterButtonClick(); } }