private async void LoadDataClick(object sender, RoutedEventArgs e)
        {
            try
            {
                RSACryptoServiceProvider rsa;
                WorkHistroySmartContract workHistroySmartContract = new WorkHistroySmartContract();

                using (rsa = new RSACryptoServiceProvider(Consts.RSA_KEY_LENGTH))
                {
                    try
                    {
                        RSACryptoServiceProviderExtensions.FromXmlString(rsa, File.ReadAllText(_xmlStringPath));

                        DataSubjectSharer dataSubjectSharer = new DataSubjectSharer();
                        dataSubjectSharer.WorkHistroySmartContract = workHistroySmartContract;
                        dataSubjectSharer.RSA = rsa;

                        workHistroySmartContract.LoadContract(AddressEntry.Text);

                        string decryptedData = await dataSubjectSharer.DecryptDataFromContract();

                        DataOutput.Text = decryptedData;
                    }
                    finally
                    {
                        rsa.PersistKeyInCsp = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemple #2
0
 public byte[] Encrypt(string publicKey, string plain)
 {
     byte[] encrypted;
     using (var rsa = new RSACryptoServiceProvider(2048))
     {
         rsa.PersistKeyInCsp = false;
         RSACryptoServiceProviderExtensions.FromXmlString(rsa, publicKey);
         encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(plain), true);
     }
     return(encrypted);
 }
Exemple #3
0
        public void FromXmlStringImpl_GivenPrivateKeyXmlWithUnknownNode_ThrowsInvalidOperationException()
        {
            const string samplePrivateKeyXml =
                "<RSAKeyValue><pi>unexpected</pi></RSAKeyValue>";

            using (var provider = new RSACryptoServiceProvider())
            {
                var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                          RSACryptoServiceProviderExtensions.FromXmlStringImpl(provider, samplePrivateKeyXml));
                Assert.AreEqual("Unknown node name: pi", exception.Message);
            }
        }
Exemple #4
0
        public void FromXmlStringImpl_GivenInvalidPrivateKeyXml_ThrowsInvalidOperationException()
        {
            const string samplePrivateKeyXml =
                "<something></something>";

            using (var provider = new RSACryptoServiceProvider())
            {
                var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                          RSACryptoServiceProviderExtensions.FromXmlStringImpl(provider, samplePrivateKeyXml));
                Assert.AreEqual("Invalid XML RSA key.", exception.Message);
            }
        }
Exemple #5
0
 public string Decrypt(string privateKey, byte[] encrypted)
 {
     byte[] decrypted;
     using (var rsa = new RSACryptoServiceProvider(2048))
     {
         rsa.PersistKeyInCsp = false;
         RSACryptoServiceProviderExtensions.FromXmlString(rsa, privateKey);
         //rsa.FromXmlString(privateKey);
         decrypted = rsa.Decrypt(encrypted, true);
     }
     return(Encoding.UTF8.GetString(decrypted));
 }
Exemple #6
0
        public static async Task BenchmarkWorkHistoryBundleCreation(Queue <string> inputQue)
        {
            WorkHistroySmartContract workHistroySmartContract = new WorkHistroySmartContract();
            RSACryptoServiceProvider rsa;
            DataSubjectSharer        dataSubjectSharer = new DataSubjectSharer
            {
                WorkHistroySmartContract = workHistroySmartContract,
            };

            using (rsa = new RSACryptoServiceProvider(Consts.RSA_KEY_LENGTH))
            {
                try
                {
                    dataSubjectSharer.RSA = rsa;

                    string xmlString = new StreamReader("privatePaul.xml").ReadToEnd();
                    RSACryptoServiceProviderExtensions.FromXmlString(rsa, xmlString);

                    DataBundle   dataBundle  = new DataBundle();
                    List <Entry> workHistory = await dataSubjectSharer.GetAllWorkHistory();

                    string input;

                    do
                    {
                        input = inputQue.Dequeue();
                        try
                        {
                            int i = Int32.Parse(input) - 1;
                            dataBundle.WorkHistory.Add(workHistory[i]);
                        }
                        catch (Exception e)
                        {
                        }
                    } while (input != "done");

                    while ((input = inputQue.Dequeue()) != "done")
                    {
                        dataBundle.Referees.Add(await Entry.ReadEntry(input));
                    }

                    input = inputQue.Dequeue();

                    Utils.ExportToJsonFile(input, dataBundle);
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }
        }
Exemple #7
0
        private static async Task EmpMenu(string[] args)
        {
            WorkHistroySmartContract workHistroySmartContract = new WorkHistroySmartContract();
            RSACryptoServiceProvider rsa;
            DataSubjectSharer        dataSubjectSharer = new DataSubjectSharer
            {
                WorkHistroySmartContract = workHistroySmartContract,
            };

            using (rsa = new RSACryptoServiceProvider(Consts.RSA_KEY_LENGTH))
            {
                try
                {
                    dataSubjectSharer.RSA = rsa;

                    string xmlString = new StreamReader(args[1]).ReadToEnd();
                    RSACryptoServiceProviderExtensions.FromXmlString(rsa, xmlString);

                    switch (args[0].ToLower())
                    {
                    case "add":
                        dataSubjectSharer.AddAddress(args[2]);
                        dataSubjectSharer.SaveChangesToAddresses();
                        break;

                    case "craete":
                        await CreateWorkHistoryBundle(dataSubjectSharer);

                        break;

                    case "export":
                        await ExportEntry(dataSubjectSharer);

                        break;
                    }
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }
        }
Exemple #8
0
        public void FromXmlStringImpl_GivenPrivateKeyXml_GivesSameResultAsDotNetImplementation()
        {
            const string samplePrivateKeyXml =
                "<RSAKeyValue><Modulus>twJgSXtGu3QQKComA/6wgcTPFS6cky+EHA+fCAZm+Suz0KpiYqvk4LHV+MQQvVy1TpWjpC1iXtEa5BfMS8zDLfrXaXA6RSZ3QEw8YfmmMrKDwUULIORgqcW8Uybalp5fMdbOieAQNXpOLNjnjPZVmFrQvB+CzfltYo82aEiOTjk=</Modulus><Exponent>AQAB</Exponent><P>8x4Omo3kOOExZP/XbtWLHlW7WfEtJNXIATzYlpOQAM1+mwJ7qBAP2umzudUdfXJECMKyv1e+eVeb0WatIsj+vw==</P><Q>wLTwSuM+KG57O4VTddyBSXRHLJvahfWlB1VettJvcqgQk2zK4XwoZU7POjq5fx6kfAUyAYaaxHfwKhKBIy1pBw==</Q><DP>F3LRs8R1u6q0qeonLDB6f42DSXSChyf7Z2sn9LX80KcBTBAcPyR1cwbRZ94PPxczSqkEtoHPBEMX60V883rxXw==</DP><DQ>UQ/LxLSygO94hyEeaoXHHM784Zbt5Uvfj6YpoV4D44cu8dThwtgnZfYw1Z2+Serp5gGJd3rXv610KT5/c/y2IQ==</DQ><InverseQ>jV3wG0+jRpbnkpYLBMVFmLlhJ68oZnpI+fbVnm5mBMr3Rzytz2HfgaGpmI6MY+ni9JV0pfntKNT6uo/Jji34gQ==</InverseQ><D>D4MZDEFxvmPZFr5z2HTXGzjGYMJBrUwiw4ojbbe1NLuakz5N9pUhYlZQj7R2wsY/6/hNFZZvNyA8SkcmHuqtRGyEmE9JOzRA5YhxkC6rfy9oTR2ybIrv9mUGU7P76PBPO2VQJdIIgAdTXMIz8o3IOStINpEkGWzptQ1yxZ8Apx0=</D></RSAKeyValue>";

            using (var customBasedProvider = new RSACryptoServiceProvider())
                using (var dotnetBasedProvider = new RSACryptoServiceProvider())
                {
                    RSACryptoServiceProviderExtensions.FromXmlStringImpl(customBasedProvider, samplePrivateKeyXml);
                    dotnetBasedProvider.FromXmlString(samplePrivateKeyXml);

                    var dotnetBasedParameters = customBasedProvider.ExportParameters(true);
                    var customBasedParameters = customBasedProvider.ExportParameters(true);

                    Assert.AreEqual(dotnetBasedParameters.D, customBasedParameters.D);
                    Assert.AreEqual(dotnetBasedParameters.DP, customBasedParameters.DP);
                    Assert.AreEqual(dotnetBasedParameters.DQ, customBasedParameters.DQ);
                    Assert.AreEqual(dotnetBasedParameters.Exponent, customBasedParameters.Exponent);
                    Assert.AreEqual(dotnetBasedParameters.InverseQ, customBasedParameters.InverseQ);
                    Assert.AreEqual(dotnetBasedParameters.Modulus, customBasedParameters.Modulus);
                    Assert.AreEqual(dotnetBasedParameters.P, customBasedParameters.P);
                    Assert.AreEqual(dotnetBasedParameters.Q, customBasedParameters.Q);
                }
        }
Exemple #9
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, MimirContext db)
        {
            if (env.IsDevelopment())
            {
                log.Info("LittleC is a potato!");
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/error");
            }

            // Load logic configs.
            log.Info("Loading logic configs.");
            try
            {
                Program.ServerName = (from o in db.Options where o.Option == "ServerName" select o.Value).First();
                if ((from o in db.Options where o.Option == "PrivateKeyXml" select o.Value).First() == string.Empty)
                {
                    SignatureWorker.GenKey(db);
                }
                RSACryptoServiceProviderExtensions.FromXmlString(Program.PrivateKeyProvider,
                                                                 (from o in db.Options where o.Option == "PrivateKeyXml" select o.Value).First());
                Program.PublicKey    = (from o in db.Options where o.Option == "PublicKey" select o.Value).First();
                Program.ServerDomain = (from o in db.Options where o.Option == "ServerDomain" select o.Value).First();
                int.TryParse((from o in db.Options where o.Option == "SecurityLoginTryTimes" select o.Value).First(),
                             out Program.SecurityLoginTryTimes);
                bool.TryParse((from o in db.Options where o.Option == "IsEnableMultiProfiles" select o.Value).First(),
                              out Program.IsEnableMultiProfiles);
                int.TryParse((from o in db.Options where o.Option == "MaxTokensPerProfile" select o.Value).First(),
                             out Program.MaxTokensPerProfile);
                int.TryParse((from o in db.Options where o.Option == "TokensExpireDaysLimit" select o.Value).First(),
                             out Program.TokensExpireDaysLimit);
                long.TryParse((from o in db.Options where o.Option == "SessionsExpireSeconds" select o.Value).First(),
                              out Program.SessionsExpireSeconds);
                Program.SkinDomains = (from o in db.Options where o.Option == "SkinDomains" select o.Value).First().Split(",");
                int.TryParse((from o in db.Options where o.Option == "MaxProfileCountPerQuery" select o.Value).First(),
                             out Program.MaxProfileCountPerQuery);
                bool.TryParse((from o in db.Options where o.Option == "IsEnableLandingPage" select o.Value).First(),
                              out Program.IsEnableLandingPage);
                bool.TryParse((from o in db.Options where o.Option == "IsEnableSmtp" select o.Value).First(),
                              out Program.IsEnableSmtp);
                Program.SmtpDomain = (from o in db.Options where o.Option == "SmtpDomain" select o.Value).First();
                int.TryParse((from o in db.Options where o.Option == "SmtpPort" select o.Value).First(),
                             out Program.SmtpPort);
                Program.SmtpEmail    = (from o in db.Options where o.Option == "SmtpEmail" select o.Value).First();
                Program.SmtpName     = (from o in db.Options where o.Option == "SmtpName" select o.Value).First();
                Program.SmtpPassword = (from o in db.Options where o.Option == "SmtpPassword" select o.Value).First();
                bool.TryParse((from o in db.Options where o.Option == "SmtpIsSsl" select o.Value).First(),
                              out Program.SmtpIsSsl);
                int.TryParse((from o in db.Options where o.Option == "MaxProfileCountPerUser" select o.Value).First(),
                             out Program.MaxProfileCountPerUser);
                bool.TryParse((from o in db.Options where o.Option == "IsHttps" select o.Value).First(),
                              out Program.IsHttps);
            }
            catch (Exception)
            {
                log.Fatal("Bad database.");
                throw;
            }
            log.Info("Logic configs loaded.");

            app.UseSession();

            //app.UseHttpsRedirection();
            app.UseStaticFiles(new StaticFileOptions()
            {
                ServeUnknownFileTypes = true,
                DefaultContentType    = "image/png"
            });

            app.Use(next =>
            {
                return(async context =>
                {
                    context.Response.OnStarting(() =>
                    {
                        // Add ALI.
                        if (Program.IsHttps)
                        {
                            context.Response.Headers.Add("X-Authlib-Injector-API-Location", "https://" + Program.ServerDomain + "/api/");
                        }
                        else
                        {
                            context.Response.Headers.Add("X-Authlib-Injector-API-Location", "http://" + Program.ServerDomain + "/api/");
                        }

                        context.Response.Headers["Server"] = "Mimir";
                        context.Response.Headers.Add("Author", "Romonov");
                        return Task.CompletedTask;
                    });
                    await next(context);
                });
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "yggdrasil_sessionserver_get_profile",
                    template: "api/sessionserver/session/minecraft/profile/{uuid}",
                    defaults: new { controller = "SessionServer", action = "Profile" });
                routes.MapRoute(
                    name: "yggdrasil_sessionserver_join",
                    template: "api/sessionserver/session/minecraft/join",
                    defaults: new { controller = "SessionServer", action = "Join" });
                routes.MapRoute(
                    name: "yggdrasil_sessionserver_has_joined",
                    template: "api/sessionserver/session/minecraft/hasJoined",
                    defaults: new { controller = "SessionServer", action = "HasJoined" });
                routes.MapRoute(
                    name: "yggdrasil_api_profiles_query",
                    template: "api/api/profiles/minecraft",
                    defaults: new { controller = "Api", action = "Profiles" });
                routes.MapRoute(
                    name: "yggdrasil_authserver",
                    template: "api/authserver/{action}",
                    defaults: new { controller = "AuthServer" });
                routes.MapRoute(
                    name: "yggdrasil_index",
                    template: "api",
                    defaults: new { controller = "Api", action = "Index" });
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Index}/{action=Index}/{id?}");
            });
        }