Browse Source

😁优化调整国密算法

zuohuaijun 3 years ago
parent
commit
d887ebc8ec

+ 1 - 1
Admin.NET/Admin.NET.Application/Configuration/App.json

@@ -36,7 +36,7 @@
         "SeqBitLength": 6 // 序列数位长 默认值6,取值范围 [3, 21](建议不小于4)
     },
     "Cryptogram": {
-        "CryptoType": "SM2", // 密码加密算法 MD5、SM2(国密)
+        "CryptoType": "SM2", // 密码加密算法:MD5、SM2、SM4
         "PublicKey": "04F6E0C3345AE42B51E06BF50B98834988D54EBC7460FE135A48171BC0629EAE205EEDE253A530608178A98F1E19BB737302813BA39ED3FA3C51639D7A20C7391A", // 公钥
         "PrivateKey": "3690655E33D5EA3D9A4AE1A1ADD766FDEA045CDEAA43A9206FB8C430CEFE0D94" // 私钥
     }

+ 5 - 5
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -21,13 +21,13 @@
     <PackageReference Include="DotNetCore.Compile.Environment" Version="3.2.0" />
     <PackageReference Include="DotNetCore.Natasha.CSharp" Version="5.1.0" />
     <PackageReference Include="FluentEmail.Smtp" Version="3.0.2" />
-    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.8.8.2" />
-    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.8.8.2" />
-    <PackageReference Include="Furion.Pure" Version="4.8.8.2" />
+    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.8.8.3" />
+    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.8.8.3" />
+    <PackageReference Include="Furion.Pure" Version="4.8.8.3" />
     <PackageReference Include="IPTools.China" Version="1.6.0" />
     <PackageReference Include="Lazy.Captcha.Core" Version="2.0.3" />
-    <PackageReference Include="Magicodes.IE.Excel" Version="2.7.4.3" />
-    <PackageReference Include="Magicodes.IE.Pdf" Version="2.7.4.3" />
+    <PackageReference Include="Magicodes.IE.Excel" Version="2.7.4.4" />
+    <PackageReference Include="Magicodes.IE.Pdf" Version="2.7.4.4" />
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.16" />
     <PackageReference Include="NEST" Version="7.17.5" />
     <PackageReference Include="NewLife.Redis" Version="5.3.2023.401" />

+ 7 - 1
Admin.NET/Admin.NET.Core/Enum/CryptogramEnum.cs

@@ -16,5 +16,11 @@ public enum CryptogramEnum
     /// SM2(国密)
     /// </summary>
     [Description("SM2")]
-    SM2 = 1
+    SM2 = 1,
+
+    /// <summary>
+    /// SM4(国密)
+    /// </summary>
+    [Description("SM4")]
+    SM4 = 2
 }

+ 8 - 0
Admin.NET/Admin.NET.Core/Util/CryptogramUtil.cs

@@ -23,6 +23,10 @@ public class CryptogramUtil
         {
             return SM2Encrypt(plainText);
         }
+        else if (CryptoType == CryptogramEnum.SM4.ToString())
+        {
+            return SM4EncryptECB(plainText);
+        }
         return plainText;
     }
 
@@ -37,6 +41,10 @@ public class CryptogramUtil
         {
             return SM2Decrypt(cipherText);
         }
+        else if (CryptoType == CryptogramEnum.SM4.ToString())
+        {
+            return SM4DecryptECB(cipherText);
+        }
         return cipherText;
     }
 

+ 0 - 83
Admin.NET/Admin.NET.Core/Util/SM/ByteUtil.cs

