Beispiel #1
0
        static void MessageReceived(HS_SocketDataWorker sdw, string message)
        {
            //Decrypt
            string       json    = RSAHandler.Decrypt(private_key, message);
            LoginRequest request = LoginRequest.Parse(json);

            Console.WriteLine(json);

            //Check login
            //TODO make this async
            string stringResponse = NetUtil.PostSynchro(Settings.Current.InternalLoginUrl,
                                                        new System.Collections.Generic.Dictionary <string, string>()
            {
                { "code", Settings.Current.InternalApiAccessCode }, { "userid", request.Username },
                { "password", request.Password }, { "key", request.Key }
            });
            ServerResponse response = ServerResponse.Parse(stringResponse);

            Log(stringResponse);

            if (response != null)
            {
                if (response.Result == "success")
                {
                    Log("success sending back");
                    string sec = Convert.ToBase64String(TDESHandler.Encrypt(request.Key, stringResponse));
                    sdw.Send(sec);
                }
                else
                {
                }
            }
        }
Beispiel #2
0
 public static void RSA_UnitTest()
 {
     for (int i = 0; i < 1000000; i++)
     {
         String[] keys       = RSAHandler.GenerateKeyPair(1024);
         String   publickey  = keys[0];
         String   privatekey = keys[1];
         String   data       = GenerateRandomString(78);
         string   cypher     = RSAHandler.Encrypt(publickey, data);
         string   text       = RSAHandler.Decrypt(privatekey, cypher);
         if (data != text)
         {
             Console.WriteLine("\n\nFailed!");
             Console.WriteLine("Data: " + data);
             Console.WriteLine("Text: " + text);
             for (int a = 0; a < data.Length; a++)
             {
                 if (data[a] != text[a])
                 {
                     int x = data[a];
                     int y = text[a];
                     int z = 0;
                 }
             }
             File.WriteAllText("err_dump.txt", data);
             return;
         }
         else
         {
             Console.WriteLine(i + " passed! " + data.Substring(0, (data.Length < 10) ? data.Length : 10));
         }
     }
     Console.WriteLine("Test passed!");
 }
Beispiel #3
0
        private static void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;

                // Complete the connection.
                client.EndConnect(ar);

                Console.WriteLine("Socket connected to {0}",
                                  client.RemoteEndPoint.ToString());

                //HS_Request r = new HS_Request("mtear", "NAMESET");
                //Send(client, r.Json);

                string symkey = GenerateSymKey();
                skey = symkey;
                LoginRequest lr = new LoginRequest("", "", symkey);
                string       e  = RSAHandler.Encrypt(public_key, lr.Json);
                Console.WriteLine("Sending data");
                Send(client, e);

                // Signal that the connection has been made.
                connectDone.Set();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
 public ASyncMainServerConnection(string ip, int port, string licenseID, int slots)
 {
     this.notInDemoMode = true;
     this.RSAService = new RSAHandler();
     this.ClientModule = new ClientSocketInformation(ip, port, true);
     this.ClientModule.connectionChanged += ClientModule_connectionChanged;
     this.ClientModule.packetArrival += OnNewPacket;
     this.licenseID = licenseID;
     this.Slots = Slots;
     monitorThread = new Thread(CheckLoop);
     monitorThread.Start();
 }
Beispiel #5
0
    public static string Read(string filename)
    {
        byte[] buffer;
        if (isCheckZk)
        {
            return("");
        }
        int num = 128;

        byte[] buffer3 = new byte[128];
        try
        {
            FileStream stream = new FileStream(filename, (FileMode)3, (FileAccess)1, (FileShare)1);
            buffer = new byte[stream.Length - num];
            stream.Read(buffer3, 0, num);
            stream.Read(buffer, 0, (int)(stream.Length - num));
            stream.Close();
        }
        catch (Exception exception)
        {
            return(exception.Message);
        }
        string[] strArray = Encoding.GetEncoding("GB2312").GetString(buffer).Split(new char[] { ',' });
        if (strArray.Length == 7)
        {
            string   str2  = strArray[2];
            DateTime time  = DateTime.ParseExact(str2, "yyyy-MM-dd", null);
            string   str3  = strArray[3];
            DateTime time2 = DateTime.ParseExact(str3, "yyyy-MM-dd", null);
            DateTime time3 = DateTime.Now;
            if ((time3 >= time) && (time3 <= time2))
            {
                RSAHandler handler = new RSAHandler();
                RSAPKCS1SignatureDeformatter deformatter = handler.CreateRSADeformatter("<root><Modulus>uOTMyuLJ8IzCD+i+1K7ho5Dqssit6X94Kei58/xs/MEgvCLn0EUUJmNC6pIm+S8166aU201TQ7IW45xl4pCUVqGCtItgXVq20T8kCtO7UzeTwT4ibBHhDBbhZrhZO+zYwfiA7UfKIhf96bO8BDqg45MyQQYpTVTZfifgSgJU7uk=</Modulus><Exponent>AQAB</Exponent></root>");
                byte[] hashData = handler.GetHashData(buffer);
                if (!deformatter.VerifySignature(hashData, buffer3))
                {
                    return("授权文件" + filename + "不正确!");
                }
                sOrgName   = Escape.unescape(strArray[0]);
                sItemName  = Escape.unescape(strArray[1]);
                sWWW       = strArray[5];
                sIP        = strArray[6];
                dStartDate = time;
                dEndDate   = time2;
                isCheckZk  = true;
                return("");
            }
            return("授权文件" + filename + "已过期!");
        }
        return("授权文件" + filename + "非法!");
    }
