using Microsoft.Extensions.Configuration; using System.Security.Cryptography; namespace KBR.Shared.Extensions { public static class ConfigurationExtensions { public static string DecryptConnectionString(this IConfiguration configuration, string encryptedConnectionString) { // Fix: Use GetSection and Value instead of GetValue (since GetValue is not available on IConfiguration) string keyBase64 = configuration.GetSection("ConnectionStrings:Key").Value; string vectorBase64 = encryptedConnectionString.Split(":")[1]; string cipherText = encryptedConnectionString.Split(":")[0]; using (Aes aesAlgorithm = Aes.Create()) { aesAlgorithm.Key = Convert.FromBase64String(keyBase64); aesAlgorithm.IV = Convert.FromBase64String(vectorBase64); Console.WriteLine($"Aes Cipher Mode : {aesAlgorithm.Mode}"); Console.WriteLine($"Aes Padding Mode: {aesAlgorithm.Padding}"); Console.WriteLine($"Aes Key Size : {aesAlgorithm.KeySize}"); Console.WriteLine($"Aes Block Size : {aesAlgorithm.BlockSize}"); // Create decryptor object ICryptoTransform decryptor = aesAlgorithm.CreateDecryptor(); byte[] cipher = Convert.FromBase64String(cipherText); //Decryption will be done in a memory stream through a CryptoStream object using (MemoryStream ms = new MemoryStream(cipher)) { using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { using (StreamReader sr = new StreamReader(cs)) { return sr.ReadToEnd(); } } } } } } }