/// <summary> /// Saves the config file encrypted using the passed key /// </summary> /// <param name="filePath">The path to the file where to save the configuration</param> /// <param name="key">The key for encrypting the file</param> /// <exception cref="ConfigException">Something went wrong while encryption (Unknown)</exception> /// <exception cref="Exception">All exception that can be passed on by File.WriteAllBytes called with the filePath</exception> public void SaveConfig(string filePath, string key) { // Generates the object that will be saved as the config file JObject json = new JObject() { ["port"] = this.Port, ["host"] = this.Host, ["rsa"] = SimpleSecurityUtils.SaveRSAToJson(this.PrivateKey) }; // Generates the actual key and iv from the previous plain text key byte[] aesKey = SimpleSecurityUtils.HashSHA256(Encoding.UTF8.GetBytes(key)); byte[] aesIv = SimpleSecurityUtils.HashMD5(Encoding.UTF8.GetBytes(key)); // Encryptes the config-json object byte[] encData = SimpleSecurityUtils.EncryptAES(Encoding.UTF8.GetBytes(json.ToString()), aesKey, aesIv); // Checks if the encryption failed if (encData == null) { // Unknown exception throw new ConfigException(); } // Writes all data to the file File.WriteAllBytes(filePath, encData); }
public ConfigWindow(Config cfg, string password) { InitializeComponent(); this.cfg = cfg; this.password = password; // Sets the values this.FieldPort.Text = cfg.Port.ToString(); this.FieldHost.Text = cfg.Host; this.FieldRSA.Text = SimpleSecurityUtils.SaveRSAToJson(cfg.PrivateKey).ToString(Formatting.None); // Disables the save button this.buttonSave.IsEnabled = false; }