ntminer 5 years ago
parent
commit
134feb181b

+ 2 - 2
src/AppModels/MinerStudio/Vms/MinerClientViewModel.cs

@@ -598,11 +598,11 @@ namespace NTMiner.MinerStudio.Vms {
                 if (string.IsNullOrEmpty(_data.WindowsPassword)) {
                     return string.Empty;
                 }
-                return HashUtil.TextDecrypt(Convert.FromBase64String(_data.WindowsPassword), RpcRoot.RpcUser.Password);
+                return Cryptography.QuickUtil.TextDecrypt(Convert.FromBase64String(_data.WindowsPassword), RpcRoot.RpcUser.Password);
             }
             set {
                 if (!string.IsNullOrEmpty(value)) {
-                    value = Convert.ToBase64String(HashUtil.TextEncrypt(value, RpcRoot.RpcUser.Password));
+                    value = Convert.ToBase64String(Cryptography.QuickUtil.TextEncrypt(value, RpcRoot.RpcUser.Password));
                 }
                 if (_data.WindowsPassword != value) {
                     var old = _data.WindowsPassword;

+ 3 - 3
src/NTMinerDaemon/NTMinerDaemon.csproj

@@ -97,12 +97,12 @@
     <Compile Include="..\ntminerlib\cryptography\BigInteger.cs">
       <Link>Cryptography\BigInteger.cs</Link>
     </Compile>
-    <Compile Include="..\ntminerlib\cryptography\RSAHelper.cs">
-      <Link>Cryptography\RSAHelper.cs</Link>
-    </Compile>
     <Compile Include="..\ntminerlib\cryptography\RSAKey.cs">
       <Link>Cryptography\RSAKey.cs</Link>
     </Compile>
+    <Compile Include="..\NTMinerlib\Cryptography\RSAUtil.cs">
+      <Link>Cryptography\RSAUtil.cs</Link>
+    </Compile>
     <Compile Include="..\NTMinerlib\DataExtensions.cs">
       <Link>DataExtensions.cs</Link>
     </Compile>

+ 1 - 1
src/NTMinerDaemon/Ws/AbstractWsClient.cs

@@ -247,7 +247,7 @@ namespace NTMiner.Ws {
                         case NTMinerAppType.MinerClient:
                             if (message.Type == WsMessage.UpdateAESPassword) {
                                 if (message.TryGetData(out AESPassword aesPassword)) {
-                                    aesPassword.Password = Cryptography.RSAHelper.DecryptString(aesPassword.Password, aesPassword.PublicKey);
+                                    aesPassword.Password = Cryptography.RSAUtil.DecryptString(aesPassword.Password, aesPassword.PublicKey);
                                     _aesPassword = aesPassword;
                                 }
                                 return;

+ 28 - 0
src/NTMinerDataSchemas/Base64Util.cs

@@ -0,0 +1,28 @@
+using System.Linq;
+
+namespace NTMiner {
+    public static class Base64Util {
+        private static char[] _base64CodeArray = new char[] {
+            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+            '0', '1', '2', '3', '4',  '5', '6', '7', '8', '9', '+', '/'// 刚好64个
+        };
+
+        public static bool IsBase64OrEmpty(string base64Str) {
+            if (string.IsNullOrEmpty(base64Str)) {
+                return true;
+            }
+            else if (base64Str.Length % 4 != 0) {
+                return false;
+            }
+            else if (base64Str.Any(c => !_base64CodeArray.Contains(c))) {
+                return false;
+            }
+            return true;
+        }
+
+        public static bool IsBase64Char(char c) {
+            return _base64CodeArray.Contains(c);
+        }
+    }
+}

+ 1 - 1
src/NTMinerDataSchemas/Core/MinerServer/ClientData.cs

@@ -594,7 +594,7 @@ namespace NTMiner.Core.MinerServer {
                 return _windowsPassword;
             }
             set {
-                if (!HashUtil.IsBase64OrEmpty(value)) {
+                if (!Base64Util.IsBase64OrEmpty(value)) {
                     value = string.Empty;
                 }
                 _windowsPassword = value;

+ 13 - 1
src/NTMinerDataSchemas/Core/MinerServer/MinerData.cs

@@ -87,7 +87,19 @@ namespace NTMiner.Core.MinerServer {
         public string MACAddress { get; set; }
         public string WindowsLoginName { get; set; }
 
-        public string WindowsPassword { get; set; }
+        private string _windowsPassword;
+        public string WindowsPassword {
+            get {
+                return _windowsPassword;
+            }
+            set {
+                if (!Base64Util.IsBase64OrEmpty(value)) {
+                    value = string.Empty;
+                }
+                _windowsPassword = value;
+            }
+        }
+
         public DateTime CreatedOn { get; set; }
         public Guid GroupId { get; set; }
     }

+ 0 - 45
src/NTMinerDataSchemas/HashUtil.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Linq;
 using System.Security.Cryptography;
 using System.Text;
 
@@ -23,49 +22,5 @@ namespace NTMiner {
             }
             return Sha1(Encoding.UTF8.GetBytes(text));
         }
-
-        public static bool IsBase64OrEmpty(string base64Str) {
-            if (string.IsNullOrEmpty(base64Str)) {
-                return true;
-            }
-            else if (base64Str.Length % 4 != 0) {
-                return false;
-            }
-            else if (base64Str.Any(c => !_base64CodeArray.Contains(c))) {
-                return false;
-            }
-            return true;
-        }
-
-
-        private static char[] _base64CodeArray = new char[]
-        {
-            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-            '0', '1', '2', '3', '4',  '5', '6', '7', '8', '9', '+', '/', '='
-        };
-
-        // 矿机上填写的Windows远程桌面的密码是加密返回的,先前的加解密方法引入了一个BUG因为
-        // 加密结果有换行等特殊符号从而导致服务器端json序列化时CPU和内存使用率奇高,现已修复。
-        public static byte[] TextEncrypt(string content, string secretKey) {
-            byte[] data = Encoding.UTF8.GetBytes(content);
-            byte[] key = Encoding.UTF8.GetBytes(secretKey);
-
-            for (int i = 0; i < data.Length; i++) {
-                data[i] ^= key[i % key.Length];
-            }
-
-            return data;
-        }
-
-        public static string TextDecrypt(byte[] data, string secretKey) {
-            byte[] key = Encoding.UTF8.GetBytes(secretKey);
-
-            for (int i = 0; i < data.Length; i++) {
-                data[i] ^= key[i % key.Length];
-            }
-
-            return Encoding.UTF8.GetString(data, 0, data.Length);
-        }
     }
 }

+ 1 - 0
src/NTMinerDataSchemas/NTMinerDataSchemas.csproj

@@ -37,6 +37,7 @@
     <Reference Include="System" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Base64Util.cs" />
     <Compile Include="Core\CaptchaData.cs" />
     <Compile Include="Core\ICaptcha.cs" />
     <Compile Include="Core\IOperationResult.cs" />

+ 1 - 2
src/NTMinerlib/Cryptography/AESHelper.partials.cs → src/NTMinerlib/Cryptography/AESUtil.cs

@@ -1,8 +1,7 @@
 namespace NTMiner.Cryptography {
-    public static partial class AESHelper {
+    public static partial class AESUtil {
         public static string GetRandomPassword() {
             return VirtualRoot.GetRandomString(16);
         }
-
     }
 }

+ 28 - 0
src/NTMinerlib/Cryptography/QuickUtil.cs

@@ -0,0 +1,28 @@
+using System.Text;
+
+namespace NTMiner.Cryptography {
+    public static class QuickUtil {
+        // 矿机上填写的Windows远程桌面的密码是加密返回的,先前的加解密方法引入了一个BUG因为
+        // 加密结果有换行等特殊符号从而导致服务器端json序列化时CPU和内存使用率奇高,现已修复。
+        public static byte[] TextEncrypt(string content, string secretKey) {
+            byte[] data = Encoding.UTF8.GetBytes(content);
+            byte[] key = Encoding.UTF8.GetBytes(secretKey);
+
+            for (int i = 0; i < data.Length; i++) {
+                data[i] ^= key[i % key.Length];
+            }
+
+            return data;
+        }
+
+        public static string TextDecrypt(byte[] data, string secretKey) {
+            byte[] key = Encoding.UTF8.GetBytes(secretKey);
+
+            for (int i = 0; i < data.Length; i++) {
+                data[i] ^= key[i % key.Length];
+            }
+
+            return Encoding.UTF8.GetString(data, 0, data.Length);
+        }
+    }
+}

+ 1 - 1
src/NTMinerlib/Cryptography/RSAHelper.cs → src/NTMinerlib/Cryptography/RSAUtil.cs

@@ -10,7 +10,7 @@ namespace NTMiner.Cryptography {
     /// 反正公钥加密 私匙来解密
     /// 需要BigInteger类来辅助
     /// </summary>
-    public static class RSAHelper {
+    public static class RSAUtil {
         /// <summary>
         /// RSA的容器 可以解密的源字符串长度为 DwKeySize/8-11 
         /// 增量为 8 位支持 384 位至 16384 位的密钥大小

+ 3 - 2
src/NTMinerlib/NTMinerlib.csproj

@@ -43,15 +43,16 @@
   <ItemGroup>
     <Compile Include="AppSetting\LocalAppSettingSet.cs" />
     <Compile Include="AppSetting\IAppSettingSet.cs" />
-    <Compile Include="Cryptography\AESHelper.partials.cs" />
+    <Compile Include="Cryptography\AESUtil.cs" />
     <Compile Include="Cryptography\BigInteger.cs" />
+    <Compile Include="Cryptography\QuickUtil.cs" />
     <Compile Include="Cryptography\RSAKey.cs" />
     <Compile Include="Net\Hosts.cs" />
     <Compile Include="NTKeyword.cs" />
     <Compile Include="NTMinerRegistry.partials.cs" />
     <Compile Include="Net\IpUtil.cs" />
     <Compile Include="Repositories\JsonReadOnlyRepository`1.cs" />
-    <Compile Include="Cryptography\RSAHelper.cs" />
+    <Compile Include="Cryptography\RSAUtil.cs" />
     <Compile Include="StringExtensions.cs" />
     <Compile Include="NTMinerRegistry.cs" />
     <Compile Include="Repositories\IJsonReadOnlyRepository.cs" />

+ 18 - 0
src/UnitTests/Base64StringTests.cs

@@ -0,0 +1,18 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Linq;
+using System.Text;
+
+namespace NTMiner {
+    [TestClass]
+    public class Base64StringTests {
+        [TestMethod]
+        public void Test1() {
+            for (int i = 0; i < 10000; i++) {
+                var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
+                Assert.IsTrue(Base64Util.IsBase64OrEmpty(base64String));
+                Assert.IsTrue(base64String.All(a => Base64Util.IsBase64Char(a)));
+            }
+        }
+    }
+}

+ 7 - 7
src/UnitTests/CryptographyTests.cs

@@ -10,20 +10,20 @@ namespace NTMiner {
     public class CryptographyTests {
         [TestMethod]
         public void Test1() {
-            var key = RSAHelper.GetRASKey();
+            var key = RSAUtil.GetRASKey();
             Console.WriteLine(key.PublicKey);
             Console.WriteLine(key.PrivateKey);
             string text = Guid.NewGuid().ToString();
-            Assert.AreEqual(text, RSAHelper.DecryptString(RSAHelper.EncryptString(text, key.PrivateKey), key.PublicKey));
+            Assert.AreEqual(text, RSAUtil.DecryptString(RSAUtil.EncryptString(text, key.PrivateKey), key.PublicKey));
             text = new string(Enumerable.Repeat('a', 40).ToArray());
             Assert.AreEqual(40, text.Length);
-            Assert.AreEqual(text, RSAHelper.DecryptString(RSAHelper.EncryptString(text, key.PrivateKey), key.PublicKey));
+            Assert.AreEqual(text, RSAUtil.DecryptString(RSAUtil.EncryptString(text, key.PrivateKey), key.PublicKey));
             text = new string(Enumerable.Repeat('a', 20).ToArray());
             Assert.AreEqual(20, text.Length);
-            Assert.AreEqual(text, RSAHelper.DecryptString(RSAHelper.EncryptString(text, key.PrivateKey), key.PublicKey));
+            Assert.AreEqual(text, RSAUtil.DecryptString(RSAUtil.EncryptString(text, key.PrivateKey), key.PublicKey));
             text = new string(Enumerable.Repeat('啊', 20).ToArray());
             Assert.AreEqual(20, text.Length);
-            Assert.AreEqual(text, RSAHelper.DecryptString(RSAHelper.EncryptString(text, key.PrivateKey), key.PublicKey));
+            Assert.AreEqual(text, RSAUtil.DecryptString(RSAUtil.EncryptString(text, key.PrivateKey), key.PublicKey));
         }
 
         // 注意RSA的性能很慢,只能用于加密AES密码,然后大规模加密使用AES
@@ -34,10 +34,10 @@ namespace NTMiner {
             for (int i = 0; i < n; i++) {
                 messages.Add(Guid.NewGuid().ToString());
             }
-            var key = RSAHelper.GetRASKey();
+            var key = RSAUtil.GetRASKey();
             NTStopwatch.Start();
             foreach (var message in messages) {
-                RSAHelper.EncryptString(message, key.PrivateKey);
+                RSAUtil.EncryptString(message, key.PrivateKey);
             }
             var elapsedMilliseconds = NTStopwatch.Stop();
             Console.WriteLine(elapsedMilliseconds);

+ 2 - 2
src/UnitTests/UnitTest1.cs

@@ -136,9 +136,9 @@ namespace NTMiner {
         public void EncDecTest() {
             for (int i = 0; i < 100; i++) {
                 string input = Guid.NewGuid().ToString();
-                string secText = Convert.ToBase64String(HashUtil.TextEncrypt(input, "this is a test"));
+                string secText = Convert.ToBase64String(Cryptography.QuickUtil.TextEncrypt(input, "this is a test"));
                 Console.WriteLine(secText);
-                Assert.AreEqual(input, HashUtil.TextDecrypt(Convert.FromBase64String(secText), "this is a test"));
+                Assert.AreEqual(input, Cryptography.QuickUtil.TextDecrypt(Convert.FromBase64String(secText), "this is a test"));
             }
         }
 

+ 1 - 0
src/UnitTests/UnitTests.csproj

@@ -78,6 +78,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AppTypeTests.cs" />
+    <Compile Include="Base64StringTests.cs" />
     <Compile Include="ConvertTests.cs" />
     <Compile Include="CpuTests.cs" />
     <Compile Include="DependencyObjectTests.cs" />

+ 1 - 1
src/WebApiServer/Controllers/UserController.cs

@@ -226,7 +226,7 @@ namespace NTMiner.Controllers {
 
             // TODO:验证手机验证码,注册时如果填写了手机则验证手机验证码,更新用户信息时如果变动了手机则验证手机验证码。
             // 验证验证码的存在性以及手机和验证码的对应关系的正确性而不是只验证验证码的存在性。
-            var key = Cryptography.RSAHelper.GetRASKey();
+            var key = Cryptography.RSAUtil.GetRASKey();
             UserData userData = request.ToUserData(key.PublicKey, key.PrivateKey);
             WebApiRoot.UserSet.Add(userData);
 

+ 3 - 3
src/WsServer/MinerClientBehavior.cs

@@ -41,7 +41,7 @@ namespace NTMiner {
                 minerSign.LoginName = userData.LoginName;
             }
             if (string.IsNullOrEmpty(userData.PublicKey) || string.IsNullOrEmpty(userData.PrivateKey)) {
-                var key = Cryptography.RSAHelper.GetRASKey();
+                var key = Cryptography.RSAUtil.GetRASKey();
                 userData.PublicKey = key.PublicKey;
                 userData.PrivateKey = key.PrivateKey;
                 WsRoot.UserMqSender.SendUpdateUserRSAKey(userData.LoginName, key);
@@ -49,13 +49,13 @@ namespace NTMiner {
             DateTime now = DateTime.Now;
             if (string.IsNullOrEmpty(minerSign.AESPassword) || minerSign.AESPasswordOn.AddDays(1) < now) {
                 isMinerSignChanged = true;
-                minerSign.AESPassword = Cryptography.AESHelper.GetRandomPassword();
+                minerSign.AESPassword = Cryptography.AESUtil.GetRandomPassword();
                 minerSign.AESPasswordOn = now;
             }
             base.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.UpdateAESPassword) {
                 Data = new AESPassword {
                     PublicKey = userData.PublicKey,
-                    Password = Cryptography.RSAHelper.EncryptString(minerSign.AESPassword, userData.PrivateKey)
+                    Password = Cryptography.RSAUtil.EncryptString(minerSign.AESPassword, userData.PrivateKey)
                 }
             }.SignToJson(minerSign.AESPassword), completed: null);
             if (isMinerSignChanged) {