Exemplo n.º 1
0
        public static string DecryptionPost(Post post, Account account)
        {
            try
            {

                //byte[] bytePrivateKey = System.Text.Encoding.Default.GetBytes(privateKey);
                //tworzy klucz AES i macierz Inicjującą
                    //tworzy nowa pare kluczy RSA
                    using (RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider())
                    {
                        // Deszyfrowanie
                        RSAParameters RSAKeyInfo = new RSAParameters();
                        RSAKeyInfo = myRsa.ExportParameters(true);
                        RSAKeyInfo.Modulus = account._Modulus;
                        RSAKeyInfo.D = account._D;
                        RSAKeyInfo.DP = account._DP;
                        RSAKeyInfo.DQ = account._DQ;
                        RSAKeyInfo.Exponent = account._Exponent;
                        RSAKeyInfo.InverseQ = account._InvereQ;
                        RSAKeyInfo.P = account._P;
                        RSAKeyInfo.Q = account._Q;
                        myRsa.ImportParameters(RSAKeyInfo);
                        byte[][] AesKeys = DecryptAesKey(post.Key, RSAKeyInfo);
                        string plaintext = AesDecrypt(post.Content, AesKeys[0], AesKeys[1]);

                        return plaintext;
                    }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Exemplo n.º 2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    var db = new ApplicationDbContext();
                    var account = new Account {AccountId = user.Id};
                    

                    //string privateKey = account.PrivateKey;
                    //XmlDocument xml = new XmlDocument();
                    //xml.LoadXml(privateKey);
                    //string str = "";
                    //XmlNodeList xnList = xml.SelectNodes("/RSAKeyValue");
                    //foreach (XmlNode xn in xnList)
                    //{
                    //    str = xn["Modulus"].InnerText;
                    //}

                    //xml.LoadXml(account.PublicKey);
                    //xnList = xml.SelectNodes("/RSAKeyValue");
                    //foreach (XmlNode xn in xnList)
                    //{
                    //    account.PublicKey = xn["Modulus"].InnerText;
                    //}
                    string str = System.Text.Encoding.Default.GetString(account._D);
                    db.Accounts.Add(account);
                    db.SaveChanges();
                    //account.PrivateKey = String.Empty;                  
                    return RedirectToAction("Index", "Home", new {key=str});

                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 3
0
        public static Post EncryptionPost(Post post, string content, Account account)
        {
            try
            {

                //tworzy klucz AES i macierz Inicjującą
                using (Aes myAes = Aes.Create())
                {
                    //tworzy nowa pare kluczy RSA
                    using (RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider())
                    {
                        //Szyfrujemy text i zapisujemy do bazy
                        byte[] encrypted_data = AesEncrypt(content, myAes.Key, myAes.IV);
                        //Szyfrujemy klucz i zapisujemy do bazy
                        RSAParameters RSAKeyInfo = new RSAParameters();
                        RSAKeyInfo = myRsa.ExportParameters(false);
                        RSAKeyInfo.Modulus = account.PublicKey;
                        RSAKeyInfo.Exponent = account.Exponent;
                        myRsa.ImportParameters(RSAKeyInfo);
                        byte[] encrypted_key = AesKeyEncrypt(myAes.Key, myAes.IV, myRsa.ExportParameters(false));
                        post.Content = encrypted_data;
                        post.Key = encrypted_key;
                        return post;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }