Skip to content

Commit

Permalink
Merge pull request #298 from mattosaurus/bug/297-decrypt-with-no-pass…
Browse files Browse the repository at this point in the history
…word

Bug/297 decrypt with no password
  • Loading branch information
mattosaurus authored Sep 16, 2024
2 parents 4afc247 + b87fc34 commit 93b2ff9
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 26 deletions.
39 changes: 37 additions & 2 deletions PgpCore.Tests/UnitTests/Decrypt/DecryptAsync.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)]
Expand Down
40 changes: 37 additions & 3 deletions PgpCore.Tests/UnitTests/Decrypt/DecryptSync.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
78 changes: 60 additions & 18 deletions PgpCore/Models/EncryptionKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/// <summary>
/// Initializes a new instance of the EncryptionKeys class.
Expand All @@ -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);
}
}

/// <summary>
Expand All @@ -299,9 +313,16 @@ public EncryptionKeys(IEnumerable<string> 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);
}
}

/// <summary>
Expand All @@ -324,19 +345,33 @@ public EncryptionKeys(IEnumerable<FileInfo> 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)
{
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<Stream> publicKeyStreams)
Expand All @@ -349,9 +384,16 @@ public EncryptionKeys(IEnumerable<Stream> 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
Expand Down
6 changes: 3 additions & 3 deletions PgpCore/PgpCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<PackageProjectUrl>https://github.com/mattosaurus/PgpCore</PackageProjectUrl>
<RepositoryUrl>https://github.com/mattosaurus/PgpCore</RepositoryUrl>
<PackageTags>PGP .NET Core</PackageTags>
<Version>6.5.0.0</Version>
<Version>6.5.1.0</Version>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<FileVersion>6.5.0</FileVersion>
<PackageReleaseNotes>v6.5.0 - Fix setting key hash algorithim</PackageReleaseNotes>
<FileVersion>6.5.1</FileVersion>
<PackageReleaseNotes>v6.5.1 - Make publioc key in place of private key wrrors more obvious</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down

0 comments on commit 93b2ff9

Please sign in to comment.