/// <summary> /// Waits for a new packet from the remote client. /// </summary> /// <returns>The data</returns> /// <exception cref="IOException">If anything went wrong with the I/O</exception> public byte[] ReceivePacket() { try { // Receives the length of the packet int len = this.ReadByte() | (this.ReadByte() << 8); // Reserves the space to store the data byte[] back = new byte[len]; // Waits for the data for (int i = 0; i < len; i++) { back[i] = this.ReadByte(); } // Decrypts the data byte[] dec = SimpleSecurityUtils.DecryptAES(back, this.aesKey, this.aesIv); // Checks if the decryption failed if (dec == null) { throw new Exception("Data-decryption failed."); } this.log .Debug("Checking nonce") .Critical($"Received bytes=[{string.Join(",",dec)}}}"); // Checks if the dec has at least enough bytes for the nonce bytes if (dec.Length < this.nonceBytes.Length) { throw new IOException("No nonce provided."); } // Check if the bytes of the nonce match for (int i = 0; i < this.nonceBytes.Length; i++) { if (this.nonceBytes[i] != dec[i]) { throw new IOException("Nonce does not match."); } } this.log .Debug("Nonce is correct."); // Creates a copy of the packet without the nonce byte[] finPkt = new byte[dec.Length - this.nonceBytes.Length]; // Copies the bytes of the actual packet Array.Copy(dec, this.nonceBytes.Length, finPkt, 0, finPkt.Length); return(finPkt); } catch (Exception e) { // Converts any exception to an i/o-exception throw new IOException(e.Message); } }
/// <summary> /// Loads the given config-file, decryptes it using the passed key and loads it as a config object /// </summary> /// <param name="filePath">The path to the configuration file.</param> /// <param name="key">The key to decrypt those bytes</param> /// <returns>The fully loaded config</returns> /// <exception cref="ConfigInvalidKeyException">If the given key couldn't decrypt the config</exception> /// <exception cref="ConfigCurruptedException">If the config seems to be currupted</exception> /// <exception cref="Exception">All exception that can be passed by File.ReadAllBytes using the filepath.</exception> public static Config LoadConfig(string filePath, string key) { // Reads the content of the passed config file byte[] rawData = File.ReadAllBytes(filePath); // 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)); // Tries to decrypt the config byte[] decrypted = SimpleSecurityUtils.DecryptAES(rawData, aesKey, aesIv); // Checks if the decryption failed if (decrypted == null) { throw new ConfigInvalidKeyException(); } try { // The config that will be created Config cfg = new Config(); // Tries to parse the config from json JObject json = JObject.Parse(Encoding.UTF8.GetString(decrypted)); // Gets all values cfg.Port = (int)json["port"]; cfg.Host = (string)json["host"] ?? string.Empty; cfg.PrivateKey = SimpleSecurityUtils.LoadRSAFromJson((JObject)json["rsa"]); // Returns the fully loaded config return(cfg); } catch { throw new ConfigCurruptedException(Encoding.UTF8.GetString(decrypted)); } }