implemented passwort encryption for new entries and decryption to show the password
This commit is contained in:
parent
a0da2394b0
commit
d29e6b8767
@ -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
|
||||
}
|
||||
|
||||
|
||||
68
Project.Persistence/Crypto.cs
Normal file
68
Project.Persistence/Crypto.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,7 +25,7 @@
|
||||
VerticalAlignment="Bottom" />
|
||||
|
||||
<Border Background="RosyBrown" Grid.Column="1">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<StackPanel Orientation="Horizontal" x:Name="ShowEntryDetails">
|
||||
|
||||
<StackPanel Margin="20,20,100,20" Orientation="Vertical">
|
||||
<TextBlock
|
||||
|
||||
@ -3,6 +3,7 @@ using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Project.Controller;
|
||||
using Project.Model;
|
||||
using Project.Persistence;
|
||||
|
||||
namespace Project.View;
|
||||
@ -46,15 +47,51 @@ public partial class MenuView : Window
|
||||
Padding = new Thickness(10),
|
||||
Width = 200
|
||||
};
|
||||
// Add click handler for the button
|
||||
//button.Click += (sender, args) => 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)
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user