feat: Implementieren der Verschlüsselungs- und Entschlüsselungsdienste mit AES und Integration in die API
- Hinzufügen der `Encryptor`-Klasse für AES-Verschlüsselung und -Entschlüsselung. - Implementierung des `EncryptionController` zur Bereitstellung von Endpunkten für Verschlüsselung, Entschlüsselung und Generierung von Verschlüsselungsparametern. - Erweiterung der DI-Konfiguration mit `AddEncryptor`-Erweiterungsmethode und Integration in `Program.cs`. - Bedingte Registrierung des `EncryptionController` basierend auf der Konfiguration `UseEncryptor`, um sicherzustellen, dass der Controller nur bei Bedarf verfügbar ist. - Implementierung von Lazy Loading für die Verbindungszeichenfolge in `UserManagerDbContext` zur sicheren Handhabung von verschlüsselten Verbindungszeichenfolgen.
This commit is contained in:
78
DigitalData.UserManager.Application/Services/Encryptor.cs
Normal file
78
DigitalData.UserManager.Application/Services/Encryptor.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using DigitalData.UserManager.Application.Services.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace DigitalData.UserManager.Application.Services
|
||||
{
|
||||
public class Encryptor
|
||||
{
|
||||
public const int KeyByteLength = 32;
|
||||
|
||||
private readonly EncryptionParameters _params;
|
||||
|
||||
public Encryptor(IOptions<EncryptionParameters> options)
|
||||
{
|
||||
_params = options.Value;
|
||||
}
|
||||
|
||||
public string Encrypt(string plainText) => Encrypt(plainText, _params.Key, _params.IV);
|
||||
|
||||
public string Decrypt(string cipherText) => Decrypt(cipherText, _params.Key, _params.IV);
|
||||
|
||||
public static string Encrypt(string plainText, string key, string iv)
|
||||
{
|
||||
using Aes aes = Aes.Create();
|
||||
aes.KeySize = KeyByteLength * 8;
|
||||
aes.Key = AdjustKeySize(Encoding.UTF8.GetBytes(key), aes.KeySize / 8);
|
||||
aes.IV = AdjustKeySize(Encoding.UTF8.GetBytes(iv), aes.BlockSize / 8);
|
||||
|
||||
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
|
||||
|
||||
using MemoryStream ms = new();
|
||||
using (CryptoStream cs = new(ms, encryptor, CryptoStreamMode.Write))
|
||||
{
|
||||
using StreamWriter sw = new(cs);
|
||||
sw.Write(plainText);
|
||||
}
|
||||
return Convert.ToBase64String(ms.ToArray());
|
||||
}
|
||||
|
||||
public static string Decrypt(string cipherText, string key, string iv)
|
||||
{
|
||||
using Aes aes = Aes.Create();
|
||||
aes.KeySize = KeyByteLength * 8;
|
||||
aes.Key = AdjustKeySize(Encoding.UTF8.GetBytes(key), aes.KeySize / 8);
|
||||
aes.IV = AdjustKeySize(Encoding.UTF8.GetBytes(iv), aes.BlockSize / 8);
|
||||
|
||||
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
|
||||
|
||||
using MemoryStream ms = new(Convert.FromBase64String(cipherText));
|
||||
using CryptoStream cs = new(ms, decryptor, CryptoStreamMode.Read);
|
||||
using StreamReader sr = new(cs);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
|
||||
public static EncryptionParameters GenerateParameters()
|
||||
{
|
||||
using Aes aes = Aes.Create();
|
||||
aes.KeySize = KeyByteLength * 8;
|
||||
aes.GenerateKey();
|
||||
aes.GenerateIV();
|
||||
return new EncryptionParameters
|
||||
{
|
||||
Key = Convert.ToBase64String(aes.Key),
|
||||
IV = Convert.ToBase64String(aes.IV)
|
||||
};
|
||||
}
|
||||
|
||||
private static byte[] AdjustKeySize(byte[] key, int size)
|
||||
{
|
||||
if (key.Length < size)
|
||||
Array.Resize(ref key, size);
|
||||
else if (key.Length > size)
|
||||
Array.Resize(ref key, size);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace DigitalData.UserManager.Application.Services.Options
|
||||
{
|
||||
public class EncryptionParameters
|
||||
{
|
||||
public required string Key { get; init; }
|
||||
|
||||
public required string IV { get; init; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user