refactor: RSA-Kryptografie-Klassen für bessere Flexibilität und Effizienz überarbeitet
- Konstruktoren zu `RSACryptographer`, `RSADecryptor` und `RSAEncryptor` hinzugefügt, um die Initialisierung zu verbessern. - `PublicKeyPem` in `RSADecryptor` optimiert, um unnötige Objekterstellungen zu vermeiden. - `Verify`-Methode in `RSAEncryptor` korrigiert, um eine korrekte Signaturprüfung zu gewährleisten. - Code-Wiederverwendbarkeit verbessert, indem Base64-Konvertierungslogik zentralisiert wurde.
This commit is contained in:
parent
84dbca97d5
commit
0804ea1418
21
DigitalData.Core.Security/RSACryptographer.cs
Normal file
21
DigitalData.Core.Security/RSACryptographer.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace DigitalData.Core.Security
|
||||
{
|
||||
public class RSACryptographer
|
||||
{
|
||||
internal RSACryptographer() { }
|
||||
|
||||
public required string Pem
|
||||
{
|
||||
init
|
||||
{
|
||||
_rsa.ImportFromPem(value);
|
||||
}
|
||||
}
|
||||
|
||||
public required RSAEncryptionPadding Padding { get; init; }
|
||||
|
||||
protected readonly RSA _rsa = RSA.Create();
|
||||
}
|
||||
}
|
||||
25
DigitalData.Core.Security/RSADecryptor.cs
Normal file
25
DigitalData.Core.Security/RSADecryptor.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace DigitalData.Core.Security
|
||||
{
|
||||
public class RSADecryptor : RSACryptographer
|
||||
{
|
||||
public string PublicKeyPem => _rsa.ExportRSAPublicKeyPem();
|
||||
|
||||
public RSAEncryptor Encryptor
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ()
|
||||
{
|
||||
Pem = PublicKeyPem,
|
||||
Padding = Padding
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding);
|
||||
|
||||
public string Decrypt(string data) => _rsa.Decrypt(data.Base64ToByte(), Padding).BytesToString();
|
||||
}
|
||||
}
|
||||
11
DigitalData.Core.Security/RSAEncryptor.cs
Normal file
11
DigitalData.Core.Security/RSAEncryptor.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace DigitalData.Core.Security
|
||||
{
|
||||
public class RSAEncryptor : RSACryptographer
|
||||
{
|
||||
public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding);
|
||||
|
||||
public string Encrypt(string data) => _rsa.Encrypt(data.Base64ToByte(), Padding).BytesToString();
|
||||
|
||||
public bool Verify(string data, string signature) => Encrypt(data) == signature;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user