diff --git a/ChoPGP.Core/ChoPGP.Core.csproj b/ChoPGP.Core/ChoPGP.Core.csproj index 2134a10..37047c6 100644 --- a/ChoPGP.Core/ChoPGP.Core.csproj +++ b/ChoPGP.Core/ChoPGP.Core.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - Raj Nagalingam + Cinchoo Copyright 2018 Cinchoo Inc PGP .NET c# PGP Library for .NET Core @@ -14,6 +14,8 @@ true true https://github.com/Cinchoo/ChoPGP + 1.0.0.3 + GitHub diff --git a/ChoPGP.Test/Program.cs b/ChoPGP.Test/Program.cs index e557ea3..c014448 100644 --- a/ChoPGP.Test/Program.cs +++ b/ChoPGP.Test/Program.cs @@ -21,8 +21,8 @@ static void Main(string[] args) //EncryptFileNSign(); //DecryptFileNVerify(); - EncryptNSign(); - DecryptNVerify(); + EncryptFile(); + //DecryptNVerify(); } private static void GenerateKeyPair() @@ -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."); } } @@ -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); diff --git a/ChoPGP/ChoPGPEncryptDecrypt.cs b/ChoPGP/ChoPGPEncryptDecrypt.cs index 6a6c560..d897a4b 100644 --- a/ChoPGP/ChoPGPEncryptDecrypt.cs +++ b/ChoPGP/ChoPGPEncryptDecrypt.cs @@ -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, @@ -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); } @@ -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, @@ -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); + } + } } /// @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/ChoPGP/Properties/AssemblyInfo.cs b/ChoPGP/Properties/AssemblyInfo.cs index 6591649..f897036 100644 --- a/ChoPGP/Properties/AssemblyInfo.cs +++ b/ChoPGP/Properties/AssemblyInfo.cs @@ -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")]