加解密预备
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
2dd86652f3
commit
3501f2acdf
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RSA解密
|
||||
/// </summary>
|
||||
/// <param name="privateKey">私钥</param>
|
||||
/// <param name="decryptstring">待解密的字符串(Base64)</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
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);
|
||||
}
|
||||
}/// <summary>
|
||||
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="publicKey">公钥</param>
|
||||
/// <param name="encryptstring">待加密的字符串</param>
|
||||
/// <returns>加密后的Base64</returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RSA解密
|
||||
/// </summary>
|
||||
/// <param name="privateKey">私钥</param>
|
||||
/// <param name="decryptstring">待解密的字符串(Base64)</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
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);
|
||||
}
|
||||
}/// <summary>
|
||||
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="publicKey">公钥</param>
|
||||
/// <param name="encryptstring">待加密的字符串</param>
|
||||
/// <returns>加密后的Base64</returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,6 +29,20 @@
|
|||
签名
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.RSAHelper">
|
||||
<summary>
|
||||
https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.BusinessFilter.RSAHelper.Decrypt(System.String,System.String)">
|
||||
<summary>
|
||||
RSA解密
|
||||
</summary>
|
||||
<param name="privateKey">私钥</param>
|
||||
<param name="decryptstring">待解密的字符串(Base64)</param>
|
||||
<returns>解密后的字符串</returns>
|
||||
</member>
|
||||
<!-- Badly formed XML comment ignored for member "M:IRaCIS.Core.Application.BusinessFilter.RSAHelper.Encrypt(System.String,System.String)" -->
|
||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.GlobalExceptionHandler">
|
||||
<summary>
|
||||
不生效,不知道为啥
|
||||
|
@ -102,20 +116,6 @@
|
|||
<param name="prefix"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:IRaCIS.Core.Application.Helper.RSAHelper">
|
||||
<summary>
|
||||
https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Helper.RSAHelper.Decrypt(System.String,System.String)">
|
||||
<summary>
|
||||
RSA解密
|
||||
</summary>
|
||||
<param name="privateKey">私钥</param>
|
||||
<param name="decryptstring">待解密的字符串(Base64)</param>
|
||||
<returns>解密后的字符串</returns>
|
||||
</member>
|
||||
<!-- Badly formed XML comment ignored for member "M:IRaCIS.Core.Application.Helper.RSAHelper.Encrypt(System.String,System.String)" -->
|
||||
<member name="T:IRaCIS.Core.Application.Helper.WordTempleteHelper">
|
||||
<summary>
|
||||
利用DocX 库 处理word国际化模板
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue