private void savePropertiesBtn_Click(object sender, EventArgs e) { var path = System.IO.Directory.GetCurrentDirectory() + "\\config.ini"; try { // Create the file, or overwrite if the file exists. using (FileStream fs = File.Create(path)) { using (StreamWriter fw = new StreamWriter(fs)) { fw.WriteLine(StringToHash.Encrypt(connectionStringTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(destinationPathTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(dbNameTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(destinationEmailTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(senderTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(smtpTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(smtpLoginTxtBox.Text, "a")); fw.WriteLine(StringToHash.Encrypt(smtpPasswordTxtBox.Text, "a")); } } MessageBox.Show("Plik konfiguracyjny zapisany"); this.Close(); // File.Encrypt(path); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }
/// <summary> /// Encrypt a string. /// </summary> /// <param name="plainText">String to be encrypted</param> /// <param name="password">Password</param> public static string Encrypt(string plainText, string password) { if (plainText == null) { return(null); } if (password == null) { password = String.Empty; } // Get the bytes of the string var bytesToBeEncrypted = Encoding.UTF8.GetBytes(plainText); var passwordBytes = Encoding.UTF8.GetBytes(password); // Hash the password with SHA256 passwordBytes = SHA256.Create().ComputeHash(passwordBytes); var bytesEncrypted = StringToHash.Encrypt(bytesToBeEncrypted, passwordBytes); return(Convert.ToBase64String(bytesEncrypted)); }