diff --git a/DigitalData.Core.Security/RSACryptographer.cs b/DigitalData.Core.Security/RSACryptographer.cs index 8093a8a..55161cc 100644 --- a/DigitalData.Core.Security/RSACryptographer.cs +++ b/DigitalData.Core.Security/RSACryptographer.cs @@ -9,7 +9,7 @@ namespace DigitalData.Core.Security public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256; - protected readonly RSA _rsa = RSA.Create(); + protected virtual RSA RSA { get; } = RSA.Create(); internal RSACryptographer() { } } diff --git a/DigitalData.Core.Security/RSADecryptor.cs b/DigitalData.Core.Security/RSADecryptor.cs index 5c22d51..b610b5d 100644 --- a/DigitalData.Core.Security/RSADecryptor.cs +++ b/DigitalData.Core.Security/RSADecryptor.cs @@ -1,6 +1,6 @@ using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Security.Extensions; -using System.Runtime.Serialization; +using System.Security.Cryptography; namespace DigitalData.Core.Security { @@ -16,33 +16,32 @@ namespace DigitalData.Core.Security public IRSAEncryptor Encryptor => _lazyEncryptor.Value; + private readonly Lazy lazyRSA; + + protected override RSA RSA => lazyRSA.Value; + public RSADecryptor() { _lazyEncryptor = new(() => new RSAEncryptor() { - Pem = _rsa.ExportRSAPublicKeyPem(), + Pem = RSA.ExportRSAPublicKeyPem(), Padding = Padding }); + + lazyRSA = new(() => + { + var rsa = RSA.Create(); + if (Password is null) + RSA.ImportFromPem(Pem); + else + RSA.ImportFromEncryptedPem(Pem, Password.AsSpan()); + + return rsa; + }); } + + public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding); - [OnDeserialized] - private void OnDeserialized(StreamingContext context) => Init(); - - private IRSADecryptor Init() - { - if (string.IsNullOrWhiteSpace(Pem)) - throw new InvalidOperationException("Pem cannot be null or empty."); - - if (Password is null) - _rsa.ImportFromPem(Pem); - else - _rsa.ImportFromEncryptedPem(Pem, Password.AsSpan()); - - return this; - } - - 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(); } } \ No newline at end of file diff --git a/DigitalData.Core.Security/RSAEncryptor.cs b/DigitalData.Core.Security/RSAEncryptor.cs index 4ee7e13..7783902 100644 --- a/DigitalData.Core.Security/RSAEncryptor.cs +++ b/DigitalData.Core.Security/RSAEncryptor.cs @@ -10,17 +10,14 @@ namespace DigitalData.Core.Security get => base.Pem; init { - if (string.IsNullOrWhiteSpace(Pem)) - throw new InvalidOperationException("Pem cannot be null or empty."); - - _rsa.ImportFromPem(base.Pem); + RSA.ImportFromPem(base.Pem); base.Pem = value; } } - public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding); + public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding); - public string Encrypt(string data) => _rsa.Encrypt(data.Base64ToByte(), Padding).BytesToString(); + public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString(); public bool Verify(string data, string signature) => Encrypt(data) == signature; }