예제 #1
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();
        }
예제 #2
0
파일: Program.cs 프로젝트: khanhlive/NDK
        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();
        }
예제 #3
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!");
            }
        }