diff --git a/PgpCore.Tests/UnitTests/Decrypt/DecryptAsync.File.cs b/PgpCore.Tests/UnitTests/Decrypt/DecryptAsync.File.cs index 1a70730..5f88735 100644 --- a/PgpCore.Tests/UnitTests/Decrypt/DecryptAsync.File.cs +++ b/PgpCore.Tests/UnitTests/Decrypt/DecryptAsync.File.cs @@ -7,8 +7,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using System.Threading.Tasks; using Xunit; @@ -466,6 +464,43 @@ public async Task DecryptAsync_DecryptEncryptedMessageWithWrongKey_ShouldThrowEx testFactory.Teardown(); } + [Fact] + public async Task DecryptAsync_DecryptEncryptedMessageWithoutPassword_ShouldDecryptMessage() + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(KeyType.Generated, FileType.Known); + string password = string.Empty; + + PGP pgpKeys = new PGP(); + await pgpKeys.GenerateKeyAsync( + testFactory.PublicKeyFileInfo, + testFactory.PrivateKeyFileInfo, + testFactory.UserName, + password + ); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); + EncryptionKeys decryptionKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpDecrypt = new PGP(decryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); + await pgpDecrypt.DecryptAsync(testFactory.EncryptedContentFileInfo, testFactory.DecryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + testFactory.DecryptedContentFileInfo.Exists.Should().BeTrue(); + File.ReadAllText(testFactory.DecryptedContentFileInfo.FullName).Should().Be(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + [Theory] [InlineData(KeyType.Generated)] [InlineData(KeyType.Known)] diff --git a/PgpCore.Tests/UnitTests/Decrypt/DecryptSync.File.cs b/PgpCore.Tests/UnitTests/Decrypt/DecryptSync.File.cs index a47893d..0d0280f 100644 --- a/PgpCore.Tests/UnitTests/Decrypt/DecryptSync.File.cs +++ b/PgpCore.Tests/UnitTests/Decrypt/DecryptSync.File.cs @@ -7,9 +7,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Xunit; namespace PgpCore.Tests.UnitTests.Decrypt @@ -465,6 +462,43 @@ public void Decrypt_DecryptEncryptedMessageWithWrongKey_ShouldThrowException(Key testFactory.Teardown(); } + [Fact] + public void Decrypt_DecryptEncryptedMessageWithoutPassword_ShouldDecryptMessage() + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(KeyType.Generated, FileType.Known); + string password = string.Empty; + + PGP pgpKeys = new PGP(); + pgpKeys.GenerateKey( + testFactory.PublicKeyFileInfo, + testFactory.PrivateKeyFileInfo, + testFactory.UserName, + password + ); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); + EncryptionKeys decryptionKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpDecrypt = new PGP(decryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); + pgpDecrypt.Decrypt(testFactory.EncryptedContentFileInfo, testFactory.DecryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + testFactory.DecryptedContentFileInfo.Exists.Should().BeTrue(); + File.ReadAllText(testFactory.DecryptedContentFileInfo.FullName).Should().Be(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + [Theory] [InlineData(KeyType.Generated)] [InlineData(KeyType.Known)] diff --git a/PgpCore/Models/EncryptionKeys.cs b/PgpCore/Models/EncryptionKeys.cs index 804ba8f..8a70708 100644 --- a/PgpCore/Models/EncryptionKeys.cs +++ b/PgpCore/Models/EncryptionKeys.cs @@ -258,10 +258,17 @@ public EncryptionKeys(string publicKey) if (string.IsNullOrEmpty(publicKey)) throw new ArgumentException("PublicKey"); - var keyRings = Utilities.ReadAllKeyRings(publicKey.GetStream()); - - InitializeKeys(keyRings); - } + try + { + var keyRings = Utilities.ReadAllKeyRings(publicKey.GetStream()); + + InitializeKeys(keyRings); + } + catch (Exception ex) + { + throw new PgpException($"Error reading public key file [{publicKey}].", ex); + } + } /// /// Initializes a new instance of the EncryptionKeys class. @@ -278,9 +285,16 @@ public EncryptionKeys(FileInfo publicKeyFile) if (!publicKeyFile.Exists) throw new FileNotFoundException($"Public Key file [{publicKeyFile.FullName}] does not exist."); - var keyRings = Utilities.ReadAllKeyRings(publicKeyFile.OpenRead()); + try + { + var keyRings = Utilities.ReadAllKeyRings(publicKeyFile.OpenRead()); - InitializeKeys(keyRings); + InitializeKeys(keyRings); + } + catch (Exception ex) + { + throw new PgpException($"Error reading public key file [{publicKeyFile.FullName}].", ex); + } } /// @@ -299,9 +313,16 @@ public EncryptionKeys(IEnumerable publicKeys) throw new ArgumentException(nameof(publicKey)); } - var keyRings = Utilities.ReadAllKeyRings(publicKeyStrings.Select(s => s.GetStream())); - - InitializeKeys(keyRings); + try + { + var keyRings = Utilities.ReadAllKeyRings(publicKeyStrings.Select(s => s.GetStream())); + + InitializeKeys(keyRings); + } + catch (Exception ex) + { + throw new PgpException("Error reading public key files.", ex); + } } /// @@ -324,9 +345,16 @@ public EncryptionKeys(IEnumerable publicKeyFiles) throw new FileNotFoundException($"Input file [{publicKeyFile.FullName}] does not exist."); } - var keyRings = Utilities.ReadAllKeyRings(publicKeys.Select(fileInfo => fileInfo.OpenRead())); - - InitializeKeys(keyRings); + try + { + var keyRings = Utilities.ReadAllKeyRings(publicKeys.Select(fileInfo => fileInfo.OpenRead())); + + InitializeKeys(keyRings); + } + catch (Exception ex) + { + throw new PgpException("Error reading public key files.", ex); + } } public EncryptionKeys(Stream publicKeyStream) @@ -334,9 +362,16 @@ public EncryptionKeys(Stream publicKeyStream) if (publicKeyStream == null) throw new ArgumentException("PublicKeyStream"); - var keyRings = Utilities.ReadAllKeyRings(publicKeyStream); - - InitializeKeys(keyRings); + try + { + var keyRings = Utilities.ReadAllKeyRings(publicKeyStream); + + InitializeKeys(keyRings); + } + catch (Exception ex) + { + throw new PgpException("Error reading public key stream.", ex); + } } public EncryptionKeys(IEnumerable publicKeyStreams) @@ -349,9 +384,16 @@ public EncryptionKeys(IEnumerable publicKeyStreams) throw new ArgumentException("PublicKeyStream"); } - var keyRings = Utilities.ReadAllKeyRings(publicKeys); - - InitializeKeys(keyRings); + try + { + var keyRings = Utilities.ReadAllKeyRings(publicKeys); + + InitializeKeys(keyRings); + } + catch (Exception ex) + { + throw new PgpException("Error reading public key streams.", ex); + } } #endregion Constructors diff --git a/PgpCore/PgpCore.csproj b/PgpCore/PgpCore.csproj index af026d4..7ad3504 100644 --- a/PgpCore/PgpCore.csproj +++ b/PgpCore/PgpCore.csproj @@ -10,10 +10,10 @@ https://github.com/mattosaurus/PgpCore https://github.com/mattosaurus/PgpCore PGP .NET Core - 6.5.0.0 + 6.5.1.0 6.0.0.0 - 6.5.0 - v6.5.0 - Fix setting key hash algorithim + 6.5.1 + v6.5.1 - Make publioc key in place of private key wrrors more obvious MIT true true