raise events on error

This commit is contained in:
2020-11-26 12:35:36 +01:00
parent e833e48421
commit 4fe144c8b1
2 changed files with 10 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ namespace Pilz.Cryptography
public class AESStringCrypter : ICrypter public class AESStringCrypter : ICrypter
{ {
public delegate void AESStringCrypterEventHandler(AESStringCrypter sender); public delegate void AESStringCrypterEventHandler(AESStringCrypter sender);
public delegate void AESStringCrypterErrorEventHandler(AESStringCrypter sender, Exception exception);
/// <summary> /// <summary>
/// This event throws if no key and no default key was found. You can now provide a key or default key (maybe depending on the given instance). If no value is provided, a default key will be created. /// This event throws if no key and no default key was found. You can now provide a key or default key (maybe depending on the given instance). If no value is provided, a default key will be created.
@@ -18,6 +19,8 @@ namespace Pilz.Cryptography
/// This event throws if no IV and no default IV was found. You can now provide a IV or default IV (maybe depending on the given instance). If no value is provided, a default IV will be created. /// This event throws if no IV and no default IV was found. You can now provide a IV or default IV (maybe depending on the given instance). If no value is provided, a default IV will be created.
/// </summary> /// </summary>
public static event AESStringCrypterEventHandler NeedIV; public static event AESStringCrypterEventHandler NeedIV;
public static event AESStringCrypterErrorEventHandler ErrorAtDecrypting;
public static event AESStringCrypterErrorEventHandler ErrorAtEncrypting;
private byte[] key = new byte[] { }; private byte[] key = new byte[] { };
private byte[] iv = new byte[] { }; private byte[] iv = new byte[] { };
@@ -69,8 +72,9 @@ namespace Pilz.Cryptography
{ {
return DecryptStringFromBytes_Aes(Convert.FromBase64String(encryptedValue), GetKey(), GetIV()); return DecryptStringFromBytes_Aes(Convert.FromBase64String(encryptedValue), GetKey(), GetIV());
} }
catch (CryptographicException) catch (Exception ex)
{ {
ErrorAtDecrypting?.Invoke(this, ex);
return string.Empty; return string.Empty;
} }
} }
@@ -87,8 +91,9 @@ namespace Pilz.Cryptography
{ {
return Convert.ToBase64String(EncryptStringToBytes_Aes(plainValue, GetKey(), GetIV())); return Convert.ToBase64String(EncryptStringToBytes_Aes(plainValue, GetKey(), GetIV()));
} }
catch (CryptographicException) catch (Exception ex)
{ {
ErrorAtEncrypting?.Invoke(this, ex);
return string.Empty; return string.Empty;
} }
} }

View File

@@ -5,6 +5,9 @@
<Product>Pilz.Cryptography</Product> <Product>Pilz.Cryptography</Product>
<Copyright>Copyright © 2020</Copyright> <Copyright>Copyright © 2020</Copyright>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>D:\repos\Pilz\Pilz.Cryptography\Pilz.Cryptography.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Management" Version="4.7.0" /> <PackageReference Include="System.Management" Version="4.7.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" /> <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />