Skip to content

Commit

Permalink
Change Vault implementation to use System.Security.Cryptography.Prote…
Browse files Browse the repository at this point in the history
…ctedData
  • Loading branch information
cezarypiatek committed Jul 21, 2022
1 parent a31e2a5 commit 696474f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/ScriptRunner/ScriptRunner.GUI/ScriptRunner.GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<PackageReference Include="CliWrap" Version="3.4.4" />
<PackageReference Include="Projektanker.Icons.Avalonia" Version="4.4.0" />
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="4.4.0" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="6.0.0" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.3.4" />
</ItemGroup>
<ItemGroup>
Expand Down
25 changes: 25 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/ViewModels/EncryptionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace ScriptRunner.GUI.ViewModels;

public static class EncryptionHelper
{
private static byte[] EntropyKey = Encoding.ASCII.GetBytes("80CD0C6D-74D3-4E6D-9E4F-ECA485E69FC7");

public static string Encrypt(string value)
{
byte[] data = Encoding.ASCII.GetBytes(value);
string protectedData = Convert.ToBase64String(ProtectedData.Protect(data, EntropyKey, DataProtectionScope.CurrentUser));
return protectedData;
}

public static string Decrypt(string value)
{
byte[] protectedData = Convert.FromBase64String(value);
string data = Encoding.ASCII.GetString(ProtectedData.Unprotect(protectedData, EntropyKey, DataProtectionScope.CurrentUser));
return data;
}
}
25 changes: 15 additions & 10 deletions src/ScriptRunner/ScriptRunner.GUI/ViewModels/VaultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using ReactiveUI;
Expand All @@ -19,20 +17,27 @@ public static IReadOnlyList<VaultEntry> ReadFromVault()
var vaultPath = AppSettingsService.GetSettingsPathFor("Vault.dat");
if (File.Exists(vaultPath))
{
File.Decrypt(vaultPath);
var content = File.ReadAllText(vaultPath);
File.Encrypt(vaultPath);
var data = JsonSerializer.Deserialize<List<VaultEntry>>(content);
return data ?? new List<VaultEntry>();
var contentEncrypted = File.ReadAllText(vaultPath);
try
{
var content = EncryptionHelper.Decrypt(contentEncrypted);
var data = JsonSerializer.Deserialize<List<VaultEntry>>(content);
return data ?? new List<VaultEntry>();
}
catch (Exception e)
{
//TODO: Invalid key
Console.WriteLine(e);
throw;
}
}
return Array.Empty<VaultEntry>();
}

public static void UpdateVault(List<VaultEntry> date)
public static void UpdateVault(List<VaultEntry> data)
{
var vaultPath = AppSettingsService.GetSettingsPathFor("Vault.dat");
File.WriteAllText(vaultPath, JsonSerializer.Serialize(date), Encoding.UTF8);
File.Encrypt(vaultPath);
File.WriteAllText(vaultPath, EncryptionHelper.Encrypt(JsonSerializer.Serialize(data)), Encoding.UTF8);
}
}

Expand Down

0 comments on commit 696474f

Please sign in to comment.