Updated the encription fuction
This commit is contained in:
parent
4282a6e1f7
commit
9fbaf03ce4
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
public class EncryptResponseAttribute : TypeFilterAttribute
|
public class EncryptResponseAttribute : TypeFilterAttribute
|
||||||
{
|
{
|
||||||
@ -71,29 +72,62 @@ public class EncryptResponseAttribute : TypeFilterAttribute
|
|||||||
aes.Mode = CipherMode.CBC;
|
aes.Mode = CipherMode.CBC;
|
||||||
aes.Padding = PaddingMode.PKCS7;
|
aes.Padding = PaddingMode.PKCS7;
|
||||||
|
|
||||||
// We do NOT use 'using' on the MemoryStream here yet,
|
// 1. Convert string to bytes directly (Avoids StreamWriter encoding issues)
|
||||||
// because we need to read from it after the CryptoStream finishes.
|
var plainBytes = Encoding.UTF8.GetBytes(plainText);
|
||||||
|
|
||||||
using var ms = new MemoryStream();
|
using var ms = new MemoryStream();
|
||||||
|
|
||||||
// Write IV first (16 bytes)
|
// 2. Write IV (16 bytes)
|
||||||
ms.Write(aes.IV, 0, aes.IV.Length);
|
ms.Write(aes.IV, 0, aes.IV.Length);
|
||||||
|
|
||||||
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
|
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
|
||||||
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
|
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
|
||||||
using (var sw = new StreamWriter(cs))
|
|
||||||
{
|
{
|
||||||
// CRITICAL FIX: Use Async Write
|
// 3. Write Data
|
||||||
await sw.WriteAsync(plainText);
|
await cs.WriteAsync(plainBytes, 0, plainBytes.Length);
|
||||||
|
|
||||||
// Flush the writer, but do not close the underlying streams yet via 'using' exit
|
// 4. CRITICAL: Flush the final block (Padding) to the MemoryStream
|
||||||
await sw.FlushAsync();
|
// Without this, Dart receives incomplete data and throws "Invalid Padding"
|
||||||
|
cs.FlushFinalBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, CryptoStream is closed (disposed by using block),
|
// 5. Convert full stream to Base64
|
||||||
// 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());
|
return Convert.ToBase64String(ms.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//private async Task<string> 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());
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user