Ejemplo n.º 1
0
        public static IConfiguration Decrypt(this IConfiguration config, ICryptoHelper crypto)
        {
            string key;
            string foundVal;
            string decryptedVal;
            List <ConfigSetting> configList = config.GetConfigSettings();
            string encPrefix = config["ConfigOptions:Cryptography:EncValPrefix"];

            for (int i = 0; i < configList.Count; i++)
            {
                foundVal = configList[i].SettingValue;
                if (foundVal.StartsWith(encPrefix) && foundVal != encPrefix)
                {
                    key          = configList[i].SettingKey;
                    decryptedVal = crypto.Unprotect(key, foundVal, encPrefix);
                    config[key]  = decryptedVal;
                }
            }
            return(config);
        }
Ejemplo n.º 2
0
        internal void UserLoop()
        {
            string message = null;

            string[] options;
            string   inputMain     = null;
            string   inputName     = null;
            string   inputValue    = null;
            string   inputConfirm  = null;
            string   plainVal      = null;
            string   encVal        = null;
            string   optionString  = null;
            string   input         = null;
            string   _encValPrefix = _prefix;


Start:
            while (true)
            {
                message =
                    "\n-- Enter [E] to Encrypt a configuration setting\n"
                    + "-- Enter [D] to Secrypt a configuration setting\n"
                    + "-- Enter [X] to Exit\n\n";
                options = new string[] { "E", "D", "X" };

                inputMain = GetUserChoice(message, options).ToUpper();
                if (inputMain == "X")
                {
                    return;
                }

                // if we are still here, then we wil either encrypt or decrypt a value
                string settingKey   = "";
                string settingValue = "";

                // Ask the user to confirm the name of the setting
                if (inputMain == "E")
                {
                    optionString = "ENCRYPT";
                }
                if (inputMain == "D")
                {
                    optionString = "DECRYPT";
                }

                if (_crypto != null)
                {
                    message = $"Enter the full key of the setting to be {optionString}ED";

                    //Get User Confirmation
                    inputName = GetUserInput(message);

                    message = $"Enter the value to be {optionString}ED";

                    inputValue = GetUserInput(message);

                    try
                    {
                        if (optionString == "ENCRYPT")
                        {
                            encVal  = _crypto.Protect(inputName, inputValue, _encValPrefix);
                            message = $"\nOperation Success!\n\nSettingName: {inputName}\nDecrypted Value:{inputValue}\nEncrypted Value:\n{encVal}";
                        }
                        if (optionString == "DECRYPT")
                        {
                            plainVal = _crypto.Unprotect(inputName, inputValue, _encValPrefix);
                            message  = $"\nOperation Success.\nSettingName: '{inputName}'\nEncrypted Value:'\n{inputValue}'\nDecrypted Value: '{plainVal}'";
                        }
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                    }
                    Console.WriteLine(message);
                }
                else
                {
                    throw new Exception("Cryptography services not initialized. Check configuration section 'ConfigOptions:Cryptography'");
                }

                goto Start;
            }
        }