From 58c8520c08d811fbf54e74a9fa3413d2d904a5ac Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 16 Dec 2024 10:05:51 +0100 Subject: [PATCH] refactor(RSADecryptor): Verbesserung der PEM-Initialisierung und Konsistenz MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Die `Pem`-Eigenschaft aktualisiert, sodass während der Initialisierung automatisch `Init()` aufgerufen wird, um eine konsistente Einrichtung sicherzustellen. - Die Methode `SetPem` überarbeitet, um nach dem Setzen des PEM-Werts `Init()` aufzurufen. - Die Methode `Init()` verbessert, um null- oder leere PEM-Werte robuster zu behandeln. - Fehlermeldungen für mehr Klarheit und bessere Debugging-Unterstützung verbessert. - Interne RSA-Initialisierungslogik an die Verarbeitung von Verschlüsselungen angepasst. --- .../Config/AsymCryptParams.cs | 2 -- .../Cryptographer/RSADecryptor.cs | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/DigitalData.Core.Security/Config/AsymCryptParams.cs b/DigitalData.Core.Security/Config/AsymCryptParams.cs index 2108183..c8befa1 100644 --- a/DigitalData.Core.Security/Config/AsymCryptParams.cs +++ b/DigitalData.Core.Security/Config/AsymCryptParams.cs @@ -69,8 +69,6 @@ namespace DigitalData.Core.Security.Config Task.Run(async () => await File.WriteAllTextAsync(path: path, pem)); } } - - decryptor.Init(); } } } diff --git a/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs index aeba8ca..6039926 100644 --- a/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs @@ -8,7 +8,15 @@ namespace DigitalData.Core.Security.Cryptographer { private string? _pem; - public override string Pem { get => _pem ?? throw PemIsNullException; init => _pem = value; } + public override string Pem + { + get => _pem ?? throw PemIsNullException; + init + { + _pem = value; + Init(); + } + } public bool IsPemNull => _pem is null; @@ -31,11 +39,15 @@ namespace DigitalData.Core.Security.Cryptographer public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString(); - internal void SetPem(string pem) => _pem = pem; + internal void SetPem(string pem) + { + _pem = pem; + Init(); + } - public void Init() + private void Init() { - if (_pem is null) + if (string.IsNullOrEmpty(_pem)) throw PemIsNullException; if (IsEncrypted) @@ -44,6 +56,6 @@ namespace DigitalData.Core.Security.Cryptographer RSA.ImportFromPem(Pem); } - private InvalidOperationException PemIsNullException => new($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}."); + private InvalidOperationException PemIsNullException => new($"Pem is null or empty. Issuer: {Issuer}, Audience: {Audience}."); } } \ No newline at end of file