From 3501f2acdfbcf77767fa1129b45327d2365ad9a4 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 18 Sep 2024 18:03:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E8=A7=A3=E5=AF=86=E9=A2=84=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Encryption/EncryptionRequestMiddleware.cs | 67 +++++++++++++ .../BusinessFilter/Encryption/RSAHelper.cs | 96 ++++++++++++++++++ IRaCIS.Core.Application/Helper/RSAHelper.cs | 97 ------------------- .../IRaCIS.Core.Application.xml | 28 +++--- IRaCIS.Core.Application/TestService.cs | 1 + 5 files changed, 178 insertions(+), 111 deletions(-) create mode 100644 IRaCIS.Core.Application/BusinessFilter/Encryption/EncryptionRequestMiddleware.cs create mode 100644 IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs delete mode 100644 IRaCIS.Core.Application/Helper/RSAHelper.cs diff --git a/IRaCIS.Core.Application/BusinessFilter/Encryption/EncryptionRequestMiddleware.cs b/IRaCIS.Core.Application/BusinessFilter/Encryption/EncryptionRequestMiddleware.cs new file mode 100644 index 000000000..055e306f9 --- /dev/null +++ b/IRaCIS.Core.Application/BusinessFilter/Encryption/EncryptionRequestMiddleware.cs @@ -0,0 +1,67 @@ +using DocumentFormat.OpenXml.InkML; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.BusinessFilter; + +public class EncryptionRequestMiddleware +{ + private readonly RequestDelegate _next; + + public EncryptionRequestMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task InvokeAsync(HttpContext context) + { + // 检查请求头中是否包含加密的对称密钥 + if (context.Request.Headers.ContainsKey("X-Encrypted-Key")) + { + var encryptedSymmetricKey = Convert.FromBase64String(context.Request.Headers["X-Encrypted-Key"]); + + //// 使用私钥解密对称密钥 + //var decryptedSymmetricKey = RsaEncryptionHelper.DecryptRsa(encryptedSymmetricKey, _rsaPrivateKey); + //var aesKey = decryptedSymmetricKey[..32]; // 前32字节作为AES密钥 + //var aesIv = decryptedSymmetricKey[32..]; // 后面16字节作为IV + + //// 读取并解密请求体中的JSON数据 + //context.Request.EnableBuffering(); + //using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8, leaveOpen: true)) + //{ + // var encryptedBody = await reader.ReadToEndAsync(); + // context.Request.Body.Position = 0; + + // // 尝试解析为JObject + // var encryptedJson = JObject.Parse(encryptedBody); + // var decryptedJson = new JObject(); + + // // 解密每个字段的值 + // foreach (var property in encryptedJson.Properties()) + // { + // var encryptedValue = property.Value.ToString(); + // var decryptedValue = AesEncryptionHelper.DecryptString(encryptedValue, aesKey, aesIv); + // decryptedJson[property.Name] = decryptedValue; + // } + + // // 将解密后的JSON对象转换回字符串,并替换原始请求体 + // var decryptedBody = decryptedJson.ToString(); + // var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(decryptedBody)); + // context.Request.Body = bodyStream; + // context.Request.ContentLength = bodyStream.Length; + // bodyStream.Seek(0, SeekOrigin.Begin); + } + + + // 调用下一个中间件 + await _next(context); + } + + + +} diff --git a/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs b/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs new file mode 100644 index 000000000..a89e57cbf --- /dev/null +++ b/IRaCIS.Core.Application/BusinessFilter/Encryption/RSAHelper.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Encodings; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Generators; +using Org.BouncyCastle.OpenSsl; +using Org.BouncyCastle.Security; + +namespace IRaCIS.Core.Application.BusinessFilter; + +/// +/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html +/// +public class RSAHelper +{ + + public static AsymmetricCipherKeyPair GenerateRSAKeyPair(int keySize) + { + var keyGenerationParameters = new KeyGenerationParameters(new SecureRandom(), keySize); + var keyPairGenerator = new RsaKeyPairGenerator(); + keyPairGenerator.Init(keyGenerationParameters); + return keyPairGenerator.GenerateKeyPair(); + } + + public static string ExportPublicKey(AsymmetricKeyParameter publicKey) + { + using (StringWriter sw = new StringWriter()) + { + PemWriter pw = new PemWriter(sw); + pw.WriteObject(publicKey); + pw.Writer.Flush(); + return sw.ToString(); + } + } + + public static string ExportPrivateKey(AsymmetricKeyParameter privateKey) + { + using (StringWriter sw = new StringWriter()) + { + PemWriter pw = new PemWriter(sw); + pw.WriteObject(privateKey); + pw.Writer.Flush(); + return sw.ToString(); + } + } + + /// + /// RSA解密 + /// + /// 私钥 + /// 待解密的字符串(Base64) + /// 解密后的字符串 + public static string Decrypt(string privateKey, string decryptstring) + { + using (TextReader reader = new StringReader(privateKey)) + { + dynamic key = new PemReader(reader).ReadObject(); + var rsaDecrypt = new Pkcs1Encoding(new RsaEngine()); + if (key is AsymmetricKeyParameter) + { + key = (AsymmetricKeyParameter)key; + } + else if (key is AsymmetricCipherKeyPair) + { + key = ((AsymmetricCipherKeyPair)key).Private; + } + rsaDecrypt.Init(false, key); //这里加密是true;解密是false + + byte[] entData = Convert.FromBase64String(decryptstring); + entData = rsaDecrypt.ProcessBlock(entData, 0, entData.Length); + return Encoding.UTF8.GetString(entData); + } + }/// + + /// 加密 + /// + /// 公钥 + /// 待加密的字符串 + /// 加密后的Base64 + public static string Encrypt(string publicKey, string encryptstring) + { + using (TextReader reader = new StringReader(publicKey)) + { + AsymmetricKeyParameter key = new PemReader(reader).ReadObject() as AsymmetricKeyParameter; + Pkcs1Encoding pkcs1 = new Pkcs1Encoding(new RsaEngine()); + pkcs1.Init(true, key);//加密是true;解密是false; + byte[] entData = Encoding.UTF8.GetBytes(encryptstring); + entData = pkcs1.ProcessBlock(entData, 0, entData.Length); + return Convert.ToBase64String(entData); + } + } +} diff --git a/IRaCIS.Core.Application/Helper/RSAHelper.cs b/IRaCIS.Core.Application/Helper/RSAHelper.cs deleted file mode 100644 index fb5309691..000000000 --- a/IRaCIS.Core.Application/Helper/RSAHelper.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Encodings; -using Org.BouncyCastle.Crypto.Engines; -using Org.BouncyCastle.Crypto.Generators; -using Org.BouncyCastle.OpenSsl; -using Org.BouncyCastle.Security; - -namespace IRaCIS.Core.Application.Helper -{ - /// - /// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html - /// - public class RSAHelper - { - - public static AsymmetricCipherKeyPair GenerateRSAKeyPair(int keySize) - { - var keyGenerationParameters = new KeyGenerationParameters(new SecureRandom(), keySize); - var keyPairGenerator = new RsaKeyPairGenerator(); - keyPairGenerator.Init(keyGenerationParameters); - return keyPairGenerator.GenerateKeyPair(); - } - - public static string ExportPublicKey(AsymmetricKeyParameter publicKey) - { - using (StringWriter sw = new StringWriter()) - { - PemWriter pw = new PemWriter(sw); - pw.WriteObject(publicKey); - pw.Writer.Flush(); - return sw.ToString(); - } - } - - public static string ExportPrivateKey(AsymmetricKeyParameter privateKey) - { - using (StringWriter sw = new StringWriter()) - { - PemWriter pw = new PemWriter(sw); - pw.WriteObject(privateKey); - pw.Writer.Flush(); - return sw.ToString(); - } - } - - /// - /// RSA解密 - /// - /// 私钥 - /// 待解密的字符串(Base64) - /// 解密后的字符串 - public static string Decrypt(string privateKey, string decryptstring) - { - using (TextReader reader = new StringReader(privateKey)) - { - dynamic key = new PemReader(reader).ReadObject(); - var rsaDecrypt = new Pkcs1Encoding(new RsaEngine()); - if (key is AsymmetricKeyParameter) - { - key = (AsymmetricKeyParameter)key; - } - else if (key is AsymmetricCipherKeyPair) - { - key = ((AsymmetricCipherKeyPair)key).Private; - } - rsaDecrypt.Init(false, key); //这里加密是true;解密是false - - byte[] entData = Convert.FromBase64String(decryptstring); - entData = rsaDecrypt.ProcessBlock(entData, 0, entData.Length); - return Encoding.UTF8.GetString(entData); - } - }/// - - /// 加密 - /// - /// 公钥 - /// 待加密的字符串 - /// 加密后的Base64 - public static string Encrypt(string publicKey, string encryptstring) - { - using (TextReader reader = new StringReader(publicKey)) - { - AsymmetricKeyParameter key = new PemReader(reader).ReadObject() as AsymmetricKeyParameter; - Pkcs1Encoding pkcs1 = new Pkcs1Encoding(new RsaEngine()); - pkcs1.Init(true, key);//加密是true;解密是false; - byte[] entData = Encoding.UTF8.GetBytes(encryptstring); - entData = pkcs1.ProcessBlock(entData, 0, entData.Length); - return Convert.ToBase64String(entData); - } - } - } -} diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 063780b32..db99c7473 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -29,6 +29,20 @@ 签名 + + + https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html + + + + + RSA解密 + + 私钥 + 待解密的字符串(Base64) + 解密后的字符串 + + 不生效,不知道为啥 @@ -102,20 +116,6 @@ - - - https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html - - - - - RSA解密 - - 私钥 - 待解密的字符串(Base64) - 解密后的字符串 - - 利用DocX 库 处理word国际化模板 diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index c36a7c6b5..f6cf88f9e 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -3,6 +3,7 @@ using DocumentFormat.OpenXml.Drawing.Charts; using DocumentFormat.OpenXml.Wordprocessing; using IP2Region.Net.XDB; using IRaCIS.Application.Contracts; +using IRaCIS.Core.Application.BusinessFilter; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Helper; using IRaCIS.Core.Application.Service;