feat(RSADecryptor): Aktualisiert für den Import von verschlüsseltem pem

This commit is contained in:
Developer 02 2024-11-18 14:27:53 +01:00
parent 0bb779b7b6
commit 6ff0d0a876
2 changed files with 16 additions and 5 deletions

View File

@ -2,7 +2,9 @@
{ {
public interface IRSADecryptor : IRSACryptographer public interface IRSADecryptor : IRSACryptographer
{ {
public string PrivateKeyPem { init; } public string PrivateKeyPem { get; init; }
public string? Password { get; init; }
public IRSAEncryptor Encryptor { get; } public IRSAEncryptor Encryptor { get; }

View File

@ -5,10 +5,11 @@ namespace DigitalData.Core.Security
{ {
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
{ {
public required string PrivateKeyPem public required string PrivateKeyPem { get; init; }
{
init => _rsa.ImportFromPem(value); public string? Password { get; init; }
}
public bool IsEncrypted => Password is not null;
public IRSAEncryptor Encryptor 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 byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding);
public string Decrypt(string data) => _rsa.Decrypt(data.Base64ToByte(), Padding).BytesToString(); public string Decrypt(string data) => _rsa.Decrypt(data.Base64ToByte(), Padding).BytesToString();