public static void EncryptFile(string path, EncryptionOptions options, Logger logger) { if (options.EnableEncryption) { string encrypted; byte[] key; if (options.RandomKey) { key = GenerateKey(16); options.Key = key; } else { key = options.Key; } try { using (StreamReader sr = new StreamReader(path)) { encrypted = Encrypt(sr.ReadToEnd(), key); } using (StreamWriter sw = new StreamWriter(path)) { sw.Write(encrypted); } logger.Log("Encrypted file successfully"); } catch { logger.Log("Failed to encrypt file"); } } }
public ETLOptions(SendingOptions sendingOptions, EncryptionOptions encryptionOptions, LoggingOptions loggingOptions, ArchiveOptions archiveOptions) { SendingOptions = sendingOptions; EncryptionOptions = encryptionOptions; LoggingOptions = loggingOptions; ArchiveOptions = archiveOptions; }
public static string Validate(ETLOptions options) { string report = ""; #region Sending Validation SendingOptions sending = options.SendingOptions; if (!CreateDirectoryIfNotExist(sending.SourceDirectory)) { sending.SourceDirectory = "C:\\FileWatcher\\source"; CreateDirectoryIfNotExist(sending.SourceDirectory); report += "Cannot open source directory, using default. "; } if (!CreateDirectoryIfNotExist(sending.TargetDirectory)) { sending.TargetDirectory = "C:\\FileWatcher\\target"; report += "Cannot open target directory, using default. "; CreateDirectoryIfNotExist(sending.TargetDirectory); } if (!CreateDirectoryIfNotExist(sending.ArchiveDirectory)) { sending.ArchiveDirectory = "C:\\FileWatcher\\target\\archive"; report += "Cannot open source archive, using default. "; CreateDirectoryIfNotExist(sending.ArchiveDirectory); } #endregion #region Logging Validation LoggingOptions logging = options.LoggingOptions; if (!CreateFileIfNotExist(logging.LogPath)) { logging.LogPath = "C:\\FileWatcher\\target\\log.txt"; report += "Cannot open source log file, using default. "; CreateFileIfNotExist(logging.LogPath); } #endregion #region Encryption Validation EncryptionOptions encryption = options.EncryptionOptions; if (!encryption.RandomKey && encryption.Key.Length != 16) { report += "Encryption key's length must be 16, using random key. "; } #endregion #region Archive Validation ArchiveOptions archive = options.ArchiveOptions; if ((int)archive.CompressionLevel < 0 || (int)archive.CompressionLevel > 2) { archive.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal; report += "Compression level value can't be below zero and abowe 2, using default value. "; } #endregion return(report); }
private void Created(object sender, FileSystemEventArgs e) { WaitUntilFileIsReady(e.FullPath); FileInfo file = new FileInfo(e.FullPath); EncryptionOptions encryptionOptions = optionsManager.GetOptions <EncryptionOptions>() as EncryptionOptions; Encryption.EncryptFile(e.FullPath, encryptionOptions, logger); string newPath = SendFile(file, optionsManager.GetOptions <SendingOptions>() as SendingOptions, logger, optionsManager.GetOptions <ArchiveOptions>() as ArchiveOptions); Encryption.DecryptFile(newPath, encryptionOptions, logger); }
public static void DecryptFile(string path, EncryptionOptions options, Logger logger) { if (options.EnableEncryption) { try { string decrypted; byte[] key = options.Key; using (StreamReader sr = new StreamReader(path)) { decrypted = Decrypt <string>(sr.ReadToEnd(), key); } using (StreamWriter sw = new StreamWriter(path)) { sw.Write(decrypted); } logger.Log("Decrypted file successfully"); } catch { logger.Log("Failed to decrypt file"); } } }