加密解密组件准备完毕
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
3501f2acdf
commit
de274e00d6
|
@ -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<EncreptResponseOption> _encreptResponseMonitor;
|
||||
|
||||
public EncreptApiResultFilter(IOptionsMonitor<EncreptResponseOption> 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<object>;
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 测试加密API 返回的结果
|
||||
/// </summary>
|
||||
public class EncreptApiResultFilter : IAsyncResultFilter
|
||||
{
|
||||
private readonly IOptionsMonitor<EncreptResponseOption> _encreptResponseMonitor;
|
||||
|
||||
public EncreptApiResultFilter(IOptionsMonitor<EncreptResponseOption> 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<object>;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@ namespace IRaCIS.Core.Application.BusinessFilter;
|
|||
/// <summary>
|
||||
/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
||||
/// </summary>
|
||||
public class RSAHelper
|
||||
public class RSAEncryption
|
||||
{
|
||||
|
||||
public static AsymmetricCipherKeyPair GenerateRSAKeyPair(int keySize)
|
|
@ -62,6 +62,7 @@
|
|||
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.400.16" />
|
||||
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="3.7.402.7" />
|
||||
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
|
||||
<PackageReference Include="DocX" Version="3.0.1" />
|
||||
<PackageReference Include="FreeSpire.Doc" Version="12.2.0" />
|
||||
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
|
||||
|
|
|
@ -29,12 +29,17 @@
|
|||
签名
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.RSAHelper">
|
||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.EncreptApiResultFilter">
|
||||
<summary>
|
||||
测试加密API 返回的结果
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.RSAEncryption">
|
||||
<summary>
|
||||
https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.BusinessFilter.RSAHelper.Decrypt(System.String,System.String)">
|
||||
<member name="M:IRaCIS.Core.Application.BusinessFilter.RSAEncryption.Decrypt(System.String,System.String)">
|
||||
<summary>
|
||||
RSA解密
|
||||
</summary>
|
||||
|
@ -42,7 +47,7 @@
|
|||
<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)" -->
|
||||
<!-- Badly formed XML comment ignored for member "M:IRaCIS.Core.Application.BusinessFilter.RSAEncryption.Encrypt(System.String,System.String)" -->
|
||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.GlobalExceptionHandler">
|
||||
<summary>
|
||||
不生效,不知道为啥
|
||||
|
|
|
@ -248,14 +248,40 @@ namespace IRaCIS.Application.Services
|
|||
[UnitOfWork]
|
||||
public async Task<string> 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);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue