Exemple #1
0
        public void Decrypt_secured_fields_in_json(string filename)
        {
            var cipher   = new Aes256Cipher(CipherKey);
            var jsonFile = new FileInfo(filename);
            var json     = File.ReadAllText(jsonFile.FullName);

            json = Regex.Replace(
                json,
                @"""CipherText:(?<CipherText>[^""]+)""",
                m =>
            {
                var cipherText = m.Groups["CipherText"].Value;
                try
                {
                    var text = cipher.Decrypt(cipherText);
                    return($@"""CipherText:{text}""");
                }
                catch (Exception)
                {
                    Console.WriteLine($@"Failed decrypting ""{cipherText}""");
                    return(m.Value);
                }
            });
            File.WriteAllText(jsonFile.FullName, json);
        }
        public static IConfigurationRoot Decrypt(this IConfigurationRoot root, string keyPath, string cipherPrefix)
        {
            var secret = root[keyPath];
            var cipher = new Aes256Cipher(secret);

            DecryptInChildren(root);
            return(root);

            void DecryptInChildren(IConfiguration parent)
            {
                foreach (var child in parent.GetChildren())
                {
                    if (child.Value?.StartsWith(cipherPrefix) == true)
                    {
                        var cipherText = child.Value.Substring(cipherPrefix.Length);
                        parent[child.Key] = cipher.Decrypt(cipherText);
                    }

                    DecryptInChildren(child);
                }
            }
        }
Exemple #3
0
 public void Generate_new_key_and_print()
 {
     Console.WriteLine("Key: " + Aes256Cipher.GenerateNewKey());
 }
Exemple #4
0
        public void Decrypt_value_and_print(string value)
        {
            var cipher = new Aes256Cipher(CipherKey);

            Console.WriteLine(cipher.Decrypt(value));
        }