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); } }
public ActionResult AddPost(string content) { var currentId = User.Identity.GetUserId(); var post = new Post { PostId = Guid.NewGuid().ToString(), Date = DateTime.Now, Author = User.Identity.GetUserName(), Rated = false, Plus = 0, Minus = 0 }; post.OrginId = post.PostId; var account = db.Accounts.Single(x => x.AccountId == currentId); post.AccountId = currentId; var publicKey = account.PublicKey; post = TextEncryption.EncryptionPost(post, content, account); account.Posts.Add(post); db.Accounts.AddOrUpdate(account); db.SaveChanges(); foreach (var friend in account.Friends) { var friendTmp = db.Accounts.Single(x => x.AccountId == friend.FriendId); var friendPublicKey = friendTmp.PublicKey; var postTmp = new Post { PostId = Guid.NewGuid().ToString(), Author = post.Author, Date = post.Date, OrginId = post.OrginId, Minus = post.Minus, Plus = post.Plus, Rated = post.Rated, AccountId = friendTmp.AccountId }; postTmp = TextEncryption.EncryptionPost(postTmp, content, friendTmp); friendTmp.Posts.Add(postTmp); db.Accounts.AddOrUpdate(friendTmp); db.SaveChanges(); } return View("Index"); }
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); } }