diff --git a/Project.Controller/AppController.cs b/Project.Controller/AppController.cs index 0ebd12f..6d3387d 100644 --- a/Project.Controller/AppController.cs +++ b/Project.Controller/AppController.cs @@ -22,9 +22,9 @@ public partial class AppController //save new entry - hashes the password and gives it to persistence public static void NewEntrySave(string name, string mailusername, string url, string password, string note) { - string hashedPassword = ShaHash.HashPassword(password); + string encrptedPassword = Crypto.EncryptPassword(password); string ownerUsername = _currentUser; - SQLite.SaveEntry(name, hashedPassword, mailusername, url, note, ownerUsername); + SQLite.SaveEntry(name, encrptedPassword, mailusername, url, note, ownerUsername); //string name, string pass, string mailUsername, url, string? note, string ownerUsername } diff --git a/Project.Persistence/Crypto.cs b/Project.Persistence/Crypto.cs new file mode 100644 index 0000000..bc3d206 --- /dev/null +++ b/Project.Persistence/Crypto.cs @@ -0,0 +1,68 @@ +using System.Security.Cryptography; +using System.Text; +namespace Project.Persistence; + +public class Crypto +{ + private static readonly string _key = "myKeyIsSecureAsFuck"; + + public static string EncryptPassword(string password) + { + using (Aes aes = Aes.Create()) + { + byte[] key = DeriveKey(_key); + aes.Key = key; + aes.GenerateIV(); // generate new IV + + ICryptoTransform encryptor = aes.CreateEncryptor(); + + using (MemoryStream msEncrypt = new MemoryStream()) + { + // save IV + msEncrypt.Write(aes.IV, 0, aes.IV.Length); + + using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) + using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) + { + swEncrypt.Write(password); + } + + return Convert.ToBase64String(msEncrypt.ToArray()); + } + } + } + + public static string DecryptPassword(string encryptedPassword) + { + byte[] fullCipher = Convert.FromBase64String(encryptedPassword); + + using (Aes aes = Aes.Create()) + { + byte[] key = DeriveKey(_key); + aes.Key = key; + + // extract IV from encryption + byte[] iv = new byte[aes.BlockSize / 8]; + Array.Copy(fullCipher, 0, iv, 0, iv.Length); + aes.IV = iv; + + ICryptoTransform decryptor = aes.CreateDecryptor(); + + using (MemoryStream msDecrypt = new MemoryStream(fullCipher, iv.Length, fullCipher.Length - iv.Length)) + using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) + using (StreamReader srDecrypt = new StreamReader(csDecrypt)) + { + return srDecrypt.ReadToEnd(); + } + } + } + + private static byte[] DeriveKey(string key) + { + // 256 bits for AES-256 + using (var deriveBytes = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }, 1000)) + { + return deriveBytes.GetBytes(32); + } + } +} \ No newline at end of file diff --git a/Project.View/MenuView/MenuView.axaml b/Project.View/MenuView/MenuView.axaml index 1cacaf5..a7b5ef5 100644 --- a/Project.View/MenuView/MenuView.axaml +++ b/Project.View/MenuView/MenuView.axaml @@ -25,7 +25,7 @@ VerticalAlignment="Bottom" /> - + ShowEntryDetails(entry); + // add click handler for the button + button.Click += (object? sender, Avalonia.Interactivity.RoutedEventArgs e) => EntryDetails(entry); // Add the button to your container entriesContainer.Children.Add(button); } } + //refreshing the buttons when saving a new entry public void AfterSavingNewEntry() { - CreateEntryButtons(); // Refresh the buttons + CreateEntryButtons(); } + public void EntryDetails(SavedEntries entry) + { + ShowEntryDetails.Children.Clear(); + + ShowEntryDetails.Children.Add(new TextBlock + { + Text = $"Name: {entry.Name}", + Margin = new Thickness(0, 0, 0, 5) + }); + ShowEntryDetails.Children.Add(new TextBlock + { + Text = $"Email/Username: {entry.MailUsername}", + Margin = new Thickness(0, 0, 0, 5) + }); + ShowEntryDetails.Children.Add(new TextBlock + { + Text = $"URL: {entry.Url}", + Margin = new Thickness(0, 0, 0, 5) + }); + ShowEntryDetails.Children.Add(new TextBlock + { + Text = $"Password: {Crypto.DecryptPassword(entry.Pass)}", + Margin = new Thickness(0, 0, 0, 5) + }); + + if (!string.IsNullOrEmpty(entry.Note)) + { + ShowEntryDetails.Children.Add(new TextBlock + { + Text = $"Note: {entry.Note}", + Margin = new Thickness(0, 0, 0, 5) + }); + } + + } } \ No newline at end of file