From 9fbaf03ce4cfea455cb896784c20847b20a979f0 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Sat, 6 Dec 2025 16:08:43 +0530 Subject: [PATCH] Updated the encription fuction --- .../Extensions/EncryptResponseAttribute.cs | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/Marco.Pms.Services/Extensions/EncryptResponseAttribute.cs b/Marco.Pms.Services/Extensions/EncryptResponseAttribute.cs index ad6a580..3b321fc 100644 --- a/Marco.Pms.Services/Extensions/EncryptResponseAttribute.cs +++ b/Marco.Pms.Services/Extensions/EncryptResponseAttribute.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.Filters; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Security.Cryptography; +using System.Text; public class EncryptResponseAttribute : TypeFilterAttribute { @@ -71,29 +72,62 @@ public class EncryptResponseAttribute : TypeFilterAttribute aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; - // We do NOT use 'using' on the MemoryStream here yet, - // because we need to read from it after the CryptoStream finishes. + // 1. Convert string to bytes directly (Avoids StreamWriter encoding issues) + var plainBytes = Encoding.UTF8.GetBytes(plainText); + using var ms = new MemoryStream(); - // Write IV first (16 bytes) + // 2. Write IV (16 bytes) ms.Write(aes.IV, 0, aes.IV.Length); using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV)) using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) - using (var sw = new StreamWriter(cs)) { - // CRITICAL FIX: Use Async Write - await sw.WriteAsync(plainText); + // 3. Write Data + await cs.WriteAsync(plainBytes, 0, plainBytes.Length); - // Flush the writer, but do not close the underlying streams yet via 'using' exit - await sw.FlushAsync(); + // 4. CRITICAL: Flush the final block (Padding) to the MemoryStream + // Without this, Dart receives incomplete data and throws "Invalid Padding" + cs.FlushFinalBlock(); } - // At this point, CryptoStream is closed (disposed by using block), - // causing the final block to be flushed to MemoryStream. - // MemoryStream is technically closed, but .NET allows ToArray() on closed MemoryStreams. - + // 5. Convert full stream to Base64 return Convert.ToBase64String(ms.ToArray()); } + + //private async Task EncryptAsync(string plainText) + //{ + // if (string.IsNullOrEmpty(plainText)) return plainText; + + // using var aes = Aes.Create(); + // aes.Key = Convert.FromBase64String(_keyBase64); + // aes.GenerateIV(); + // aes.Mode = CipherMode.CBC; + // aes.Padding = PaddingMode.PKCS7; + + // // We do NOT use 'using' on the MemoryStream here yet, + // // because we need to read from it after the CryptoStream finishes. + // using var ms = new MemoryStream(); + + // // Write IV first (16 bytes) + // ms.Write(aes.IV, 0, aes.IV.Length); + + // using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV)) + // using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) + // using (var sw = new StreamWriter(cs)) + // { + // // CRITICAL FIX: Use Async Write + // await sw.WriteAsync(plainText); + + // // Flush the writer, but do not close the underlying streams yet via 'using' exit + // await sw.FlushAsync(); + // } + + // // At this point, CryptoStream is closed (disposed by using block), + // // causing the final block to be flushed to MemoryStream. + // // MemoryStream is technically closed, but .NET allows ToArray() on closed MemoryStreams. + + // return Convert.ToBase64String(ms.ToArray()); + //} } } \ No newline at end of file