From a78bd2d54b9051596cccd16e11a66fa5e191d9a2 Mon Sep 17 00:00:00 2001 From: "jiahua.zhang" <279328316@qq.com> Date: Sun, 7 Apr 2024 18:40:16 +0800 Subject: [PATCH] =?UTF-8?q?1=20EnumHelper=E5=85=81=E8=AE=B8DisplayName?= =?UTF-8?q?=E8=BF=94=E5=9B=9E""=E5=80=BC=202=20QueryFilterHelper=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jc.Base/EnumHelper.cs | 4 +- Jc.Base/Jc.Base.csproj | 2 +- Jc.Cache/Jc.Cache.csproj | 2 +- .../Data/Query/QueryFilterHelper.cs | 9 +- Jc.Core.Database/Jc.Core.Database.csproj | 2 +- Jc.Core.MsSql/Jc.Core.MsSql.csproj | 2 +- Jc.Core.MySql/Jc.Core.MySql.csproj | 2 +- Jc.Core.PostgreSql/Jc.Core.PostgreSql.csproj | 2 +- Jc.Core.Sqlite/Jc.Core.Sqlite.csproj | 2 +- Jc.Core/Jc.Core.csproj | 2 +- Jc.Core/Security/AesHelper.cs | 289 ++++++++++++++++++ Jc.Core/Security/DesHelper.cs | 1 + Jc.Util.Excel/Jc.Util.Excel.csproj | 2 +- xcopyFile.bat | 2 +- 14 files changed, 310 insertions(+), 13 deletions(-) create mode 100644 Jc.Core/Security/AesHelper.cs diff --git a/Jc.Base/EnumHelper.cs b/Jc.Base/EnumHelper.cs index cc57fd3..a9baa81 100644 --- a/Jc.Base/EnumHelper.cs +++ b/Jc.Base/EnumHelper.cs @@ -285,7 +285,7 @@ public static string GetDisplayName(Type type) { result = attr.DisplayName; } - if(string.IsNullOrEmpty(result)) + if(result == null) { result = type.Name; } @@ -305,7 +305,7 @@ public static string GetDisplayName(FieldInfo fieldInfo) { result = attr.DisplayName; } - if(string.IsNullOrEmpty(result)) + if(result == null) { result = fieldInfo.Name; } diff --git a/Jc.Base/Jc.Base.csproj b/Jc.Base/Jc.Base.csproj index 2688bdc..536d3cf 100644 --- a/Jc.Base/Jc.Base.csproj +++ b/Jc.Base/Jc.Base.csproj @@ -6,7 +6,7 @@ true 简单好用的.net开源Orm框架 https://github.com/279328316/JcCore - 1.2.1.1 + 1.2.1.2 Jc https://github.com/279328316/JcCore https://github.com/279328316/JcCore diff --git a/Jc.Cache/Jc.Cache.csproj b/Jc.Cache/Jc.Cache.csproj index e8e5c0e..b709db6 100644 --- a/Jc.Cache/Jc.Cache.csproj +++ b/Jc.Cache/Jc.Cache.csproj @@ -5,7 +5,7 @@ true 简单好用的.net开源Orm框架 https://github.com/279328316/JcCore - 1.2.1.1 + 1.2.1.2 Jc.Core MIT https://github.com/279328316/JcCore diff --git a/Jc.Core.Database/Data/Query/QueryFilterHelper.cs b/Jc.Core.Database/Data/Query/QueryFilterHelper.cs index 76155e2..6106f08 100644 --- a/Jc.Core.Database/Data/Query/QueryFilterHelper.cs +++ b/Jc.Core.Database/Data/Query/QueryFilterHelper.cs @@ -442,7 +442,14 @@ private object MemberExpressionProvider(Expression exp) { if (me.Expression != null && !me.Expression.Type.IsValueType) { - result = dtoDbMapping.FieldMappings[memberName].FieldName; + if (me.Expression.Type == dtoDbMapping.EntityType && dtoDbMapping.FieldMappings.ContainsKey(memberName)) + { + result = dtoDbMapping.FieldMappings[memberName].FieldName; + } + else + { + result = Expression.Lambda(exp).Compile().DynamicInvoke(); + } } else { // me.Expression == null DateTime.Now Guid.Empty diff --git a/Jc.Core.Database/Jc.Core.Database.csproj b/Jc.Core.Database/Jc.Core.Database.csproj index ab13cc5..a00b732 100644 --- a/Jc.Core.Database/Jc.Core.Database.csproj +++ b/Jc.Core.Database/Jc.Core.Database.csproj @@ -3,7 +3,7 @@ netstandard2.0 True - 1.2.1.1 + 1.2.1.2 Jc.Core 简单好用的.net开源Orm框架 https://github.com/279328316/JcCore MIT diff --git a/Jc.Core.MsSql/Jc.Core.MsSql.csproj b/Jc.Core.MsSql/Jc.Core.MsSql.csproj index 791ab2b..852c989 100644 --- a/Jc.Core.MsSql/Jc.Core.MsSql.csproj +++ b/Jc.Core.MsSql/Jc.Core.MsSql.csproj @@ -3,7 +3,7 @@ netstandard2.0 True - 1.2.1.1 + 1.2.1.2 Jc.Core MIT 简单好用的.net开源Orm框架 diff --git a/Jc.Core.MySql/Jc.Core.MySql.csproj b/Jc.Core.MySql/Jc.Core.MySql.csproj index 19edaa9..fc5619c 100644 --- a/Jc.Core.MySql/Jc.Core.MySql.csproj +++ b/Jc.Core.MySql/Jc.Core.MySql.csproj @@ -9,7 +9,7 @@ https://github.com/279328316/JcCore MIT https://github.com/279328316/JcCore https://github.com/279328316/JcCore - 1.2.1.1 + 1.2.1.2 Jc;JcCore;Orm diff --git a/Jc.Core.PostgreSql/Jc.Core.PostgreSql.csproj b/Jc.Core.PostgreSql/Jc.Core.PostgreSql.csproj index 51bb825..046601e 100644 --- a/Jc.Core.PostgreSql/Jc.Core.PostgreSql.csproj +++ b/Jc.Core.PostgreSql/Jc.Core.PostgreSql.csproj @@ -3,7 +3,7 @@ netstandard2.0 True - 1.2.1.1 + 1.2.1.2 Jc.Core 简单好用的.net开源Orm框架 https://github.com/279328316/JcCore diff --git a/Jc.Core.Sqlite/Jc.Core.Sqlite.csproj b/Jc.Core.Sqlite/Jc.Core.Sqlite.csproj index a855ad9..8f164ea 100644 --- a/Jc.Core.Sqlite/Jc.Core.Sqlite.csproj +++ b/Jc.Core.Sqlite/Jc.Core.Sqlite.csproj @@ -3,7 +3,7 @@ netstandard2.0 True - 1.2.1.1 + 1.2.1.2 Jc.Core 简单好用的.net开源Orm框架 https://github.com/279328316/JcCore diff --git a/Jc.Core/Jc.Core.csproj b/Jc.Core/Jc.Core.csproj index b806571..9ab6bba 100644 --- a/Jc.Core/Jc.Core.csproj +++ b/Jc.Core/Jc.Core.csproj @@ -7,7 +7,7 @@ author:279328316@qq.com Jc;JcCore;Orm 简单好用的.net开源Orm框架 https://github.com/279328316/JcCore - 1.2.1.1 + 1.2.1.2 false Jc Jc.Core diff --git a/Jc.Core/Security/AesHelper.cs b/Jc.Core/Security/AesHelper.cs new file mode 100644 index 0000000..14fc38a --- /dev/null +++ b/Jc.Core/Security/AesHelper.cs @@ -0,0 +1,289 @@ +using System; +using System.Text; +using System.Security.Cryptography; +using System.IO; + +namespace Jc.Security +{ + /// + /// AesHelper + /// + public class AesHelper + { + //默认密钥向量 + private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; + /// + /// 加密字符串 + /// + /// 待加密的字符串 + /// 加密密钥,要求为8位 + /// 加密成功返回加密后的字符串,失败返回源串 + public static string Encrypt(string encryptString, string encryptKey) + { + try + { + if (encryptKey.Length > 8) + { + encryptKey = encryptKey.Substring(0, 8); + } + else if (encryptKey.Length < 8) + { + encryptKey = encryptKey.PadRight(8, '1'); + } + byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey); + byte[] rgbIV = Keys; + byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); + AesCryptoServiceProvider provider = new AesCryptoServiceProvider(); + MemoryStream mStream = new MemoryStream(); + CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); + cStream.Write(inputByteArray, 0, inputByteArray.Length); + cStream.FlushFinalBlock(); + return Convert.ToBase64String(mStream.ToArray()); + } + catch + { + return encryptString; + } + } + + /// + /// DES解密字符串 + /// + /// 待解密的字符串 + /// 解密密钥,要求为8位,和加密密钥相同 + /// 解密成功返回解密后的字符串,失败返源串 + public static string DecryptDES(string decryptString, string decryptKey) + { + try + { + if (decryptKey.Length > 8) + { + decryptKey = decryptKey.Substring(0, 8); + } + else if (decryptKey.Length < 8) + { + decryptKey = decryptKey.PadRight(8, '1'); + } + byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); + byte[] rgbIV = Keys; + byte[] inputByteArray = Convert.FromBase64String(decryptString); + AesCryptoServiceProvider DCSP = new AesCryptoServiceProvider(); + MemoryStream mStream = new MemoryStream(); + CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); + cStream.Write(inputByteArray, 0, inputByteArray.Length); + cStream.FlushFinalBlock(); + return Encoding.UTF8.GetString(mStream.ToArray()); + } + catch + { + return decryptString; + } + } + + private const ulong FC_TAG = 0xFC010203040506CF; + + private const int BUFFER_SIZE = 128 * 1024; + + /// + /// 检验两个Byte数组是否相同 + /// + /// Byte数组 + /// Byte数组 + /// true-相等 + private static bool CheckByteArrays(byte[] b1, byte[] b2) + { + if (b1.Length == b2.Length) + { + for (int i = 0; i < b1.Length; ++i) + { + if (b1[i] != b2[i]) + return false; + } + return true; + } + return false; + } + + /// + /// 创建Rijndael SymmetricAlgorithm + /// + /// 密码 + /// + /// 加密对象 + private static SymmetricAlgorithm CreateRijndael(string password, byte[] salt) + { + PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000); + + SymmetricAlgorithm sma = Rijndael.Create(); + sma.KeySize = 256; + sma.Key = pdb.GetBytes(32); + sma.Padding = PaddingMode.PKCS7; + return sma; + } + + /// + /// 加密文件随机数生成 + /// + private static RandomNumberGenerator rand = new RNGCryptoServiceProvider(); + + /// + /// 生成指定长度的随机Byte数组 + /// + /// Byte数组长度 + /// 随机Byte数组 + private static byte[] GenerateRandomBytes(int count) + { + byte[] bytes = new byte[count]; + rand.GetBytes(bytes); + return bytes; + } + + /// + /// 加密文件 + /// + /// 待加密文件 + /// 加密后输入文件 + /// 加密密码 + public static void EncryptFile(string inFile, string outFile, string password) + { + using (FileStream fin = File.OpenRead(inFile), + fout = File.OpenWrite(outFile)) + { + long lSize = fin.Length; // 输入文件长度 + int size = (int)lSize; + byte[] bytes = new byte[BUFFER_SIZE]; // 缓存 + int read = -1; // 输入文件读取数量 + int value = 0; + + // 获取IV和salt + byte[] IV = GenerateRandomBytes(16); + byte[] salt = GenerateRandomBytes(16); + + // 创建加密对象 + SymmetricAlgorithm sma = AesHelper.CreateRijndael(password, salt); + sma.IV = IV; + + // 在输出文件开始部分写入IV和salt + fout.Write(IV, 0, IV.Length); + fout.Write(salt, 0, salt.Length); + + // 创建散列加密 + HashAlgorithm hasher = SHA256.Create(); + using (CryptoStream cout = new CryptoStream(fout, sma.CreateEncryptor(), CryptoStreamMode.Write), + chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) + { + BinaryWriter bw = new BinaryWriter(cout); + bw.Write(lSize); + + bw.Write(FC_TAG); + + // 读写字节块到加密流缓冲区 + while ((read = fin.Read(bytes, 0, bytes.Length)) != 0) + { + cout.Write(bytes, 0, read); + chash.Write(bytes, 0, read); + value += read; + } + // 关闭加密流 + chash.Flush(); + chash.Close(); + + // 读取散列 + byte[] hash = hasher.Hash; + + // 输入文件写入散列 + cout.Write(hash, 0, hash.Length); + + // 关闭文件流 + cout.Flush(); + cout.Close(); + } + } + } + + /// + /// 解密文件 + /// + /// 待解密文件 + /// 解密后输出文件 + /// 解密密码 + public static void DecryptFile(string inFile, string outFile, string password) + { + // 创建打开文件流 + using (FileStream fin = File.OpenRead(inFile), + fout = File.OpenWrite(outFile)) + { + int size = (int)fin.Length; + byte[] bytes = new byte[BUFFER_SIZE]; + int read = -1; + int value = 0; + int outValue = 0; + + byte[] IV = new byte[16]; + fin.Read(IV, 0, 16); + byte[] salt = new byte[16]; + fin.Read(salt, 0, 16); + + SymmetricAlgorithm sma = AesHelper.CreateRijndael(password, salt); + sma.IV = IV; + + value = 32; + long lSize = -1; + + // 创建散列对象, 校验文件 + HashAlgorithm hasher = SHA256.Create(); + + using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), + chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write)) + { + // 读取文件长度 + BinaryReader br = new BinaryReader(cin); + lSize = br.ReadInt64(); + ulong tag = br.ReadUInt64(); + + if (FC_TAG != tag) + throw new System.Exception("文件被破坏"); + + long numReads = lSize / BUFFER_SIZE; + + long slack = (long)lSize % BUFFER_SIZE; + + for (int i = 0; i < numReads; ++i) + { + read = cin.Read(bytes, 0, bytes.Length); + fout.Write(bytes, 0, read); + chash.Write(bytes, 0, read); + value += read; + outValue += read; + } + + if (slack > 0) + { + read = cin.Read(bytes, 0, (int)slack); + fout.Write(bytes, 0, read); + chash.Write(bytes, 0, read); + value += read; + outValue += read; + } + + chash.Flush(); + chash.Close(); + + fout.Flush(); + fout.Close(); + + byte[] curHash = hasher.Hash; + + // 获取比较和旧的散列对象 + byte[] oldHash = new byte[hasher.HashSize / 8]; + read = cin.Read(oldHash, 0, oldHash.Length); + if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash))) + throw new System.Exception("文件被破坏"); + } + + if (outValue != lSize) + throw new System.Exception("文件大小不匹配"); + } + } + } +} diff --git a/Jc.Core/Security/DesHelper.cs b/Jc.Core/Security/DesHelper.cs index 48729eb..f4125a0 100644 --- a/Jc.Core/Security/DesHelper.cs +++ b/Jc.Core/Security/DesHelper.cs @@ -7,6 +7,7 @@ namespace Jc.Security { /// /// DesHelper + /// 不如AES加密方式安全,推荐使用AES加密方法 /// public class DesHelper { diff --git a/Jc.Util.Excel/Jc.Util.Excel.csproj b/Jc.Util.Excel/Jc.Util.Excel.csproj index 5456e27..820a49a 100644 --- a/Jc.Util.Excel/Jc.Util.Excel.csproj +++ b/Jc.Util.Excel/Jc.Util.Excel.csproj @@ -5,7 +5,7 @@ true Excel操作Helper 简单Excel导入导出操作 - 1.2.1.1 + 1.2.1.2 True Jc.Core MIT diff --git a/xcopyFile.bat b/xcopyFile.bat index 9e11fee..7c2bac3 100644 --- a/xcopyFile.bat +++ b/xcopyFile.bat @@ -1,7 +1,7 @@ echo off cls :: 设置变量不能有空格 等号左边不允许有空格,等号右边的所有包括空格会全部赋值给变量。 -set version=1.2.1.1 +set version=1.2.1.2 echo User Dir %USERPROFILE% FileVersion : %version% set curDir="%USERPROFILE%\.nuget\packages\jc.base\%version%\lib\netstandard2.0\"