PortBlog.API/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs
Bangara Raju Kottedi 2533b5298f Support encrypted cache conn strings & update dependencies
Moved DecryptConnectionString to KBR.Shared.Extensions for reuse and improved configuration access. Added Encryption property to CacheOptions and appsettings for conditional decryption of cache connection strings. Updated ServiceCollectionExtensions to decrypt SQL Server cache connection strings when needed. Upgraded NuGet packages across projects to latest .NET 8/10.0.x versions. Included minor code cleanups and OpenApiSecurityScheme improvements.
2026-02-15 14:22:22 +05:30

47 lines
1.9 KiB
C#

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();
}
}
}
}
}
}
}