Refactor(Core.Security): Getrennte Pem-Eigenschaften für öffentliche und private Schlüssel wurden entfernt.

- Pem-Eigenschaft in der Hauptklasse RSACryptographer erstellt
This commit is contained in:
Developer 02 2024-11-18 14:39:18 +01:00
parent 6ff0d0a876
commit eccf2b32ce
6 changed files with 11 additions and 14 deletions

View File

@ -3,7 +3,9 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSACryptographer
{
{
public string Pem { get; init; }
public RSAEncryptionPadding Padding { get; init; }
}
}

View File

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

View File

@ -2,8 +2,6 @@
{
public interface IRSAEncryptor : IRSACryptographer
{
public string PublicKeyPem { get; init; }
public byte[] Encrypt(byte[] data);
public string Encrypt(string data);

View File

@ -4,7 +4,9 @@ using System.Security.Cryptography;
namespace DigitalData.Core.Security
{
public class RSACryptographer : IRSACryptographer
{
{
public required string Pem { get; init; }
public required RSAEncryptionPadding Padding { get; init; }
protected readonly RSA _rsa = RSA.Create();

View File

@ -5,8 +5,6 @@ namespace DigitalData.Core.Security
{
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
{
public required string PrivateKeyPem { get; init; }
public string? Password { get; init; }
public bool IsEncrypted => Password is not null;
@ -17,7 +15,7 @@ namespace DigitalData.Core.Security
{
return new RSAEncryptor()
{
PublicKeyPem = _rsa.ExportRSAPublicKeyPem(),
Pem = _rsa.ExportRSAPublicKeyPem(),
Padding = Padding
};
}
@ -26,9 +24,9 @@ namespace DigitalData.Core.Security
public RSADecryptor()
{
if (Password is null)
_rsa.ImportFromPem(PrivateKeyPem);
_rsa.ImportFromPem(Pem);
else
_rsa.ImportFromEncryptedPem(PrivateKeyPem, Password.AsSpan());
_rsa.ImportFromEncryptedPem(Pem, Password.AsSpan());
}
public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding);

View File

@ -5,10 +5,9 @@ namespace DigitalData.Core.Security
{
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
{
public required string PublicKeyPem
public RSAEncryptor()
{
get => _rsa.ExportRSAPublicKeyPem();
init => _rsa.ImportFromPem(value);
_rsa.ImportFromPem(Pem);
}
public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding);