移动加密方法,测试值转换器ok
parent
91fa8e75e1
commit
d7a76bc110
|
@ -1,4 +1,5 @@
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
|
using IRaCIS.Core.Infrastructure.Encryption;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
|
@ -47,7 +47,6 @@
|
||||||
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.400.16" />
|
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.400.16" />
|
||||||
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
|
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
|
||||||
<PackageReference Include="AWSSDK.S3" Version="3.7.402.7" />
|
<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="DocX" Version="3.0.1" />
|
||||||
<PackageReference Include="FreeSpire.Doc" Version="12.2.0" />
|
<PackageReference Include="FreeSpire.Doc" Version="12.2.0" />
|
||||||
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
|
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
|
||||||
|
|
|
@ -12406,20 +12406,6 @@
|
||||||
测试加密API 返回的结果
|
测试加密API 返回的结果
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</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.RSAEncryption.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.RSAEncryption.Encrypt(System.String,System.String)" -->
|
|
||||||
<member name="T:IRaCIS.Core.Application.BusinessFilter.GlobalExceptionHandler">
|
<member name="T:IRaCIS.Core.Application.BusinessFilter.GlobalExceptionHandler">
|
||||||
<summary>
|
<summary>
|
||||||
不生效,不知道为啥
|
不生效,不知道为啥
|
||||||
|
|
|
@ -6,6 +6,7 @@ using IRaCIS.Core.Application.Helper;
|
||||||
using IRaCIS.Core.Application.ViewModel;
|
using IRaCIS.Core.Application.ViewModel;
|
||||||
using IRaCIS.Core.Domain.Share;
|
using IRaCIS.Core.Domain.Share;
|
||||||
using IRaCIS.Core.Infrastructure;
|
using IRaCIS.Core.Infrastructure;
|
||||||
|
using IRaCIS.Core.Infrastructure.Encryption;
|
||||||
using IRaCIS.Core.Infrastructure.NewtonsoftJson;
|
using IRaCIS.Core.Infrastructure.NewtonsoftJson;
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
using Medallion.Threading;
|
using Medallion.Threading;
|
||||||
|
@ -104,6 +105,19 @@ namespace IRaCIS.Core.Application.Service
|
||||||
public string TestName { get; set; }
|
public string TestName { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IResponseOutput> TestAutoEncretpt([FromServices] IRepository<TestLength> _testLengthRepository)
|
||||||
|
{
|
||||||
|
await _testLengthRepository.AddAsync(new TestLength() { Name = "zhouhang1" });
|
||||||
|
await _testLengthRepository.AddAsync(new TestLength() { Name = "hewentao" });
|
||||||
|
|
||||||
|
await _testLengthRepository.SaveChangesAsync();
|
||||||
|
var list = _testLengthRepository.Where().ToList();
|
||||||
|
|
||||||
|
var exist = await _testLengthRepository.AnyAsync(t => t.Name == "zhouhang1");
|
||||||
|
|
||||||
|
return ResponseOutput.Ok(list, exist);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IResponseOutput> TestJson()
|
public async Task<IResponseOutput> TestJson()
|
||||||
{
|
{
|
||||||
|
@ -280,7 +294,7 @@ namespace IRaCIS.Core.Application.Service
|
||||||
|
|
||||||
var encreptMd5 = AesEncryption.Encrypt(MD5Helper.Md5("123456"), key);
|
var encreptMd5 = AesEncryption.Encrypt(MD5Helper.Md5("123456"), key);
|
||||||
Console.WriteLine(encreptMd5);
|
Console.WriteLine(encreptMd5);
|
||||||
var decrept= AesEncryption.Decrypt(encreptMd5, key);
|
var decrept = AesEncryption.Decrypt(encreptMd5, key);
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
using IRaCIS.Core.Infrastructure.Encryption;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Microsoft.EntityFrameworkCore.ValueGeneration;
|
||||||
|
|
||||||
|
namespace IRaCIS.Core.Infra.EFCore;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 某列保留前面几位,后续加密
|
||||||
|
/// </summary>
|
||||||
|
public class PartialEncryptionConverter : ValueConverter<string, string>
|
||||||
|
{
|
||||||
|
private readonly int _unencryptedPrefixLength;
|
||||||
|
|
||||||
|
public PartialEncryptionConverter(int unencryptedPrefixLength)
|
||||||
|
: base(
|
||||||
|
plainText => AesEncryption.EncryptPartial(plainText, unencryptedPrefixLength),
|
||||||
|
encryptedText => AesEncryption.DecryptPartial(encryptedText, unencryptedPrefixLength))
|
||||||
|
{
|
||||||
|
_unencryptedPrefixLength = unencryptedPrefixLength;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,12 @@
|
||||||
using IRaCIS.Core.Domain.Models;
|
using IRaCIS.Core.Domain.Models;
|
||||||
using IRaCIS.Core.Infra.EFCore.Common;
|
using IRaCIS.Core.Infra.EFCore.Common;
|
||||||
|
using IRaCIS.Core.Infrastructure.Encryption;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
using UserTypeGroup = IRaCIS.Core.Domain.Models.UserTypeGroup;
|
using UserTypeGroup = IRaCIS.Core.Domain.Models.UserTypeGroup;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Infra.EFCore;
|
namespace IRaCIS.Core.Infra.EFCore;
|
||||||
|
@ -54,6 +58,13 @@ public class IRaCISDBContext : DbContext
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
|
modelBuilder.Entity<TestLength>(entity =>
|
||||||
|
{
|
||||||
|
// 使用部分加密值转换器,前 2 个字符不加密,方便模糊搜索
|
||||||
|
entity.Property(e => e.Name)
|
||||||
|
.HasConversion(new PartialEncryptionConverter(2));
|
||||||
|
});
|
||||||
|
|
||||||
#region pgsql codefirst 配置 暂时屏蔽
|
#region pgsql codefirst 配置 暂时屏蔽
|
||||||
//if (base.Database.IsNpgsql())
|
//if (base.Database.IsNpgsql())
|
||||||
//{
|
//{
|
||||||
|
@ -544,6 +555,18 @@ public class IRaCISDBContext : DbContext
|
||||||
|
|
||||||
public virtual DbSet<TrialImageDownload> TrialImageDownload { get; set; }
|
public virtual DbSet<TrialImageDownload> TrialImageDownload { get; set; }
|
||||||
|
|
||||||
|
public virtual DbSet<TestLength> TestLength { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TestLength : Entity
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -18,6 +18,7 @@
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||||
<PackageReference Include="SharpCompress" Version="0.38.0" />
|
<PackageReference Include="SharpCompress" Version="0.38.0" />
|
||||||
<PackageReference Include="SharpZipLib" Version="1.4.2" />
|
<PackageReference Include="SharpZipLib" Version="1.4.2" />
|
||||||
|
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
|
||||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.5" />
|
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,11 @@ using Org.BouncyCastle.Crypto.Engines;
|
||||||
using Org.BouncyCastle.Crypto.Modes;
|
using Org.BouncyCastle.Crypto.Modes;
|
||||||
using Org.BouncyCastle.Crypto.Paddings;
|
using Org.BouncyCastle.Crypto.Paddings;
|
||||||
using Org.BouncyCastle.Crypto.Parameters;
|
using Org.BouncyCastle.Crypto.Parameters;
|
||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Application.BusinessFilter;
|
namespace IRaCIS.Core.Infrastructure.Encryption;
|
||||||
|
|
||||||
public class AesEncryption
|
public class AesEncryption
|
||||||
{
|
{
|
||||||
|
@ -84,4 +86,60 @@ public class AesEncryption
|
||||||
Array.Resize(ref output, length); // 调整输出数组大小以适应实际数据长度
|
Array.Resize(ref output, length); // 调整输出数组大小以适应实际数据长度
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string DefaultKey = "12345678901234567890123456789012";
|
||||||
|
|
||||||
|
public static string EncryptPartial(string plainText, int unencryptedPrefixLength)
|
||||||
|
{
|
||||||
|
if (plainText.Length <= unencryptedPrefixLength)
|
||||||
|
{
|
||||||
|
return Encrypt(plainText, DefaultKey); // 如果文本太短,直接加密
|
||||||
|
}
|
||||||
|
|
||||||
|
var prefix = plainText.Substring(0, unencryptedPrefixLength);
|
||||||
|
var suffix = plainText.Substring(unencryptedPrefixLength);
|
||||||
|
|
||||||
|
return prefix + Encrypt(suffix, DefaultKey); // 前缀保留,后缀加密
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string DecryptPartial(string encryptedText, int unencryptedPrefixLength)
|
||||||
|
{
|
||||||
|
if (encryptedText.Length <= unencryptedPrefixLength)
|
||||||
|
{
|
||||||
|
return Decrypt(encryptedText, DefaultKey); // 如果文本太短,直接解密
|
||||||
|
}
|
||||||
|
|
||||||
|
var prefix = encryptedText.Substring(0, unencryptedPrefixLength);
|
||||||
|
var suffix = encryptedText.Substring(unencryptedPrefixLength);
|
||||||
|
|
||||||
|
return prefix + Decrypt(suffix, DefaultKey); // 前缀保留,后缀解密
|
||||||
|
}
|
||||||
|
|
||||||
|
//public static string Encrypt(string plainText)
|
||||||
|
//{
|
||||||
|
// using var aes = Aes.Create();
|
||||||
|
// aes.Key = Encoding.UTF8.GetBytes(EncryptionKey);
|
||||||
|
// aes.Mode = CipherMode.ECB; // 根据需要选择加密模式,这里使用 ECB 模式
|
||||||
|
// aes.Padding = PaddingMode.PKCS7;
|
||||||
|
|
||||||
|
// var encryptor = aes.CreateEncryptor();
|
||||||
|
// var plainBytes = Encoding.UTF8.GetBytes(plainText);
|
||||||
|
// var encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
|
||||||
|
|
||||||
|
// return Convert.ToBase64String(encryptedBytes);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//public static string Decrypt(string encryptedText)
|
||||||
|
//{
|
||||||
|
// using var aes = Aes.Create();
|
||||||
|
// aes.Key = Encoding.UTF8.GetBytes(EncryptionKey);
|
||||||
|
// aes.Mode = CipherMode.ECB;
|
||||||
|
// aes.Padding = PaddingMode.PKCS7;
|
||||||
|
|
||||||
|
// var decryptor = aes.CreateDecryptor();
|
||||||
|
// var encryptedBytes = Convert.FromBase64String(encryptedText);
|
||||||
|
// var decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
|
||||||
|
|
||||||
|
// return Encoding.UTF8.GetString(decryptedBytes);
|
||||||
|
//}
|
||||||
}
|
}
|
|
@ -4,9 +4,11 @@ using Org.BouncyCastle.Crypto.Engines;
|
||||||
using Org.BouncyCastle.Crypto.Generators;
|
using Org.BouncyCastle.Crypto.Generators;
|
||||||
using Org.BouncyCastle.OpenSsl;
|
using Org.BouncyCastle.OpenSsl;
|
||||||
using Org.BouncyCastle.Security;
|
using Org.BouncyCastle.Security;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Application.BusinessFilter;
|
namespace IRaCIS.Core.Infrastructure.Encryption;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
|
Loading…
Reference in New Issue