Skip to content

Commit

Permalink
Decrypt method with privateKeyFilePath parameter not disposing privat…
Browse files Browse the repository at this point in the history
…e key stream. #21
  • Loading branch information
Cinchoo committed Oct 30, 2020
1 parent ff46b45 commit e6a6423
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 20 deletions.
4 changes: 3 additions & 1 deletion ChoPGP.Core/ChoPGP.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Authors>Raj Nagalingam</Authors>
<Authors>Cinchoo</Authors>
<Copyright>Copyright 2018 Cinchoo Inc</Copyright>
<PackageTags>PGP .NET c#</PackageTags>
<Description>PGP Library for .NET Core</Description>
Expand All @@ -14,6 +14,8 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/Cinchoo/ChoPGP</RepositoryUrl>
<Version>1.0.0.3</Version>
<RepositoryType>GitHub</RepositoryType>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions ChoPGP.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ static void Main(string[] args)

//EncryptFileNSign();
//DecryptFileNVerify();
EncryptNSign();
DecryptNVerify();
EncryptFile();
//DecryptNVerify();
}

private static void GenerateKeyPair()
Expand All @@ -37,7 +37,7 @@ private static void EncryptFile()
{
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Sample_Pub.asc", true, false);
pgp.EncryptFile(@"C:\Temp\SampleData.txt", @"C:\Temp\SampleData.PGP", "Sample_Pub.asc", true, false);
Console.WriteLine("PGP Encryption done.");
}
}
Expand Down Expand Up @@ -101,9 +101,9 @@ private static void EncryptNSign()
{
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
using (Stream input = File.OpenRead("SampleData.txt"))
using (Stream input = File.OpenRead(@"C:\Temp\SampleData.txt"))
{
using (Stream output = File.OpenWrite("SampleData.PGP"))
using (Stream output = File.OpenWrite(@"C:\Temp\SampleData.PGP"))
{
//pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip;
pgp.EncryptAndSign(input, output, "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false);
Expand Down
126 changes: 114 additions & 12 deletions ChoPGP/ChoPGPEncryptDecrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ public void EncryptFile(string inputFilePath, string outputFilePath, string publ
public async Task EncryptAsync(Stream inputStream, Stream outputStream, string publicKeyFilePath,
bool armor = true, bool withIntegrityCheck = true)
{
await Task.Run(() => Encrypt(inputStream, outputStream, File.OpenRead(publicKeyFilePath), armor, withIntegrityCheck));
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));

await Task.Run(() => {
using (Stream pkStream = File.OpenRead(publicKeyFilePath))
Encrypt(inputStream, outputStream, pkStream, armor, withIntegrityCheck);
});
}

public async Task EncryptAsync(Stream inputStream, Stream outputStream, Stream publicKeyStream,
Expand All @@ -171,7 +179,13 @@ public async Task EncryptAsync(Stream inputStream, Stream outputStream, Stream p
public void Encrypt(Stream inputStream, Stream outputStream, string publicKeyFilePath,
bool armor = true, bool withIntegrityCheck = true)
{
Encrypt(inputStream, outputStream, File.OpenRead(publicKeyFilePath),
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));

using (Stream pkStream = File.OpenRead(publicKeyFilePath))
Encrypt(inputStream, outputStream, pkStream,
armor, withIntegrityCheck);
}

Expand Down Expand Up @@ -306,8 +320,25 @@ private void OutputEncrypted(string inputFilePath, Stream outputStream, ChoPGPEn

public async Task EncryptAndSignAsync(Stream inputStream, Stream outputStream, string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor = true, bool withIntegrityCheck = true)
{
await Task.Run(() => EncryptAndSignAsync(inputStream, outputStream, File.OpenRead(publicKeyFilePath),
File.OpenRead(privateKeyFilePath), passPhrase, armor, withIntegrityCheck));
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (String.IsNullOrEmpty(privateKeyFilePath))
throw new ArgumentException(nameof(privateKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));
if (!File.Exists(privateKeyFilePath))
throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));

await Task.Run(() => {
using (Stream pkStream = File.OpenRead(publicKeyFilePath))
{
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
{
EncryptAndSignAsync(inputStream, outputStream, pkStream,
keyStream, passPhrase, armor, withIntegrityCheck);
}
}
});
}

public async Task EncryptAndSignAsync(Stream inputStream, Stream outputStream, Stream publicKeyStream,
Expand All @@ -319,7 +350,22 @@ await Task.Run(() => EncryptAndSignAsync(inputStream, outputStream, publicKeyStr

public void EncryptAndSign(Stream inputStream, Stream outputStream, string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor = true, bool withIntegrityCheck = true)
{
EncryptAndSign(inputStream, outputStream, File.OpenRead(publicKeyFilePath), File.OpenRead(privateKeyFilePath), passPhrase, armor, withIntegrityCheck);
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (String.IsNullOrEmpty(privateKeyFilePath))
throw new ArgumentException(nameof(privateKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));
if (!File.Exists(privateKeyFilePath))
throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));

using (Stream pkStream = File.OpenRead(publicKeyFilePath))
{
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
{
EncryptAndSign(inputStream, outputStream, pkStream, keyStream, passPhrase, armor, withIntegrityCheck);
}
}
}

/// <summary>
Expand Down Expand Up @@ -500,7 +546,16 @@ public void DecryptFile(string inputFilePath, string outputFilePath, string priv

public async Task DecryptAsync(Stream inputStream, Stream outputStream, string privateKeyFilePath, string passPhrase)
{
await Task.Run(() => Decrypt(inputStream, outputStream, File.OpenRead(privateKeyFilePath), passPhrase));
if (String.IsNullOrEmpty(privateKeyFilePath))
throw new ArgumentException(nameof(privateKeyFilePath));
if (!File.Exists(privateKeyFilePath))
throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));

await Task.Run(() =>
{
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
Decrypt(inputStream, outputStream, keyStream, passPhrase);
});
}

public async Task DecryptAsync(Stream inputStream, Stream outputStream, Stream privateKeyStream, string passPhrase)
Expand All @@ -510,7 +565,8 @@ public async Task DecryptAsync(Stream inputStream, Stream outputStream, Stream p

public void Decrypt(Stream inputStream, Stream outputStream, string privateKeyFilePath, string passPhrase)
{
Decrypt(inputStream, outputStream, File.OpenRead(privateKeyFilePath), passPhrase);
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
Decrypt(inputStream, outputStream, keyStream, passPhrase);
}

public byte[] DecryptInMemory(byte[] inputData, Stream keyIn, string passCode)
Expand Down Expand Up @@ -817,8 +873,6 @@ public void Decrypt(Stream inputStream, Stream outputStream, Stream privateKeySt
if (objFactory != null)
obj = objFactory.NextPgpObject();



// the first object might be a PGP marker packet.
PgpEncryptedDataList enc = null;
if (obj is PgpEncryptedDataList)
Expand Down Expand Up @@ -975,7 +1029,25 @@ public void DecryptFileAndVerify(string inputFilePath, string outputFilePath, st

public async Task DecryptAndVerifyAsync(Stream inputStream, Stream outputStream, string publicKeyFilePath, string privateKeyFilePath, string passPhrase)
{
await Task.Run(() => DecryptAndVerify(inputStream, outputStream, File.OpenRead(publicKeyFilePath), File.OpenRead(privateKeyFilePath), passPhrase));
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (String.IsNullOrEmpty(privateKeyFilePath))
throw new ArgumentException(nameof(privateKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));
if (!File.Exists(privateKeyFilePath))
throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));

await Task.Run(() =>
{
using (Stream pkStream = File.OpenRead(publicKeyFilePath))
{
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
{
DecryptAndVerify(inputStream, outputStream, pkStream, keyStream, passPhrase);
}
}
});
}

public async Task DecryptAndVerifyAsync(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase)
Expand All @@ -985,11 +1057,41 @@ public async Task DecryptAndVerifyAsync(Stream inputStream, Stream outputStream,

public void DecryptAndVerify(Stream inputStream, Stream outputStream, string publicKeyFilePath, string privateKeyFilePath, string passPhrase)
{
DecryptAndVerify(inputStream, outputStream, File.OpenRead(publicKeyFilePath), File.OpenRead(privateKeyFilePath), passPhrase);
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (String.IsNullOrEmpty(privateKeyFilePath))
throw new ArgumentException(nameof(privateKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));
if (!File.Exists(privateKeyFilePath))
throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));

using (Stream pkStream = File.OpenRead(publicKeyFilePath))
{
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
{
DecryptAndVerify(inputStream, outputStream, pkStream, keyStream, passPhrase);
}
}
}
public Stream DecryptAndVerifyInMemory(Stream inputStream, Stream outputStream, string publicKeyFilePath, string privateKeyFilePath, string passPhrase)
{
return DecryptAndVerifyInMemory(inputStream, outputStream, File.OpenRead(publicKeyFilePath), File.OpenRead(privateKeyFilePath), passPhrase);
if (String.IsNullOrEmpty(publicKeyFilePath))
throw new ArgumentException(nameof(publicKeyFilePath));
if (String.IsNullOrEmpty(privateKeyFilePath))
throw new ArgumentException(nameof(privateKeyFilePath));
if (!File.Exists(publicKeyFilePath))
throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));
if (!File.Exists(privateKeyFilePath))
throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));

using (Stream pkStream = File.OpenRead(publicKeyFilePath))
{
using (Stream keyStream = File.OpenRead(privateKeyFilePath))
{
return DecryptAndVerifyInMemory(inputStream, outputStream, pkStream, keyStream, passPhrase);
}
}
}

public void DecryptAndVerify(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase)
Expand Down
4 changes: 2 additions & 2 deletions ChoPGP/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.1.3")]
[assembly: AssemblyFileVersion("1.0.1.3")]

0 comments on commit e6a6423

Please sign in to comment.