diff --git a/IRaCIS.Core.Application/BusinessFilter/EncreptApiResultFilter.cs b/IRaCIS.Core.Application/BusinessFilter/EncreptApiResultFilter.cs deleted file mode 100644 index fd58ae885..000000000 --- a/IRaCIS.Core.Application/BusinessFilter/EncreptApiResultFilter.cs +++ /dev/null @@ -1,71 +0,0 @@ -using IRaCIS.Core.Domain.Share; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Options; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace IRaCIS.Core.Application.BusinessFilter -{ - public class EncreptApiResultFilter : IAsyncResultFilter - { - - private readonly IOptionsMonitor _encreptResponseMonitor; - - public EncreptApiResultFilter(IOptionsMonitor encreptResponseMonitor) - { - _encreptResponseMonitor = encreptResponseMonitor; - } - - public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) - { - - if(_encreptResponseMonitor.CurrentValue.IsEnable) - { - - if (context.Result is ObjectResult objectResult) - { - var statusCode = objectResult.StatusCode ?? context.HttpContext.Response.StatusCode; - - var objectValue = objectResult.Value; - - - if (objectValue is IResponseOutput) - { - var responseOutput = objectValue as IResponseOutput; - - var path = context.HttpContext?.Request.Path.Value?.ToLower(); - - - if(!string.IsNullOrEmpty(path) && path.Length>5 && _encreptResponseMonitor.CurrentValue.ApiPathList.Contains(path.ToLower())) - { - - if(responseOutput.IsSuccess) - { - responseOutput.Code = ApiResponseCodeEnum.ResultEncrepted; - responseOutput.Data = JsonConvert.SerializeObject(Convert.ToBase64String(Encoding.UTF8.GetBytes(responseOutput.Data.ToString()))); - - objectResult.Value = responseOutput; - } - - } - - } - - - - } - } - - - - - await next.Invoke(); - - } - } -} diff --git a/IRaCIS.Core.Application/BusinessFilter/Encryption/AesEncryption.cs b/IRaCIS.Core.Application/BusinessFilter/Encryption/AesEncryption.cs new file mode 100644 index 000000000..8c17165c8 --- /dev/null +++ b/IRaCIS.Core.Application/BusinessFilter/Encryption/AesEncryption.cs @@ -0,0 +1,91 @@ +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Paddings; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Crypto; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Org.BouncyCastle.Crypto.Modes; + +namespace IRaCIS.Core.Application.BusinessFilter; + +public class AesEncryption +{ + // AES 加密(不带 IV) + public static string Encrypt(string plainText, string key) + { + var keyBytes = Encoding.UTF8.GetBytes(key); + + // 使用 AES 引擎 + PKCS7 填充 + var engine = new AesEngine(); + var blockCipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding()); + blockCipher.Init(true, new KeyParameter(keyBytes)); // true 表示加密 + + var inputBytes = Encoding.UTF8.GetBytes(plainText); + var encryptedBytes = ProcessCipher(blockCipher, inputBytes); + + // 返回 Base64 编码的加密字符串 + return Convert.ToBase64String(encryptedBytes); + } + + // AES 解密(不带 IV) + public static string Decrypt(string encryptedText, string key) + { + var keyBytes = Encoding.UTF8.GetBytes(key); + var cipherBytes = Convert.FromBase64String(encryptedText); + + // 使用 AES 引擎 + PKCS7 填充 + var engine = new AesEngine(); + var blockCipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding()); + blockCipher.Init(false, new KeyParameter(keyBytes)); // false 表示解密 + + var decryptedBytes = ProcessCipher(blockCipher, cipherBytes); + return Encoding.UTF8.GetString(decryptedBytes); + } + + // AES 加密(带 IV) + public static string Encrypt(string plainText, string key, string iv) + { + var keyBytes = Encoding.UTF8.GetBytes(key); + var ivBytes = Encoding.UTF8.GetBytes(iv); + + // 使用 AES 引擎 + PKCS7 填充 + CBC 模式 + var engine = new AesEngine(); + var blockCipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding()); + blockCipher.Init(true, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes)); // true 表示加密 + + var inputBytes = Encoding.UTF8.GetBytes(plainText); + var encryptedBytes = ProcessCipher(blockCipher, inputBytes); + + // 返回 Base64 编码的加密字符串 + return Convert.ToBase64String(encryptedBytes); + } + + // AES 解密(带 IV) + public static string Decrypt(string encryptedText, string key, string iv) + { + var keyBytes = Encoding.UTF8.GetBytes(key); + var ivBytes = Encoding.UTF8.GetBytes(iv); + var cipherBytes = Convert.FromBase64String(encryptedText); + + // 使用 AES 引擎 + PKCS7 填充 + CBC 模式 + var engine = new AesEngine(); + var blockCipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding()); + blockCipher.Init(false, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes)); // false 表示解密 + + var decryptedBytes = ProcessCipher(blockCipher, cipherBytes); + return Encoding.UTF8.GetString(decryptedBytes); + } + + // 处理加密/解密数据 + private static byte[] ProcessCipher(IBufferedCipher cipher, byte[] input) + { + var output = new byte[cipher.GetOutputSize(input.Length)]; + int length = cipher.ProcessBytes(input, 0, input.Length, output, 0); + length += cipher.DoFinal(output, length); + Array.Resize(ref output, length); // 调整输出数组大小以适应实际数据长度 + return output; + } +} diff --git a/IRaCIS.Core.Application/BusinessFilter/Encryption/EncreptApiResultFilter.cs b/IRaCIS.Core.Application/BusinessFilter/Encryption/EncreptApiResultFilter.cs new file mode 100644 index 000000000..bae8e48ad --- /dev/null +++ b/IRaCIS.Core.Application/BusinessFilter/Encryption/EncreptApiResultFilter.cs @@ -0,0 +1,64 @@ +using IRaCIS.Core.Domain.Share; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.BusinessFilter; + +/// +/// 测试加密API 返回的结果 +/// +public class EncreptApiResultFilter : IAsyncResultFilter +{ + private readonly IOptionsMonitor _encreptResponseMonitor; + + public EncreptApiResultFilter(IOptionsMonitor encreptResponseMonitor) + { + _encreptResponseMonitor = encreptResponseMonitor; + } + + public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + + if (_encreptResponseMonitor.CurrentValue.IsEnable) + { + + if (context.Result is ObjectResult objectResult) + { + var statusCode = objectResult.StatusCode ?? context.HttpContext.Response.StatusCode; + + var objectValue = objectResult.Value; + + + if (objectValue is IResponseOutput) + { + var responseOutput = objectValue as IResponseOutput; + + var path = context.HttpContext?.Request.Path.Value?.ToLower(); + + + if (!string.IsNullOrEmpty(path) && path.Length > 5 && _encreptResponseMonitor.CurrentValue.ApiPathList.Contains(path.ToLower())) + { + + if (responseOutput.IsSuccess) + { + responseOutput.Code = ApiResponseCodeEnum.ResultEncrepted; + responseOutput.Data = JsonConvert.SerializeObject(Convert.ToBase64String(Encoding.UTF8.GetBytes(responseOutput.Data.ToString()))); + + objectResult.Value = responseOutput; + } + + } + + } + } + } + await next.Invoke(); + } +} diff --git a/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs b/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAEncryption.cs similarity index 99% rename from IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs rename to IRaCIS.Core.Application/BusinessFilter/Encryption/RSAEncryption.cs index a89e57cbf..c4bd92d6e 100644 --- a/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs +++ b/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAEncryption.cs @@ -15,7 +15,7 @@ namespace IRaCIS.Core.Application.BusinessFilter; /// /// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html /// -public class RSAHelper +public class RSAEncryption { public static AsymmetricCipherKeyPair GenerateRSAKeyPair(int keySize) diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj index 0f1672ecf..9faf0ffec 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj @@ -62,6 +62,7 @@ + diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index db99c7473..1ce5ba12e 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -29,12 +29,17 @@ 签名 - + + + 测试加密API 返回的结果 + + + https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html - + RSA解密 @@ -42,7 +47,7 @@ 待解密的字符串(Base64) 解密后的字符串 - + 不生效,不知道为啥 diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index f6cf88f9e..2c12fa561 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -248,14 +248,40 @@ namespace IRaCIS.Application.Services [UnitOfWork] public async Task Get() { + string plainText = "Hello, BouncyCastle!"; + string key = "12345678901234567890123456789012"; // AES 密钥长度应为 16 字节(128 位) + string iv = "your-iv-12345678"; // IV 长度为 16 字节 + + Console.WriteLine($"原始文本: {plainText}"); + + // 加密 + string encrypted = AesEncryption.Encrypt(plainText, key, iv); + Console.WriteLine($"加密后的数据: {encrypted}"); + + // 解密 + string decrypted = AesEncryption.Decrypt(encrypted, key, iv); + Console.WriteLine($"解密后的数据: {decrypted}"); + + + + + Console.WriteLine($"原始文本: {plainText}"); + + // 加密 + string encrypte = AesEncryption.Encrypt(plainText, key); + Console.WriteLine($"加密后的数据: {encrypte}"); + + // 解密 + string decrypte = AesEncryption.Decrypt(encrypte, key); + Console.WriteLine($"解密后的数据: {decrypte}"); // Generate RSA keys - var keyPair = RSAHelper.GenerateRSAKeyPair(2048); + var keyPair = RSAEncryption.GenerateRSAKeyPair(2048); // Export the public and private keys to PEM format - string publicKey = RSAHelper.ExportPublicKey(keyPair.Public); - string privateKey = RSAHelper.ExportPrivateKey(keyPair.Private); + string publicKey = RSAEncryption.ExportPublicKey(keyPair.Public); + string privateKey = RSAEncryption.ExportPrivateKey(keyPair.Private); Console.WriteLine("Public Key:"); Console.WriteLine(publicKey); @@ -267,11 +293,11 @@ namespace IRaCIS.Application.Services Console.WriteLine("\nOriginal Data: " + dataToEncrypt); // Encrypt the data - var encryptedData = RSAHelper.Encrypt(publicKey, dataToEncrypt); + var encryptedData = RSAEncryption.Encrypt(publicKey, dataToEncrypt); Console.WriteLine("\nEncrypted Data: " + encryptedData); // Decrypt the data - string decryptedData = RSAHelper.Decrypt(privateKey, encryptedData); + string decryptedData = RSAEncryption.Decrypt(privateKey, encryptedData); Console.WriteLine("\nDecrypted Data: " + decryptedData);