From 6ff0d0a876ee8979156b66d3212a93a69b8f6ee2 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 18 Nov 2024 14:27:53 +0100 Subject: [PATCH] =?UTF-8?q?feat(RSADecryptor):=20Aktualisiert=20f=C3=BCr?= =?UTF-8?q?=20den=20Import=20von=20verschl=C3=BCsseltem=20pem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/IRSADecryptor.cs | 4 +++- DigitalData.Core.Security/RSADecryptor.cs | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs b/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs index 88864e0..e1fb7ee 100644 --- a/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs +++ b/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs @@ -2,7 +2,9 @@ { public interface IRSADecryptor : IRSACryptographer { - public string PrivateKeyPem { init; } + public string PrivateKeyPem { get; init; } + + public string? Password { get; init; } public IRSAEncryptor Encryptor { get; } diff --git a/DigitalData.Core.Security/RSADecryptor.cs b/DigitalData.Core.Security/RSADecryptor.cs index 5ab3fb5..87d60dd 100644 --- a/DigitalData.Core.Security/RSADecryptor.cs +++ b/DigitalData.Core.Security/RSADecryptor.cs @@ -5,10 +5,11 @@ namespace DigitalData.Core.Security { public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer { - public required string PrivateKeyPem - { - init => _rsa.ImportFromPem(value); - } + public required string PrivateKeyPem { get; init; } + + public string? Password { get; init; } + + public bool IsEncrypted => Password is not null; public IRSAEncryptor Encryptor { @@ -22,6 +23,14 @@ namespace DigitalData.Core.Security } } + public RSADecryptor() + { + if (Password is null) + _rsa.ImportFromPem(PrivateKeyPem); + else + _rsa.ImportFromEncryptedPem(PrivateKeyPem, Password.AsSpan()); + } + public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding); public string Decrypt(string data) => _rsa.Decrypt(data.Base64ToByte(), Padding).BytesToString();