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; - - public void Init() + internal void SetPem(string pem) { - if (_pem is null) + _pem = pem; + Init(); + } + + private void Init() + { + 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