Skip to content

Commit

Permalink
feat(Libraries.ConfigurationManager): Kryptographiedienst zum verschl…
Browse files Browse the repository at this point in the history
…üsseln und entschlüsseln mit Passwort hinzugefügt
  • Loading branch information
JKamue committed Aug 16, 2024
1 parent ae1c0a3 commit 8d9dbe8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AesCryptographyService()
aes = Aes.Create();
}

private AesCryptographyService(byte[] key, byte[] iv)
internal AesCryptographyService(byte[] key, byte[] iv)
{
aes = Aes.Create();
aes.Key = key;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Security.Cryptography;
using CSharpFunctionalExtensions;

namespace coIT.Libraries.ConfigurationManager.Cryptography
{
public class PasswordCryptographyService : IDoCryptography
{
private readonly AesCryptographyService _crypgraphyService;

public PasswordCryptographyService(string password)
{
var keyAndIv = PasswordToKeyAndIV(password);
_crypgraphyService = new AesCryptographyService(keyAndIv.Key, keyAndIv.IV);
}

private static (byte[] Key, byte[] IV) PasswordToKeyAndIV(string password)
{
// Da die Passwörter in Bitwarden liegen und bereits extrem lang sind
// und dieser Code nie großflächig für Invididuelle Passwörter eingesetzt
// werden sollte, kann man entgegen Best Practices hier ohne Salt arbeiten
byte[] salt = [00];
var schlüsselableiter = new Rfc2898DeriveBytes(
password,
salt,
1000,
HashAlgorithmName.SHA512
);

var key = schlüsselableiter.GetBytes(32);
var iv = schlüsselableiter.GetBytes(16);

return (key, iv);
}

public Result<string> Decrypt(string encryptedText)
{
return _crypgraphyService.Decrypt(encryptedText);
}

public Result<string> Encrypt(string plainText)
{
return _crypgraphyService.Encrypt(plainText);
}
}
}

0 comments on commit 8d9dbe8

Please sign in to comment.