Beispiel #6
0
    public static void GenFile(string filename, string data)
    {
        string str = data;

        data = str + "," + smethod_0() + "," + smethod_2() + "," + smethod_1();
        RSAHandler handler = new RSAHandler();
        RSAPKCS1SignatureFormatter formatter = handler.CreateRSAFormatter(@"c:\privatekey.xml");

        byte[]     bytes    = Encoding.GetEncoding("GB2312").GetBytes(data);
        byte[]     hashData = handler.GetHashData(bytes);
        byte[]     buffer3  = formatter.CreateSignature(hashData);
        FileStream stream   = new FileStream(filename, (FileMode)2);

        if (stream.CanWrite)
        {
            stream.Write(buffer3, 0, buffer3.Length);
            stream.Write(bytes, 0, bytes.Length);
        }
        stream.Close();
    }
Beispiel #7
0
    void LoginConnectCallback(IAsyncResult ar)
    {
        try
        {
            Socket listener = (Socket)ar.AsyncState;
            listener.EndConnect(ar);

            sym_key = GenerateSymKey();
            LoginRequest lr = new LoginRequest(username, password, sym_key);
            Debug.Log("Json: " + lr.Json);
            string e = RSAHandler.Encrypt(public_key, lr.Json);

            HS_SocketDataWorker sdw = new HS_SocketDataWorker(listener);
            sdw.SetCallback(new HS_SocketDataWorker.HS_PlayerCommandCallback(LoginMessageReceived));
            sdw.Send(e);
            Debug.Log("Sending data: " + e);
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
    }
Beispiel #8
0
 public static string ReadFile(string filename)
 {
     if (!isCheck)
     {
         byte[] buffer;
         if (sVersionNo == "")
         {
             return("");
         }
         sVersionNo = "";
         int    num     = 128;
         byte[] buffer3 = new byte[128];
         try
         {
             FileStream stream = new FileStream(filename, (FileMode)3, (FileAccess)1, (FileShare)1);
             buffer = new byte[stream.Length - num];
             stream.Read(buffer3, 0, num);
             stream.Read(buffer, 0, (int)(stream.Length - num));
             stream.Close();
         }
         catch (Exception exception)
         {
             return(exception.Message);
         }
         string[] strArray = Encoding.GetEncoding("GB2312").GetString(buffer).Split(new char[] { ',' });
         if (strArray.Length != 8)
         {
             return("授权文件" + filename + "非法!");
         }
         string_0 = strArray[4];
         if ((string_0 != "调试用标准版") && (string_0 != "调试用企业版"))
         {
             RSAHandler handler = new RSAHandler();
             RSAPKCS1SignatureDeformatter deformatter = handler.CreateRSADeformatter("<root><Modulus>uOTMyuLJ8IzCD+i+1K7ho5Dqssit6X94Kei58/xs/MEgvCLn0EUUJmNC6pIm+S8166aU201TQ7IW45xl4pCUVqGCtItgXVq20T8kCtO7UzeTwT4ibBHhDBbhZrhZO+zYwfiA7UfKIhf96bO8BDqg45MyQQYpTVTZfifgSgJU7uk=</Modulus><Exponent>AQAB</Exponent></root>");
             byte[] hashData = handler.GetHashData(buffer);
             if (!deformatter.VerifySignature(hashData, buffer3))
             {
                 return("授权文件" + filename + "不正确!");
             }
             if ((string_0 != "买断版") && (((strArray[5] != smethod_0()) || (strArray[6] != smethod_2())) || (strArray[7] != smethod_1())))
             {
                 return("授权文件" + filename + "和当前机器的硬件配置不符!");
             }
             isCheck = true;
         }
         sCustomerName = strArray[0];
         sTel          = strArray[1];
         sWWW          = strArray[2];
         sProjectName  = strArray[3];
         if (string_0 == "标准版")
         {
             sVersionNo = "1";
         }
         if (string_0 == "企业版")
         {
             sVersionNo = "2";
         }
         if (string_0 == "买断版")
         {
             sVersionNo = "3";
         }
         if (string_0 == "调试用标准版")
         {
             sVersionNo = "4";
         }
         if (string_0 == "调试用企业版")
         {
             sVersionNo = "5";
         }
     }
     return("");
 }