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:
parent
6ff0d0a876
commit
eccf2b32ce
@ -3,7 +3,9 @@
|
|||||||
namespace DigitalData.Core.Abstractions.Security
|
namespace DigitalData.Core.Abstractions.Security
|
||||||
{
|
{
|
||||||
public interface IRSACryptographer
|
public interface IRSACryptographer
|
||||||
{
|
{
|
||||||
|
public string Pem { get; init; }
|
||||||
|
|
||||||
public RSAEncryptionPadding Padding { get; init; }
|
public RSAEncryptionPadding Padding { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,8 +2,6 @@
|
|||||||
{
|
{
|
||||||
public interface IRSADecryptor : IRSACryptographer
|
public interface IRSADecryptor : IRSACryptographer
|
||||||
{
|
{
|
||||||
public string PrivateKeyPem { get; init; }
|
|
||||||
|
|
||||||
public string? Password { get; init; }
|
public string? Password { get; init; }
|
||||||
|
|
||||||
public IRSAEncryptor Encryptor { get; }
|
public IRSAEncryptor Encryptor { get; }
|
||||||
|
|||||||
@ -2,8 +2,6 @@
|
|||||||
{
|
{
|
||||||
public interface IRSAEncryptor : IRSACryptographer
|
public interface IRSAEncryptor : IRSACryptographer
|
||||||
{
|
{
|
||||||
public string PublicKeyPem { get; init; }
|
|
||||||
|
|
||||||
public byte[] Encrypt(byte[] data);
|
public byte[] Encrypt(byte[] data);
|
||||||
|
|
||||||
public string Encrypt(string data);
|
public string Encrypt(string data);
|
||||||
|
|||||||
@ -4,7 +4,9 @@ using System.Security.Cryptography;
|
|||||||
namespace DigitalData.Core.Security
|
namespace DigitalData.Core.Security
|
||||||
{
|
{
|
||||||
public class RSACryptographer : IRSACryptographer
|
public class RSACryptographer : IRSACryptographer
|
||||||
{
|
{
|
||||||
|
public required string Pem { get; init; }
|
||||||
|
|
||||||
public required RSAEncryptionPadding Padding { get; init; }
|
public required RSAEncryptionPadding Padding { get; init; }
|
||||||
|
|
||||||
protected readonly RSA _rsa = RSA.Create();
|
protected readonly RSA _rsa = RSA.Create();
|
||||||
|
|||||||
@ -5,8 +5,6 @@ namespace DigitalData.Core.Security
|
|||||||
{
|
{
|
||||||
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
|
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
|
||||||
{
|
{
|
||||||
public required string PrivateKeyPem { get; init; }
|
|
||||||
|
|
||||||
public string? Password { get; init; }
|
public string? Password { get; init; }
|
||||||
|
|
||||||
public bool IsEncrypted => Password is not null;
|
public bool IsEncrypted => Password is not null;
|
||||||
@ -17,7 +15,7 @@ namespace DigitalData.Core.Security
|
|||||||
{
|
{
|
||||||
return new RSAEncryptor()
|
return new RSAEncryptor()
|
||||||
{
|
{
|
||||||
PublicKeyPem = _rsa.ExportRSAPublicKeyPem(),
|
Pem = _rsa.ExportRSAPublicKeyPem(),
|
||||||
Padding = Padding
|
Padding = Padding
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -26,9 +24,9 @@ namespace DigitalData.Core.Security
|
|||||||
public RSADecryptor()
|
public RSADecryptor()
|
||||||
{
|
{
|
||||||
if (Password is null)
|
if (Password is null)
|
||||||
_rsa.ImportFromPem(PrivateKeyPem);
|
_rsa.ImportFromPem(Pem);
|
||||||
else
|
else
|
||||||
_rsa.ImportFromEncryptedPem(PrivateKeyPem, Password.AsSpan());
|
_rsa.ImportFromEncryptedPem(Pem, Password.AsSpan());
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding);
|
public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding);
|
||||||
|
|||||||
@ -5,10 +5,9 @@ namespace DigitalData.Core.Security
|
|||||||
{
|
{
|
||||||
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
||||||
{
|
{
|
||||||
public required string PublicKeyPem
|
public RSAEncryptor()
|
||||||
{
|
{
|
||||||
get => _rsa.ExportRSAPublicKeyPem();
|
_rsa.ImportFromPem(Pem);
|
||||||
init => _rsa.ImportFromPem(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding);
|
public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user