Преглед изворни кода

!681 解决后端SM4加密结果与前端JavaScript不一致问题
Merge pull request !681 from 程序O/next

zuohuaijun пре 3 година
родитељ
комит
9a7c8777cd
2 измењених фајлова са 28 додато и 7 уклоњено
  1. 18 6
      Admin.NET/Admin.NET.Core/Util/SM/SM4.cs
  2. 10 1
      Admin.NET/Admin.NET.Core/Util/SM/SM4Util.cs

+ 18 - 6
Admin.NET/Admin.NET.Core/Util/SM/SM4.cs

@@ -5,13 +5,25 @@ public class SM4
     public const int SM4_ENCRYPT = 1;
     public const int SM4_DECRYPT = 0;
 
-    private static long GET_ULONG_BE(byte[] b, int i)
+    /// <summary>
+    /// JS 前后端加密不一样
+    /// </summary>
+    public  bool FOR_JAVASCRIPT = false;
+    private  long GET_ULONG_BE(byte[] b, int i)
     {
-        long n = (long)(b[i] & 0xff) << 24 | (long)((b[i + 1] & 0xff) << 16) | (long)((b[i + 2] & 0xff) << 8) | b[i + 3] & 0xff & 0xffffffffL;
+        long n = 0;
+        if (FOR_JAVASCRIPT)
+        {
+            n = (b[i] & 0xff) << 24 | ((b[i + 1] & 0xff) << 16) | ((b[i + 2] & 0xff) << 8) | (b[i + 3] & 0xff) & 0xff;
+        }
+        else
+        {
+            n = (long)(b[i] & 0xff) << 24 | (long)((b[i + 1] & 0xff) << 16) | (long)((b[i + 2] & 0xff) << 8) | b[i + 3] & 0xff & 0xffffffffL;
+        }
         return n;
     }
 
-    private static 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);
@@ -19,17 +31,17 @@ public class SM4
         b[i + 3] = (byte)(int)(0xFF & n);
     }
 
-    private static long SHL(long x, int n)
+    private  long SHL(long x, int n)
     {
         return (x & 0xFFFFFFFF) << n;
     }
 
-    private static long ROTL(long x, int n)
+    private  long ROTL(long x, int n)
     {
         return SHL(x, n) | x >> (32 - n);
     }
 
-    private static void SWAP(long[] sk, int i)
+    private  void SWAP(long[] sk, int i)
     {
         long t = sk[i];
         sk[i] = sk[(31 - i)];

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

@@ -10,7 +10,7 @@ public class SM4Util
     public string secretKey = "1814546261730461"; // 长度必须为16字节
     public string iv = "0000000000000000";
     public bool hexString = false;
-
+    public bool forJavascript = false;
     public string Encrypt_ECB(string plainText)
     {
         var ctx = new SM4_Context
@@ -30,6 +30,8 @@ public class SM4Util
         }
 
         var sm4 = new SM4();
+        sm4.FOR_JAVASCRIPT = forJavascript;
+
         sm4.Sm4_setkey_enc(ctx, keyBytes);
         byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, Encoding.ASCII.GetBytes(plainText));
 
@@ -46,6 +48,9 @@ public class SM4Util
         };
 
         var sm4 = new SM4();
+        
+        sm4.FOR_JAVASCRIPT = forJavascript;
+
         sm4.Sm4_setkey_enc(ctx, keyBytes);
         byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, plainBytes);
         return encrypted;
@@ -72,6 +77,8 @@ public class SM4Util
         }
 
         var sm4 = new SM4();
+        sm4.FOR_JAVASCRIPT = forJavascript;
+
         sm4.Sm4_setkey_dec(ctx, keyBytes);
         byte[] decrypted = sm4.Sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
         return Encoding.ASCII.GetString(decrypted);
@@ -99,6 +106,7 @@ public class SM4Util
         }
 
         var sm4 = new SM4();
+        sm4.FOR_JAVASCRIPT = forJavascript;
         sm4.Sm4_setkey_enc(ctx, keyBytes);
         byte[] encrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Encoding.ASCII.GetBytes(plainText));
 
@@ -128,6 +136,7 @@ public class SM4Util
         }
 
         var sm4 = new SM4();
+        sm4.FOR_JAVASCRIPT = forJavascript;
         sm4.Sm4_setkey_dec(ctx, keyBytes);
         byte[] decrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherText));
         return Encoding.ASCII.GetString(decrypted);