From 638e680a97533377123145460b55584f44ab1c1b Mon Sep 17 00:00:00 2001 From: Bangara Raju Kottedi Date: Sat, 14 Mar 2026 22:57:00 +0530 Subject: [PATCH] Upgrade dependencies, add encrypted connection strings - Updated NuGet packages across projects for compatibility and bug fixes - Switched to Microsoft.OpenApi and refactored Swagger setup - Added AES encryption/decryption for connection strings - Stored encrypted DB/cache connection strings in config - Improved encryption reliability in Program.cs - Added AutoMapper to multiple projects for mapping support - Enhanced security and code maintainability --- AesEncryption/Program.cs | 4 +- .../Extensions/ConfigureSwaggerOptions.cs | 2 +- PortBlog.API/PortBlog.API.csproj | 23 +++++----- PortBlog.API/Program.cs | 29 +----------- PortBlog.API/appsettings.Development.json | 8 ++-- PortBlog.Tests/PortBlog.Tests.csproj | 3 +- Shared/KBR.Cache/KBR.Cache.csproj | 17 +++---- Shared/KBR.Share.Lite/KBR.Shared.Lite.csproj | 1 + .../Extensions/ConfigurationExtensions.cs | 46 +++++++++++++++---- Shared/KBR.Shared/KBR.Shared.csproj | 5 +- 10 files changed, 72 insertions(+), 66 deletions(-) diff --git a/AesEncryption/Program.cs b/AesEncryption/Program.cs index c849d27..4568445 100644 --- a/AesEncryption/Program.cs +++ b/AesEncryption/Program.cs @@ -94,10 +94,10 @@ static string EncryptDataWithAes(string plainText, string keyBase64, out string { using (StreamWriter sw = new StreamWriter(cs)) { - sw.Write(plainText); + sw.Write(plainText); // Missing explicit flush before dispose } - encryptedData = ms.ToArray(); } + encryptedData = ms.ToArray(); } return Convert.ToBase64String(encryptedData); diff --git a/PortBlog.API/Extensions/ConfigureSwaggerOptions.cs b/PortBlog.API/Extensions/ConfigureSwaggerOptions.cs index 6e6a928..a0f323b 100644 --- a/PortBlog.API/Extensions/ConfigureSwaggerOptions.cs +++ b/PortBlog.API/Extensions/ConfigureSwaggerOptions.cs @@ -1,6 +1,6 @@ using Asp.Versioning.ApiExplorer; using Microsoft.Extensions.Options; -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; using Swashbuckle.AspNetCore.SwaggerGen; public class ConfigureSwaggerOptions : IConfigureOptions diff --git a/PortBlog.API/PortBlog.API.csproj b/PortBlog.API/PortBlog.API.csproj index 1abaca1..7541d89 100644 --- a/PortBlog.API/PortBlog.API.csproj +++ b/PortBlog.API/PortBlog.API.csproj @@ -11,22 +11,23 @@ - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + - - - + + + - + diff --git a/PortBlog.API/Program.cs b/PortBlog.API/Program.cs index 5c67595..00adcc5 100644 --- a/PortBlog.API/Program.cs +++ b/PortBlog.API/Program.cs @@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.FileProviders; using Microsoft.IdentityModel.Tokens; -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; using PortBlog.API.DbContexts; using PortBlog.API.Extensions; using PortBlog.API.Middleware; @@ -13,6 +13,7 @@ using Serilog; using System.Reflection; using System.Text; using KBR.Shared.Extensions; +using Swashbuckle.AspNetCore.SwaggerGen; var builder = WebApplication.CreateBuilder(args); @@ -143,15 +144,6 @@ builder.Services.AddSwaggerGen(c => Scheme = "ApiKeyScheme" }); - var key = new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "ApiKey" - } - }; - // JWT Bearer Security Definition c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { @@ -162,23 +154,6 @@ builder.Services.AddSwaggerGen(c => Scheme = "Bearer", BearerFormat = "JWT" }); - - var bearerScheme = new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "Bearer" - } - }; - - var requirement = new OpenApiSecurityRequirement - { - { key, new List() }, - { bearerScheme, new List() } - }; - - c.AddSecurityRequirement(requirement); }); builder.Services.ConfigureOptions(); diff --git a/PortBlog.API/appsettings.Development.json b/PortBlog.API/appsettings.Development.json index 1f73a69..d2d9a02 100644 --- a/PortBlog.API/appsettings.Development.json +++ b/PortBlog.API/appsettings.Development.json @@ -1,12 +1,12 @@ { "ConnectionStrings": { - "PortBlogDBConnectionString": "SERVER=192.168.0.197; DATABASE=cv_blog; UID=PortBlogDevUser; PWD=p@$$w0rd1234", - "Encryption": "false", + "PortBlogDBConnectionString": "XHIrOTmGNBQ8xE5DPwiEvtoNkU5YT6HsgwPYYawv7sRsMCnPGrAxbaiUZZ4mveUBP5yIAzzU5KSXK5XeoA9xxXugBF3nji2icvCQvoFxRqc=:CbDuLYbh/dAGyNU38qCHBQ==", + "Encryption": "true", "Key": "rgdBsYjrgQV9YaE+6QFK5oyTOWwbl2bSWkuc2JXcIyw=" }, "Cache": { - "ConnectionString": "SERVER=192.168.0.197; DATABASE=cv_blog; UID=PortBlogDevUser; PWD=p@$$w0rd1234;Allow User Variables=true;", - "Encryption": "false", + "ConnectionString": "pxv9z73rYXioZIToJ4A4CfpmR3m6OeQ5CD5bdjXAKuH1UBu/RvdOPuUnxokDytq/vlDYfDdrsffNAs5t5r6VSVD/H/bvgGQV+I3X2mzo1343XbJfncPMORbJPP/tSdl0Yu5R5pMhrSXnG7vOHPu+Bw==:gHnavGn1ASf0Sovu3zquGg==", + "Encryption": "true", "Provider": "SqlServer" }, "Jwt": { diff --git a/PortBlog.Tests/PortBlog.Tests.csproj b/PortBlog.Tests/PortBlog.Tests.csproj index 163250f..12bdcb6 100644 --- a/PortBlog.Tests/PortBlog.Tests.csproj +++ b/PortBlog.Tests/PortBlog.Tests.csproj @@ -8,11 +8,12 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all diff --git a/Shared/KBR.Cache/KBR.Cache.csproj b/Shared/KBR.Cache/KBR.Cache.csproj index 9e1d787..1de2cd1 100644 --- a/Shared/KBR.Cache/KBR.Cache.csproj +++ b/Shared/KBR.Cache/KBR.Cache.csproj @@ -7,14 +7,15 @@ - - - - - - - - + + + + + + + + + diff --git a/Shared/KBR.Share.Lite/KBR.Shared.Lite.csproj b/Shared/KBR.Share.Lite/KBR.Shared.Lite.csproj index 22badd1..e5d898e 100644 --- a/Shared/KBR.Share.Lite/KBR.Shared.Lite.csproj +++ b/Shared/KBR.Share.Lite/KBR.Shared.Lite.csproj @@ -7,6 +7,7 @@ + diff --git a/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs b/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs index 7a026c5..80f73b4 100644 --- a/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs +++ b/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs @@ -1,13 +1,45 @@ using Microsoft.Extensions.Configuration; using System.Security.Cryptography; +using System.Text; namespace KBR.Shared.Extensions { public static class ConfigurationExtensions { + public static string EncryptConnectionString(this IConfiguration configuration, string plainConnectionString) + { + string keyBase64 = configuration.GetSection("ConnectionStrings:Key").Value; + + using (Aes aesAlgorithm = Aes.Create()) + { + aesAlgorithm.Key = Convert.FromBase64String(keyBase64); + aesAlgorithm.GenerateIV(); + aesAlgorithm.Mode = CipherMode.CBC; + aesAlgorithm.Padding = PaddingMode.PKCS7; + + ICryptoTransform encryptor = aesAlgorithm.CreateEncryptor(); + + byte[] encryptedData; + using (MemoryStream ms = new MemoryStream()) + { + using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) + { + byte[] plainBytes = Encoding.UTF8.GetBytes(plainConnectionString); + cs.Write(plainBytes, 0, plainBytes.Length); + cs.FlushFinalBlock(); // Explicitly apply padding + } + encryptedData = ms.ToArray(); + } + + string cipherBase64 = Convert.ToBase64String(encryptedData); + string ivBase64 = Convert.ToBase64String(aesAlgorithm.IV); + + return $"{cipherBase64}:{ivBase64}"; + } + } + 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]; @@ -17,24 +49,18 @@ namespace KBR.Shared.Extensions { aesAlgorithm.Key = Convert.FromBase64String(keyBase64); aesAlgorithm.IV = Convert.FromBase64String(vectorBase64); + aesAlgorithm.Mode = CipherMode.CBC; + aesAlgorithm.Padding = PaddingMode.PKCS7; - 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)) + using (StreamReader sr = new StreamReader(cs, Encoding.UTF8)) { return sr.ReadToEnd(); } diff --git a/Shared/KBR.Shared/KBR.Shared.csproj b/Shared/KBR.Shared/KBR.Shared.csproj index 69ee995..93c93a4 100644 --- a/Shared/KBR.Shared/KBR.Shared.csproj +++ b/Shared/KBR.Shared/KBR.Shared.csproj @@ -7,8 +7,9 @@ - - + + +