Пример #1
0
        public async Task Send(string message)
        {
            var decrypted = EncryptorRSA.DecryptText(message, KeyPairs[Context.ConnectionId].PrivateKey);
            var parts     = decrypted.Split("_|_");

            await Clients.All.SendAsync("Receive", parts.First(), parts.Last());
        }
Пример #2
0
        public static void AutoTest(string file)
        {
            StreamReader reader  = new StreamReader(file);
            var          content = reader.ReadToEnd();
            var          origin  = EncryptorRSA.DecryptText(content, GenratedKeys.PrivateKey);

            Console.WriteLine(origin);
        }
Пример #3
0
        private async void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text))
            {
                return;
            }

            await _connection.InvokeAsync("Send", EncryptorRSA.EncryptText($"{textBox2.Text}_|_{textBox1.Text}", _publicKey));

            textBox1.Clear();
        }
Пример #4
0
        public static void GenerateKeys()
        {
            int          keySize   = 4096;
            var          keys      = EncryptorRSA.GenerateKeys(keySize);
            StreamWriter publicKey = new StreamWriter("publickey");

            publicKey.Write(keys.PublicKey);
            StreamWriter privateKey = new StreamWriter("privatekey");

            privateKey.Write(keys.PrivateKey);
            publicKey.Close();
            privateKey.Close();
        }
Пример #5
0
        public async Task RequestPublicKey()
        {
            var keyPair = EncryptorRSA.GenerateKeys(KeySize);

            if (KeyPairs.TryAdd(Context.ConnectionId, keyPair))
            {
                Console.WriteLine($"Generated new key pair for client with ConnectionId '{Context.ConnectionId}:" +
                                  $"\n\tPublic key: {keyPair.PublicKey}" +
                                  $"\n\tPrivate key: {keyPair.PrivateKey}");

                await Clients.Caller.SendAsync("PublicKey", keyPair.PublicKey);
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            EncryptorRSAKeys key   = EncryptorRSA.GenerateKeys(2048);
            string           input = "";

            input = Console.ReadLine();
            string str = EncryptorRSA.EncryptText(input, key.PublicKey);

            Console.WriteLine("encrypt: " + str);
            Console.WriteLine("decrypt: " + EncryptorRSA.DecryptText(str, key.PrivateKey));
            Console.WriteLine(EncryptorRSA.GetMaxDataLength(16384));
            Console.ReadKey();
        }
Пример #7
0
        private static async Task RegenerateKeys()
        {
            while (true)
            {
                await Task.Delay(TimeSpan.FromMinutes(30));

                foreach (var keyValue in KeyPairs)
                {
                    var newPair = EncryptorRSA.GenerateKeys(KeySize);

                    keyValue.Value.PublicKey  = newPair.PublicKey;
                    keyValue.Value.PrivateKey = newPair.PrivateKey;

                    Console.WriteLine($"Generated new key pair for client with ConnectionId '{keyValue.Key}:" +
                                      $"\n\tPublic key: {keyValue.Value.PublicKey}" +
                                      $"\n\tPrivate key: {keyValue.Value.PrivateKey}");

                    await CallerClients.Caller.SendAsync("PublicKey", keyValue.Value.PublicKey);
                }
            }
        }
Пример #8
0
        private static string GenerateRequests(string request, ref int lastTick)
        {
            try
            {
                var operation = (string)JObject.Parse(request)["Operation"];
                switch (operation)
                {
                case Operation.Config:
                    //alloc a new engine
                    ConfigData config = JsonConvert.DeserializeObject <ConfigData>(request);
                    SimulateEngine = new SimulateEngine();
                    SimulateEngine.Init(config.Elevators, config.TaskID);
                    HistoryLog = new EncrypLog
                    {
                        User         = config.User,
                        TaskID       = config.TaskID,
                        ElevatorLogs = new List <ElevatorLog>()
                    };
                    return("Config Success");

                case Operation.GetRequests:
                    RequestPostData reqs  = JsonConvert.DeserializeObject <RequestPostData>(request);
                    int             lower = lastTick;
                    //Receive reqs data, log into the dictionary.
                    int upper = reqs.Tick;
                    if (reqs.Tick == -1)
                    {
                        upper = int.MaxValue;
                    }
                    if (reqs.FinishRequests != null && reqs.FinishRequests.Count != 0)
                    {
                        HistoryLog.ElevatorLogs.AddRange(
                            reqs.FinishRequests.Where(p => p.FinishTime >= lower && p.FinishTime <= upper));
                    }
                    lastTick = reqs.Tick;
                    if (reqs.Tick != -1)
                    {
                        //Use current tick to request data
                        var currentData = SimulateEngine.GetPassengers(reqs.Tick);
                        var retData     = new RequestResponeData
                        {
                            Passengers = currentData.Item2,
                            NextTick   = currentData.Item1
                        };
                        return(JsonConvert.SerializeObject(retData));
                    }
                    else
                    //Need a judge result
                    {
                        //Judge the result
                        HistoryLog.Score = SimulateEngine.GetScore(HistoryLog.ElevatorLogs);
                        //Log the result
                        string plainLog =
                            JsonConvert.SerializeObject(SimulateEngine.GetEncryScore(HistoryLog.Score,
                                                                                     HistoryLog.User));
                        //Encryp the log
                        var encryScore = EncryptorRSA.EncryptText(plainLog, GenratedKeys.PublicKey);
                        //log to the file
                        StreamWriter writer = new StreamWriter($"test-{HistoryLog.TaskID}.log");
                        writer.WriteLine(encryScore);
                        writer.Close();
                        return(JsonConvert.SerializeObject(HistoryLog.Score));
                    }

                default:
                    return("Wrong Parameters, Please check you json data.");
                }
            }
            catch (OwnExcepetion e)
            {
                return(e.Message);
            }
            catch (Exception e)
            {
                return("Bad Arguments!");
            }
        }