public void EncryptFileTest()
        {
            // arrange
            FileEncryptionUtility fileEncryptionUtility = new FileEncryptionUtility();
            string stringToEncrypt   = "stringToEncrypt";
            string encryptedFilePath = Path.GetTempPath() + $"{DateTime.Now.Ticks.ToString()}";
            string encryptionKey     = Guid.NewGuid().ToString().Substring(0, 8);

            // act
            fileEncryptionUtility.EncryptFile(stringToEncrypt, encryptedFilePath, encryptionKey);

            // assert
            Assert.IsTrue(File.Exists(encryptedFilePath));
        }
        public void DecryptFileTest()
        {
            // arrange
            FileEncryptionUtility fileEncryptionUtility = new FileEncryptionUtility();
            string stringToEncrypt   = "stringToEncrypt";
            string encryptedFilePath = Path.GetTempPath() + $"{DateTime.Now.Ticks.ToString()}";
            string encryptionKey     = Guid.NewGuid().ToString().Substring(0, 8);

            // act
            fileEncryptionUtility.EncryptFile(stringToEncrypt, encryptedFilePath, encryptionKey);
            string decryptedText = fileEncryptionUtility.DecryptFile(encryptedFilePath, encryptionKey);

            // assert
            Assert.AreEqual(decryptedText, "stringToEncrypt");
        }
예제 #3
0
        /// <summary>
        /// Will save the settings derived from the commandline-arguments into a file. This facilitates automatic creation and utilization of transfer-settings.
        /// </summary>
        /// <param name="settingsParser"></param>
        /// <param name="settingsFilePath"></param>
        /// <param name="logger"></param>
        /// <param name="encryptionKeyFilePath">Optional. If specified, an encryption-key will be generated and stored to this file, and will be used to encrypt the settings file.</param>
        public static void SaveSettingsToFile(SettingsParser settingsParser, string settingsFilePath, ILogger logger, string encryptionKeyFilePath = null)
        {
            logger.LogInformation($"Will save settings-file to '{settingsFilePath}...'");

            // First, remove the settings-filepath argument from the settings. We don't want to include this in the saved settings.
            settingsParser.ApplicationSettings.SettingsFilePath    = null;
            settingsParser.ApplicationSettings.SettingsKeyFilePath = null;

            // Now go ahead and serialize the settings
            string serializedSettings = Newtonsoft.Json.JsonConvert.SerializeObject(settingsParser, Formatting.Indented);

            if (!string.IsNullOrWhiteSpace(encryptionKeyFilePath))
            {
                logger.LogInformation($"Will encrypt settings file {settingsFilePath}");
                string encryptionKey = null;

                bool encryptionFileExists = File.Exists(encryptionKeyFilePath);
                if (encryptionFileExists)
                {
                    encryptionKey = File.ReadAllText(encryptionKeyFilePath);
                }
                else
                {
                    // no encryption file exists, generate a new encryption key, save it, then encrypt the settings.
                    logger.LogInformation($"Generating new encryption key...");
                    encryptionKey = Guid.NewGuid().ToString().Substring(0, 8);
                    logger.LogInformation($"Saving encryption key to '{encryptionKeyFilePath}'...");
                    File.WriteAllText(encryptionKeyFilePath, encryptionKey);
                }

                FileEncryptionUtility fileEncryptionUtility = new FileEncryptionUtility();
                fileEncryptionUtility.EncryptFile(serializedSettings, settingsFilePath, encryptionKey);
            }
            else
            {
                // Save the settings into an unencrypted file.
                File.WriteAllText(settingsFilePath, serializedSettings);
            }

            logger.LogInformation($"Saved settings-file to '{settingsFilePath}'");
        }