@@ -1,83 +0,0 @@
-using System.Globalization;
-
-namespace Admin.NET.Core;
-
-/// <summary>
-/// 字节数组操作扩展类
-/// </summary>
-public static class ByteUtil
-{
-    internal static byte[] AsciiBytes(string s)
-    {
-        byte[] bytes = new byte[s.Length];
-        for (int i = 0; i < s.Length; i++)
-        {
-            bytes[i] = (byte)s[i];
-        }
-        return bytes;
-    }
-
-    internal static byte[] HexToByteArray(this string hexString)
-    {
-        byte[] bytes = new byte[hexString.Length / 2];
-        for (int i = 0; i < hexString.Length; i += 2)
-        {
-            string s = hexString.Substring(i, 2);
-            bytes[i / 2] = byte.Parse(s, NumberStyles.HexNumber, null);
-        }
-        return bytes;
-    }
-
-    internal static string ByteArrayToHex(this byte[] bytes)
-    {
-        var builder = new StringBuilder(bytes.Length * 2);
-        foreach (byte b in bytes)
-        {
-            builder.Append(b.ToString("X2"));
-        }
-        return builder.ToString();
-    }
-
-    internal static string ByteArrayToHex(this byte[] bytes, int len)
-    {
-        return ByteArrayToHex(bytes).Substring(0, len * 2);
-    }
-
-    internal static byte[] RepeatByte(byte b, int count)
-    {
-        byte[] value = new byte[count];
-        for (int i = 0; i < count; i++)
-        {
-            value[i] = b;
-        }
-        return value;
-    }
-
-    internal static byte[] SubBytes(this byte[] bytes, int startIndex, int length)
-    {
-        byte[] res = new byte[length];
-        Array.Copy(bytes, startIndex, res, 0, length);
-        return res;
-    }
-
-    internal static byte[] XOR(this byte[] value)
-    {
-        byte[] res = new byte[value.Length];
-        for (int i = 0; i < value.Length; i++)
-        {
-            res[i] ^= value[i];
-        }
-        return res;
-    }
-
-    internal static byte[] XOR(this byte[] valueA, byte[] valueB)
-    {
-        int len = valueA.Length;
-        byte[] res = new byte[len];
-        for (int i = 0; i < len; i++)
-        {
-            res[i] = (byte)(valueA[i] ^ valueB[i]);
-        }
-        return res;
-    }
-}

+ 8 - 9
Admin.NET/Admin.NET.Core/Util/SM/SM4.cs

@@ -5,11 +5,10 @@ public class SM4
     public const int SM4_ENCRYPT = 1;
     public const int SM4_DECRYPT = 0;
 
-    /// <summary>
-    /// JS 前后端加密不一样
-    /// </summary>
-    public  bool FOR_JAVASCRIPT = false;
-    private  long GET_ULONG_BE(byte[] b, int i)
+    // JS前后端加密不一样
+    public bool FOR_JAVASCRIPT = false;
+
+    private long GET_ULONG_BE(byte[] b, int i)
     {
         long n = 0;
         if (FOR_JAVASCRIPT)
@@ -23,7 +22,7 @@ public class SM4
         return n;
     }
 
-    private  void PUT_ULONG_BE(long n, byte[] b, int i)
+    private void PUT_ULONG_BE(long n, byte[] b, int i)
     {
         b[i] = (byte)(int)(0xFF & n >> 24);
         b[i + 1] = (byte)(int)(0xFF & n >> 16);
@@ -31,17 +30,17 @@ public class SM4
         b[i + 3] = (byte)(int)(0xFF & n);
     }
 
-    private  long SHL(long x, int n)
+    private long SHL(long x, int n)
     {
         return (x & 0xFFFFFFFF) << n;
     }
 
-    private  long ROTL(long x, int n)
+    private long ROTL(long x, int n)
     {
         return SHL(x, n) | x >> (32 - n);
     }
 
-    private  void SWAP(long[] sk, int i)
+    private void SWAP(long[] sk, int i)
     {
         long t = sk[i];
         sk[i] = sk[(31 - i)];

+ 2 - 1
Admin.NET/Admin.NET.Core/Util/SM/SM4Util.cs

@@ -11,6 +11,7 @@ public class SM4Util
     public string iv = "0000000000000000";
     public bool hexString = false;
     public bool forJavascript = false;
+
     public string Encrypt_ECB(string plainText)
     {
         var ctx = new SM4_Context
@@ -48,7 +49,7 @@ public class SM4Util
         };
 
         var sm4 = new SM4();
-        
+
         sm4.FOR_JAVASCRIPT = forJavascript;
 
         sm4.Sm4_setkey_enc(ctx, keyBytes);