コード例 #1
0
ファイル: Encryption.cs プロジェクト: fedjaz/Labs
        public static void EncryptFile(string path, EncryptionOptions options, ILogger 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");
                }
            }
        }
コード例 #2
0
ファイル: Validator.cs プロジェクト: fedjaz/Labs
        public void Validate(object obj)
        {
            string     report  = "";
            ETLOptions options = obj as ETLOptions;

            #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 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
            Report = report;
        }
コード例 #3
0
ファイル: MyETL.cs プロジェクト: fedjaz/Labs
        private void Created(object sender, FileSystemEventArgs e)
        {
            Task task = Task.Factory.StartNew(() =>
            {
                lock (locker)
                {
                    logger.Log($"Processing file {e.Name}");
                    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);
                }
            });
        }
コード例 #4
0
ファイル: Encryption.cs プロジェクト: fedjaz/Labs
 public static void DecryptFile(string path, EncryptionOptions options, ILogger logger)
 {
     if (options.EnableEncryption)
     {
         try
         {
             string decrypted;
             byte[] key = options.Key;
             using (StreamReader sr = new StreamReader(path))
             {
                 decrypted = Decrypt(sr.ReadToEnd(), key);
             }
             using (StreamWriter sw = new StreamWriter(path))
             {
                 sw.Write(decrypted);
             }
             logger.Log("Decrypted file successfully");
         }
         catch
         {
             logger.Log("Failed to decrypt file");
         }
     }
 }
コード例 #5
0
        private void Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                string   pathToFile = e.FullPath;
                DateTime date       = File.GetLastWriteTime(pathToFile);
                string   name       = Path.GetFileNameWithoutExtension(pathToFile);
                string   extansion  = Path.GetExtension(pathToFile);

                EncryptionOptions encryptionOptions =
                    optionsManager.GetOptions <EncryptionOptions>() as EncryptionOptions;
                ArchivationOptions archivationOptions =
                    optionsManager.GetOptions <ArchivationOptions>() as ArchivationOptions;



                if (extansion != ".gz" && extansion != "")
                {
                    byte[] key, iv;
                    (key, iv) = Encryption.GenKeyIv();

                    if (encryptionOptions.NeedToEncrypt)
                    {
                        File.WriteAllBytes(pathToFile, Encryption.Encrypt(pathToFile, key, iv));
                    }

                    string pathToArchive = Path.Combine(source, name + ".gz");
                    Archivation.Compress(pathToFile, pathToArchive, archivationOptions);


                    File.Delete(pathToFile);

                    if (!Directory.Exists(saveArchive))
                    {
                        Directory.CreateDirectory(saveArchive);
                    }

                    string newPathToArchive = Path.Combine(saveArchive, name + ".gz");
                    if (File.Exists(newPathToArchive))
                    {
                        File.Delete(newPathToArchive);
                    }
                    File.Move(pathToArchive, newPathToArchive);


                    string newPathToFile = Path.Combine(target, date.Year.ToString(),
                                                        date.Month.ToString(), date.Day.ToString());
                    Directory.CreateDirectory(newPathToFile);

                    newPathToFile = Path.Combine(newPathToFile, name + "_"
                                                 + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + extansion);
                    Archivation.Decompress(newPathToArchive, newPathToFile);

                    if (encryptionOptions.NeedToEncrypt)
                    {
                        File.WriteAllBytes(newPathToFile, Encryption.Decrypt(newPathToFile, key, iv));
                    }
                }
            }
            catch (IOException ex)
            {
                Thread.Sleep(1000);
                Created(sender, e);
            }
            catch (Exception ex)
            {
                logger.Log(ex.Message);
            }
        }