38 lines
1.1 KiB
C#

using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Extensions;
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;
public IRSAEncryptor Encryptor
{
get
{
return new RSAEncryptor()
{
PublicKeyPem = _rsa.ExportRSAPublicKeyPem(),
Padding = Padding
};
}
}
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();
}
}