From 9b47abc0f827fe07cde763ff4cb88af19b1045e8 Mon Sep 17 00:00:00 2001 From: Mateo Date: Thu, 14 Mar 2019 16:44:57 +0100 Subject: [PATCH 1/4] Create unit testing --- ChoPGP.sln | 8 +- UnitTestProject1/Properties/AssemblyInfo.cs | 20 +++ UnitTestProject1/UnitTest1.cs | 186 ++++++++++++++++++++ UnitTestProject1/UnitTestProject1.csproj | 74 ++++++++ UnitTestProject1/packages.config | 5 + 5 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 UnitTestProject1/Properties/AssemblyInfo.cs create mode 100644 UnitTestProject1/UnitTest1.cs create mode 100644 UnitTestProject1/UnitTestProject1.csproj create mode 100644 UnitTestProject1/packages.config diff --git a/ChoPGP.sln b/ChoPGP.sln index 1f07e1c..5deeeea 100644 --- a/ChoPGP.sln +++ b/ChoPGP.sln @@ -7,7 +7,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChoPGP", "ChoPGP\ChoPGP.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChoPGP.Test", "ChoPGP.Test\ChoPGP.Test.csproj", "{FF4ACC4D-6447-4229-9D24-475C2B8E6235}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChoPGP.Core", "ChoPGP.Core\ChoPGP.Core.csproj", "{9A190C26-D375-411F-86B1-F3C34A5A17BF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChoPGP.Core", "ChoPGP.Core\ChoPGP.Core.csproj", "{9A190C26-D375-411F-86B1-F3C34A5A17BF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject1", "UnitTestProject1\UnitTestProject1.csproj", "{6D62B548-5F95-40F7-A9C7-1F6187FF0F54}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {9A190C26-D375-411F-86B1-F3C34A5A17BF}.Debug|Any CPU.Build.0 = Debug|Any CPU {9A190C26-D375-411F-86B1-F3C34A5A17BF}.Release|Any CPU.ActiveCfg = Release|Any CPU {9A190C26-D375-411F-86B1-F3C34A5A17BF}.Release|Any CPU.Build.0 = Release|Any CPU + {6D62B548-5F95-40F7-A9C7-1F6187FF0F54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D62B548-5F95-40F7-A9C7-1F6187FF0F54}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D62B548-5F95-40F7-A9C7-1F6187FF0F54}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D62B548-5F95-40F7-A9C7-1F6187FF0F54}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/UnitTestProject1/Properties/AssemblyInfo.cs b/UnitTestProject1/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..fd758cb --- /dev/null +++ b/UnitTestProject1/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("UnitTestProject1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("UnitTestProject1")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("6d62b548-5f95-40f7-a9c7-1f6187ff0f54")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UnitTestProject1/UnitTest1.cs b/UnitTestProject1/UnitTest1.cs new file mode 100644 index 0000000..e455e88 --- /dev/null +++ b/UnitTestProject1/UnitTest1.cs @@ -0,0 +1,186 @@ +using System; +using System.IO; +using System.Text; +using Cinchoo.PGP; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace UnitTestProject1 +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + } + + [TestMethod] + [Priority(0)] + public void TESTGenerateKeyPair() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + pgp.GenerateKey("Sample_Pub.asc", "Sample_Pri.asc", "mark@gmail.com", "Test123"); + + Console.WriteLine("PGP KeyPair generated."); + } + [TestMethod] + [Priority(1)] + public void EncryptFile() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Sample_Pub.asc", true, false); + Console.WriteLine("PGP Encryption done."); + } + } + [TestMethod] + [Priority(2)] + public void DecryptFile() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + pgp.DecryptFile("SampleData.PGP", "SampleData.OUT", "Sample_Pri.asc", "Test123"); + Console.WriteLine("PGP Decryption done."); + } + } + [TestMethod] + [Priority(3)] + public void Encrypt() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + using (Stream input = File.OpenRead("SampleData.txt")) + { + using (Stream output = File.OpenWrite("SampleData.PGP")) + { + //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; + pgp.Encrypt(input, output, "Sample_Pub.asc", true, false); + } + } + } + } + [TestMethod] + [Priority(4)] + public void Decrypt() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + using (Stream input = File.OpenRead("SampleData.PGP")) + { + using (Stream output = File.OpenWrite("SampleData.OUT")) + { + pgp.Decrypt(input, output, "Sample_Pri.asc", "Test123"); + } + } + Console.WriteLine("PGP Decryption done."); + } + } + [TestMethod] + [Priority(5)] + public void EncryptFileNSign() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; + pgp.EncryptFileAndSign("SampleData.txt", "SampleData.PGP", "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false); + Console.WriteLine("PGP Encryption done."); + } + } + [Priority(6)] + public void DecryptFileNVerify() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + pgp.DecryptFileAndVerify("SampleData.PGP", "SampleData.OUT", "Sample_Pub.asc", "Sample_Pri.asc", "Test123"); + Console.WriteLine("PGP Decryption done."); + } + } + [TestMethod] + [Priority(7)] + public void EncryptNSign() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + using (Stream input = File.OpenRead("SampleData.txt")) + { + using (Stream output = File.OpenWrite("SampleData.PGP")) + { + //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; + pgp.EncryptAndSign(input, output, "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false); + } + } + } + Console.WriteLine("PGP Encryption done."); + } + [TestMethod] + [Priority(8)] + public void DecryptNVerify() + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + using (Stream input = File.OpenRead("SampleData.PGP")) + { + using (Stream output = File.OpenWrite("SampleData.OUT")) + { + pgp.DecryptAndVerify(input, output, "Sample_Pub.asc", "Sample_Pri.asc", "Test123"); + } + } + Console.WriteLine("PGP Decryption done."); + } + } + + [TestMethod] + [Priority(9)] + public void EncryptDecriptString() + { + Stream a = EncryptString("hello"); + string message = DecryptString(a, "Test123"); + Assert.AreEqual("hello", message); + } + private Stream EncryptString(string text) + { + + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + // pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false); + // pgp.DecryptFile("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123"); + MemoryStream a = new System.IO.MemoryStream(); + + byte[] byteArray = Encoding.UTF8.GetBytes(text); + Stream stream = new MemoryStream(byteArray); + + pgp.Encrypt(stream, a, "Sample_Pub.asc", true, false); + return a; + + + + + + } + + } + + private string DecryptString(Stream text, string pass) + { + + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + + using (MemoryStream a = new System.IO.MemoryStream()) + { + + pgp.Decrypt(text, a, "Sample_Pri.asc", "Test123"); + StreamReader reader = new StreamReader(a); + return reader.ReadToEnd(); + + } + + + + + + } + } + } +} + diff --git a/UnitTestProject1/UnitTestProject1.csproj b/UnitTestProject1/UnitTestProject1.csproj new file mode 100644 index 0000000..10fdb7f --- /dev/null +++ b/UnitTestProject1/UnitTestProject1.csproj @@ -0,0 +1,74 @@ + + + + + + Debug + AnyCPU + {6D62B548-5F95-40F7-A9C7-1F6187FF0F54} + Library + Properties + UnitTestProject1 + UnitTestProject1 + v4.5.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + {B3911FE9-0A4B-4D07-BB93-5960D1BDBCC2} + ChoPGP + + + + + + + Este proyecto hace referencia a los paquetes NuGet que faltan en este equipo. Use la restauración de paquetes NuGet para descargarlos. Para obtener más información, consulte http://go.microsoft.com/fwlink/?LinkID=322105. El archivo que falta es {0}. + + + + + + \ No newline at end of file diff --git a/UnitTestProject1/packages.config b/UnitTestProject1/packages.config new file mode 100644 index 0000000..e5ca672 --- /dev/null +++ b/UnitTestProject1/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From bfa3d5559b36c84685eddaa1c8dc0301aa0f9fd1 Mon Sep 17 00:00:00 2001 From: Mateo Date: Fri, 15 Mar 2019 15:11:50 +0100 Subject: [PATCH 2/4] Implement and test. Decrypt Stream in memory and other. --- ChoPGP/ChoPGPEncryptDecrypt.cs | 469 ++++++++++++++++++++++++++++++++- UnitTestProject1/UnitTest1.cs | 123 ++++++--- 2 files changed, 557 insertions(+), 35 deletions(-) diff --git a/ChoPGP/ChoPGPEncryptDecrypt.cs b/ChoPGP/ChoPGPEncryptDecrypt.cs index 26d4e3f..e4e7389 100644 --- a/ChoPGP/ChoPGPEncryptDecrypt.cs +++ b/ChoPGP/ChoPGPEncryptDecrypt.cs @@ -17,6 +17,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Linq; namespace Cinchoo.PGP { @@ -518,6 +519,386 @@ public void Decrypt(Stream inputStream, Stream outputStream, string privateKeyFi Decrypt(inputStream, outputStream, File.OpenRead(privateKeyFilePath), passPhrase); } + public byte[] DecryptInMemory(byte[] inputData, Stream keyIn, string passCode) + { + byte[] error = Encoding.ASCII.GetBytes("ERROR"); + + Stream inputStream = new MemoryStream(inputData); + inputStream = PgpUtilities.GetDecoderStream(inputStream); + MemoryStream decoded = new MemoryStream(); + + try + { + PgpObjectFactory pgpF = new PgpObjectFactory(inputStream); + PgpEncryptedDataList enc; + PgpObject o = pgpF.NextPgpObject(); + + // + // the first object might be a PGP marker packet. + // + if (o is PgpEncryptedDataList) + enc = (PgpEncryptedDataList)o; + else + enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); + + // + // find the secret key + // + PgpPrivateKey sKey = null; + PgpPublicKeyEncryptedData pbe = null; + PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( + PgpUtilities.GetDecoderStream(keyIn)); + foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) + { + sKey = FindSecretKey(pgpSec, pked.KeyId, passCode.ToCharArray()); + if (sKey != null) + { + pbe = pked; + break; + } + } + if (sKey == null) + throw new ArgumentException("secret key for message not found."); + + Stream clear = pbe.GetDataStream(sKey); + PgpObjectFactory plainFact = new PgpObjectFactory(clear); + PgpObject message = plainFact.NextPgpObject(); + + if (message is PgpCompressedData) + { + PgpCompressedData cData = (PgpCompressedData)message; + PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream()); + message = pgpFact.NextPgpObject(); + } + if (message is PgpLiteralData) + { + PgpLiteralData ld = (PgpLiteralData)message; + Stream unc = ld.GetInputStream(); + Streams.PipeAll(unc, decoded); + } + else if (message is PgpOnePassSignatureList) + throw new PgpException("encrypted message contains a signed message - not literal data."); + else + throw new PgpException("message is not a simple encrypted file - type unknown."); + + //if (pbe.IsIntegrityProtected()) + //{ + // if (!pbe.Verify()) + // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + // else + // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + //else + //{ + // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + + return decoded.ToArray(); + } + catch (Exception e) + { + throw e; + //if (e.Message.StartsWith("Checksum mismatch")) + // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Object reference not")) + // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Premature end of stream")) + // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else + // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //Exception underlyingException = e.InnerException; + //if (underlyingException != null) + // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + + //return error; + } + } + + public MemoryStream DecryptInMemoryMS(byte[] inputData, Stream keyIn, string passCode) + { + byte[] error = Encoding.ASCII.GetBytes("ERROR"); + + Stream inputStream = new MemoryStream(inputData); + inputStream = PgpUtilities.GetDecoderStream(inputStream); + MemoryStream decoded = new MemoryStream(); + + try + { + PgpObjectFactory pgpF = new PgpObjectFactory(inputStream); + PgpEncryptedDataList enc; + PgpObject o = pgpF.NextPgpObject(); + + // + // the first object might be a PGP marker packet. + // + if (o is PgpEncryptedDataList) + enc = (PgpEncryptedDataList)o; + else + enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); + + // + // find the secret key + // + PgpPrivateKey sKey = null; + PgpPublicKeyEncryptedData pbe = null; + PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( + PgpUtilities.GetDecoderStream(keyIn)); + foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) + { + sKey = FindSecretKey(pgpSec, pked.KeyId, passCode.ToCharArray()); + if (sKey != null) + { + pbe = pked; + break; + } + } + if (sKey == null) + throw new ArgumentException("secret key for message not found."); + + Stream clear = pbe.GetDataStream(sKey); + PgpObjectFactory plainFact = new PgpObjectFactory(clear); + PgpObject message = plainFact.NextPgpObject(); + + if (message is PgpCompressedData) + { + PgpCompressedData cData = (PgpCompressedData)message; + PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream()); + message = pgpFact.NextPgpObject(); + } + if (message is PgpLiteralData) + { + PgpLiteralData ld = (PgpLiteralData)message; + Stream unc = ld.GetInputStream(); + Streams.PipeAll(unc, decoded); + } + else if (message is PgpOnePassSignatureList) + throw new PgpException("encrypted message contains a signed message - not literal data."); + else + throw new PgpException("message is not a simple encrypted file - type unknown."); + + //if (pbe.IsIntegrityProtected()) + //{ + // if (!pbe.Verify()) + // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + // else + // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + //else + //{ + // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + + return decoded; + } + catch (Exception e) + { + throw e; + //if (e.Message.StartsWith("Checksum mismatch")) + // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Object reference not")) + // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Premature end of stream")) + // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else + // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //Exception underlyingException = e.InnerException; + //if (underlyingException != null) + // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + + //return error; + } + } + + public MemoryStream DecryptInMemoryMS(Stream inputData, Stream keyIn, string passCode) + { + byte[] error = Encoding.ASCII.GetBytes("ERROR"); + + Stream inputStream = inputData; + inputStream = PgpUtilities.GetDecoderStream(inputStream); + MemoryStream decoded = new MemoryStream(); + + try + { + PgpObjectFactory pgpF = new PgpObjectFactory(inputStream); + PgpEncryptedDataList enc; + PgpObject o = pgpF.NextPgpObject(); + + // + // the first object might be a PGP marker packet. + // + if (o is PgpEncryptedDataList) + enc = (PgpEncryptedDataList)o; + else + enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); + + // + // find the secret key + // + PgpPrivateKey sKey = null; + PgpPublicKeyEncryptedData pbe = null; + PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( + PgpUtilities.GetDecoderStream(keyIn)); + foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) + { + sKey = FindSecretKey(pgpSec, pked.KeyId, passCode.ToCharArray()); + if (sKey != null) + { + pbe = pked; + break; + } + } + if (sKey == null) + throw new ArgumentException("secret key for message not found."); + + Stream clear = pbe.GetDataStream(sKey); + PgpObjectFactory plainFact = new PgpObjectFactory(clear); + PgpObject message = plainFact.NextPgpObject(); + + if (message is PgpCompressedData) + { + PgpCompressedData cData = (PgpCompressedData)message; + PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream()); + message = pgpFact.NextPgpObject(); + } + if (message is PgpLiteralData) + { + PgpLiteralData ld = (PgpLiteralData)message; + Stream unc = ld.GetInputStream(); + Streams.PipeAll(unc, decoded); + } + else if (message is PgpOnePassSignatureList) + throw new PgpException("encrypted message contains a signed message - not literal data."); + else + throw new PgpException("message is not a simple encrypted file - type unknown."); + + //if (pbe.IsIntegrityProtected()) + //{ + // if (!pbe.Verify()) + // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + // else + // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + //else + //{ + // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + + return decoded; + } + catch (Exception e) + { + throw e; + //if (e.Message.StartsWith("Checksum mismatch")) + // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Object reference not")) + // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Premature end of stream")) + // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else + // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //Exception underlyingException = e.InnerException; + //if (underlyingException != null) + // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + + //return error; + } + } + + public string DecryptInMemoryToString(Stream inputData, Stream keyIn, string passCode) + { + byte[] error = Encoding.ASCII.GetBytes("ERROR"); + + Stream inputStream = inputData; + inputStream = PgpUtilities.GetDecoderStream(inputStream); + MemoryStream decoded = new MemoryStream(); + + try + { + PgpObjectFactory pgpF = new PgpObjectFactory(inputStream); + PgpEncryptedDataList enc; + PgpObject o = pgpF.NextPgpObject(); + + // + // the first object might be a PGP marker packet. + // + if (o is PgpEncryptedDataList) + enc = (PgpEncryptedDataList)o; + else + enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); + + // + // find the secret key + // + PgpPrivateKey sKey = null; + PgpPublicKeyEncryptedData pbe = null; + PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( + PgpUtilities.GetDecoderStream(keyIn)); + foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) + { + sKey = FindSecretKey(pgpSec, pked.KeyId, passCode.ToCharArray()); + if (sKey != null) + { + pbe = pked; + break; + } + } + if (sKey == null) + throw new ArgumentException("secret key for message not found."); + + Stream clear = pbe.GetDataStream(sKey); + PgpObjectFactory plainFact = new PgpObjectFactory(clear); + PgpObject message = plainFact.NextPgpObject(); + + if (message is PgpCompressedData) + { + PgpCompressedData cData = (PgpCompressedData)message; + PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream()); + message = pgpFact.NextPgpObject(); + } + if (message is PgpLiteralData) + { + PgpLiteralData ld = (PgpLiteralData)message; + Stream unc = ld.GetInputStream(); + Streams.PipeAll(unc, decoded); + } + else if (message is PgpOnePassSignatureList) + throw new PgpException("encrypted message contains a signed message - not literal data."); + else + throw new PgpException("message is not a simple encrypted file - type unknown."); + + //if (pbe.IsIntegrityProtected()) + //{ + // if (!pbe.Verify()) + // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + // else + // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + //else + //{ + // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + //} + StreamReader reader = new StreamReader(decoded); + return reader.ReadToEnd(); + } + catch (Exception e) + { + throw e; + //if (e.Message.StartsWith("Checksum mismatch")) + // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Object reference not")) + // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else if (e.Message.StartsWith("Premature end of stream")) + // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else + // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + //Exception underlyingException = e.InnerException; + //if (underlyingException != null) + // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + + //return error; + } + } + /* * PGP decrypt a given stream. */ @@ -539,6 +920,8 @@ public void Decrypt(Stream inputStream, Stream outputStream, Stream privateKeySt PgpObject obj = null; if (objFactory != null) obj = objFactory.NextPgpObject(); + + // the first object might be a PGP marker packet. PgpEncryptedDataList enc = null; @@ -616,9 +999,11 @@ public void Decrypt(Stream inputStream, Stream outputStream, Stream privateKeySt throw new PgpException("Message is not a simple encrypted file."); } -#endregion Decrypt + -#region DecryptFileAndVerify + #endregion Decrypt + + #region DecryptFileAndVerify public async Task DecryptFileAndVerifyAsync(string inputFilePath, string outputFilePath, string publicKeyFilePath, string privateKeyFilePath, string passPhrase) { @@ -706,6 +1091,10 @@ public void DecryptAndVerify(Stream inputStream, Stream outputStream, string pub { DecryptAndVerify(inputStream, outputStream, File.OpenRead(publicKeyFilePath), File.OpenRead(privateKeyFilePath), 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); + } public void DecryptAndVerify(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase) { @@ -754,9 +1143,56 @@ public void DecryptAndVerify(Stream inputStream, Stream outputStream, Stream pub return; } -#endregion DecryptAndVerify + public Stream DecryptAndVerifyInMemory(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase) + { + if (inputStream == null) + throw new ArgumentException(nameof(inputStream)); + if (outputStream == null) + throw new ArgumentException(nameof(outputStream)); + if (publicKeyStream == null) + throw new ArgumentException(nameof(publicKeyStream)); + if (privateKeyStream == null) + throw new ArgumentException(nameof(privateKeyStream)); + if (passPhrase == null) + passPhrase = String.Empty; -#region GenerateKey + ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyStream, privateKeyStream, passPhrase); + + if (encryptionKeys == null) + throw new ArgumentNullException("Encryption Key not found."); + + PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKeyEncryptedDataInMemory(inputStream); + if (publicKeyED.KeyId != encryptionKeys.PublicKey.KeyId) + throw new PgpException(String.Format("Failed to verify file.")); + + PgpObject message = GetClearCompressedMessage(publicKeyED, encryptionKeys); + + if (message is PgpCompressedData) + { + message = ProcessCompressedMessage(message); + PgpLiteralData literalData = (PgpLiteralData)message; + using (Stream literalDataStream = literalData.GetInputStream()) + { + Streams.PipeAll(literalDataStream, outputStream); + } + } + else if (message is PgpLiteralData) + { + PgpLiteralData literalData = (PgpLiteralData)message; + using (Stream literalDataStream = literalData.GetInputStream()) + { + Streams.PipeAll(literalDataStream, outputStream); + } + } + else + throw new PgpException("Message is not a simple encrypted file."); + + return outputStream; + } + + #endregion DecryptAndVerify + + #region GenerateKey public async Task GenerateKeyAsync(string publicKeyFilePath, string privateKeyFilePath, string username = null, string password = null, int strength = 1024, int certainty = 8) { @@ -916,6 +1352,13 @@ private static PgpPublicKeyEncryptedData ExtractPublicKeyEncryptedData(Stream in PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKey(encryptedDataList); return publicKeyED; } + private static PgpPublicKeyEncryptedData ExtractPublicKeyEncryptedDataInMemory(Stream inputData) + { + + PgpEncryptedDataList encryptedDataList = GetEncryptedDataListInMemory(inputData); + PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKey(encryptedDataList); + return publicKeyED; + } private static PgpObject ProcessCompressedMessage(PgpObject message) { PgpCompressedData compressedData = (PgpCompressedData)message; @@ -977,6 +1420,24 @@ private static PgpEncryptedDataList GetEncryptedDataList(Stream encodedFile) } return encryptedDataList; } + private static PgpEncryptedDataList GetEncryptedDataListInMemory(Stream inputData) + { + byte[] inputStream = ((MemoryStream)inputData).ToArray(); + Stream ms = new MemoryStream(inputStream); + ms = PgpUtilities.GetDecoderStream(ms); + PgpObjectFactory pgpF = new PgpObjectFactory(ms); + PgpEncryptedDataList enc; + PgpObject o = pgpF.NextPgpObject(); + + // + // the first object might be a PGP marker packet. + // + if (o is PgpEncryptedDataList) + enc = (PgpEncryptedDataList)o; + else + enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); + return enc; + } public void Dispose() { } diff --git a/UnitTestProject1/UnitTest1.cs b/UnitTestProject1/UnitTest1.cs index e455e88..3855d26 100644 --- a/UnitTestProject1/UnitTest1.cs +++ b/UnitTestProject1/UnitTest1.cs @@ -1,17 +1,42 @@ -using System; +using Cinchoo.PGP; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.IO; using System.Text; -using Cinchoo.PGP; -using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestProject1 { [TestClass] public class UnitTest1 { - [TestMethod] - public void TestMethod1() + [ClassInitialize()] + public static void initialize(TestContext testContext) { + if (!File.Exists("SampleData.txt")) + File.Create("SampleData.txt").Close(); + using (FileStream fs = File.OpenWrite("SampleData.txt")) + { + string data = "hola"; + byte[] information = new UTF8Encoding(true).GetBytes(data); + fs.Write(information, 0, information.Length); + } + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + pgp.GenerateKey("Sample_Pub.asc", "Sample_Pri.asc", "mark@gmail.com", "Test123"); + Console.WriteLine("PGP KeyPair generated."); + } + [ClassCleanup()] + public static void cleanup() + { + if (File.Exists("SampleData.txt")) + File.Delete("SampleData.txt"); + if (File.Exists("Sample_Pub.asc")) + File.Delete("Sample_Pub.asc"); + if (File.Exists("Sample_Pri.asc")) + File.Delete("Sample_Pri.asc"); + if (File.Exists("SampleData.OUT")) + File.Delete("SampleData.OUT"); + if (File.Exists("SampleData.PGP")) + File.Delete("SampleData.PGP"); } [TestMethod] @@ -20,8 +45,7 @@ public void TESTGenerateKeyPair() { using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) pgp.GenerateKey("Sample_Pub.asc", "Sample_Pri.asc", "mark@gmail.com", "Test123"); - - Console.WriteLine("PGP KeyPair generated."); + Console.WriteLine("PGP KeyPair generated."); } [TestMethod] [Priority(1)] @@ -133,54 +157,91 @@ public void DecryptNVerify() [Priority(9)] public void EncryptDecriptString() { - Stream a = EncryptString("hello"); + MemoryStream a = EncryptString("hello"); string message = DecryptString(a, "Test123"); Assert.AreEqual("hello", message); } - private Stream EncryptString(string text) + private MemoryStream EncryptString(string text) { - using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { // pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false); // pgp.DecryptFile("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123"); MemoryStream a = new System.IO.MemoryStream(); - - byte[] byteArray = Encoding.UTF8.GetBytes(text); - Stream stream = new MemoryStream(byteArray); - - pgp.Encrypt(stream, a, "Sample_Pub.asc", true, false); - return a; - - - + byte[] byteArray = Encoding.UTF8.GetBytes(text); + MemoryStream stream = new MemoryStream(byteArray); + pgp.Encrypt(stream, a, "Sample_Pub.asc", true, false); + return a; } - + } - private string DecryptString(Stream text, string pass) + private string DecryptString(MemoryStream text, string pass) { using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { + using (MemoryStream a = new System.IO.MemoryStream()) + { - using (MemoryStream a = new System.IO.MemoryStream()) - { + //pgp.Decrypt(text, a, "Sample_Pri.asc", "Test123"); + byte[] r = pgp.DecryptInMemory(text.ToArray(), File.OpenRead("Sample_Pri.asc"), pass); + MemoryStream ms = new MemoryStream(r); + StreamReader reader = new StreamReader(ms); + return reader.ReadToEnd(); - pgp.Decrypt(text, a, "Sample_Pri.asc", "Test123"); - StreamReader reader = new StreamReader(a); - return reader.ReadToEnd(); - - } - - + } + } + } + + [TestMethod] + [Priority(10)] + public void EncryptDecriptNSignStringInMemory() + { + MemoryStream a = EncryptNSignInMemory("hello"); + string message = DecryptNVerifyInMemory(a, "Test123"); + Assert.AreEqual("hello", message); + } + + private MemoryStream EncryptNSignInMemory(string text) + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + MemoryStream a = new System.IO.MemoryStream(); + + byte[] byteArray = Encoding.UTF8.GetBytes(text); + MemoryStream stream = new MemoryStream(byteArray); + //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; + pgp.EncryptAndSign(stream, a, "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false); + return a; + + } + } - + private string DecryptNVerifyInMemory(MemoryStream text, string pass) + { + using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) + { + using (MemoryStream a = new System.IO.MemoryStream()) + { + + //pgp.Decrypt(text, a, "Sample_Pri.asc", "Test123"); + Stream message; + message = pgp.DecryptAndVerifyInMemory(text, a, "Sample_Pub.asc", "Sample_Pri.asc", pass); + MemoryStream ms = new MemoryStream(); ; + message.CopyTo(ms); + ms = new MemoryStream(a.ToArray()); + StreamReader reader = new StreamReader(ms); + return reader.ReadToEnd(); + + } } } + + } } From 72b071b29f38aee0a0b75bc42c51b4535b8d62d3 Mon Sep 17 00:00:00 2001 From: Mateo Date: Fri, 15 Mar 2019 15:19:40 +0100 Subject: [PATCH 3/4] Clean Libraries --- ChoPGP/ChoPGPEncryptDecrypt.cs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/ChoPGP/ChoPGPEncryptDecrypt.cs b/ChoPGP/ChoPGPEncryptDecrypt.cs index e4e7389..cf9ce8b 100644 --- a/ChoPGP/ChoPGPEncryptDecrypt.cs +++ b/ChoPGP/ChoPGPEncryptDecrypt.cs @@ -1,23 +1,17 @@ -using Org.BouncyCastle.Asn1.Pkcs; -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Bcpg; +using Org.BouncyCastle.Bcpg; using Org.BouncyCastle.Bcpg.OpenPgp; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; -using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.IO; -using Org.BouncyCastle.X509; using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Text; using System.Threading.Tasks; -using System.Linq; namespace Cinchoo.PGP { @@ -316,7 +310,7 @@ await Task.Run(() => EncryptAndSignAsync(inputStream, outputStream, File.OpenRea File.OpenRead(privateKeyFilePath), passPhrase, armor, withIntegrityCheck)); } - public async Task EncryptAndSignAsync(Stream inputStream, Stream outputStream, Stream publicKeyStream, + public async Task EncryptAndSignAsync(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase, bool armor = true, bool withIntegrityCheck = true) { await Task.Run(() => EncryptAndSignAsync(inputStream, outputStream, publicKeyStream, @@ -458,9 +452,9 @@ private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, ChoPG return pgpSignatureGenerator; } -#endregion Encrypt and Sign + #endregion Encrypt and Sign -#region DecryptFile + #region DecryptFile public async Task DecryptFileAsync(string inputFilePath, string outputFilePath, string privateKeyFilePath, string passPhrase) { @@ -500,9 +494,9 @@ public void DecryptFile(string inputFilePath, string outputFilePath, string priv } } -#endregion DecryptFile + #endregion DecryptFile -#region Decrypt + #region Decrypt public async Task DecryptAsync(Stream inputStream, Stream outputStream, string privateKeyFilePath, string passPhrase) { @@ -920,7 +914,7 @@ public void Decrypt(Stream inputStream, Stream outputStream, Stream privateKeySt PgpObject obj = null; if (objFactory != null) obj = objFactory.NextPgpObject(); - + // the first object might be a PGP marker packet. @@ -999,7 +993,7 @@ public void Decrypt(Stream inputStream, Stream outputStream, Stream privateKeySt throw new PgpException("Message is not a simple encrypted file."); } - + #endregion Decrypt @@ -1073,9 +1067,9 @@ public void DecryptFileAndVerify(string inputFilePath, string outputFilePath, st return; } -#endregion DecryptFileAndVerify + #endregion DecryptFileAndVerify -#region DecryptAndVerify + #region DecryptAndVerify public async Task DecryptAndVerifyAsync(Stream inputStream, Stream outputStream, string publicKeyFilePath, string privateKeyFilePath, string passPhrase) { @@ -1223,9 +1217,9 @@ public void GenerateKey(Stream publicKeyStream, Stream privateKeyStream, string ExportKeyPair(privateKeyStream, publicKeyStream, kp.Public, kp.Private, username, password.ToCharArray(), armor); } -#endregion GenerateKey + #endregion GenerateKey -#region Private helpers + #region Private helpers private string GetFileName(Stream stream) { @@ -1442,7 +1436,7 @@ public void Dispose() { } -#endregion Private helpers + #endregion Private helpers } } From 6b24d316ad1a447ecf248f15872afe26f5cb92bb Mon Sep 17 00:00:00 2001 From: Mateo Date: Fri, 15 Mar 2019 15:25:07 +0100 Subject: [PATCH 4/4] Clean code --- ChoPGP/ChoPGPEncryptDecrypt.cs | 98 ---------------------------------- UnitTestProject1/UnitTest1.cs | 10 ---- 2 files changed, 108 deletions(-) diff --git a/ChoPGP/ChoPGPEncryptDecrypt.cs b/ChoPGP/ChoPGPEncryptDecrypt.cs index cf9ce8b..6a6c560 100644 --- a/ChoPGP/ChoPGPEncryptDecrypt.cs +++ b/ChoPGP/ChoPGPEncryptDecrypt.cs @@ -575,36 +575,11 @@ public byte[] DecryptInMemory(byte[] inputData, Stream keyIn, string passCode) else throw new PgpException("message is not a simple encrypted file - type unknown."); - //if (pbe.IsIntegrityProtected()) - //{ - // if (!pbe.Verify()) - // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - // else - // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} - //else - //{ - // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} - return decoded.ToArray(); } catch (Exception e) { throw e; - //if (e.Message.StartsWith("Checksum mismatch")) - // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Object reference not")) - // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Premature end of stream")) - // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else - // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //Exception underlyingException = e.InnerException; - //if (underlyingException != null) - // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - - //return error; } } @@ -670,36 +645,12 @@ public MemoryStream DecryptInMemoryMS(byte[] inputData, Stream keyIn, string pas else throw new PgpException("message is not a simple encrypted file - type unknown."); - //if (pbe.IsIntegrityProtected()) - //{ - // if (!pbe.Verify()) - // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - // else - // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} - //else - //{ - // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} return decoded; } catch (Exception e) { throw e; - //if (e.Message.StartsWith("Checksum mismatch")) - // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Object reference not")) - // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Premature end of stream")) - // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else - // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //Exception underlyingException = e.InnerException; - //if (underlyingException != null) - // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - - //return error; } } @@ -765,36 +716,11 @@ public MemoryStream DecryptInMemoryMS(Stream inputData, Stream keyIn, string pas else throw new PgpException("message is not a simple encrypted file - type unknown."); - //if (pbe.IsIntegrityProtected()) - //{ - // if (!pbe.Verify()) - // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - // else - // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} - //else - //{ - // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} - return decoded; } catch (Exception e) { throw e; - //if (e.Message.StartsWith("Checksum mismatch")) - // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Object reference not")) - // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Premature end of stream")) - // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else - // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //Exception underlyingException = e.InnerException; - //if (underlyingException != null) - // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - - //return error; } } @@ -860,36 +786,12 @@ public string DecryptInMemoryToString(Stream inputData, Stream keyIn, string pas else throw new PgpException("message is not a simple encrypted file - type unknown."); - //if (pbe.IsIntegrityProtected()) - //{ - // if (!pbe.Verify()) - // //MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - // else - // //MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} - //else - //{ - // //MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - //} StreamReader reader = new StreamReader(decoded); return reader.ReadToEnd(); } catch (Exception e) { throw e; - //if (e.Message.StartsWith("Checksum mismatch")) - // MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Object reference not")) - // MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else if (e.Message.StartsWith("Premature end of stream")) - // MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //else - // MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - //Exception underlyingException = e.InnerException; - //if (underlyingException != null) - // MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - - //return error; } } diff --git a/UnitTestProject1/UnitTest1.cs b/UnitTestProject1/UnitTest1.cs index 3855d26..1fd5eb7 100644 --- a/UnitTestProject1/UnitTest1.cs +++ b/UnitTestProject1/UnitTest1.cs @@ -77,7 +77,6 @@ public void Encrypt() { using (Stream output = File.OpenWrite("SampleData.PGP")) { - //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; pgp.Encrypt(input, output, "Sample_Pub.asc", true, false); } } @@ -105,7 +104,6 @@ public void EncryptFileNSign() { using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { - //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; pgp.EncryptFileAndSign("SampleData.txt", "SampleData.PGP", "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false); Console.WriteLine("PGP Encryption done."); } @@ -129,7 +127,6 @@ public void EncryptNSign() { using (Stream output = File.OpenWrite("SampleData.PGP")) { - //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; pgp.EncryptAndSign(input, output, "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false); } } @@ -165,8 +162,6 @@ private MemoryStream EncryptString(string text) { using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { - // pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false); - // pgp.DecryptFile("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123"); MemoryStream a = new System.IO.MemoryStream(); byte[] byteArray = Encoding.UTF8.GetBytes(text); @@ -185,8 +180,6 @@ private string DecryptString(MemoryStream text, string pass) { using (MemoryStream a = new System.IO.MemoryStream()) { - - //pgp.Decrypt(text, a, "Sample_Pri.asc", "Test123"); byte[] r = pgp.DecryptInMemory(text.ToArray(), File.OpenRead("Sample_Pri.asc"), pass); MemoryStream ms = new MemoryStream(r); StreamReader reader = new StreamReader(ms); @@ -213,7 +206,6 @@ private MemoryStream EncryptNSignInMemory(string text) byte[] byteArray = Encoding.UTF8.GetBytes(text); MemoryStream stream = new MemoryStream(byteArray); - //pgp.CompressionAlgorithm = ChoCompressionAlgorithm.Zip; pgp.EncryptAndSign(stream, a, "Sample_Pub.asc", "Sample_Pri.asc", "Test123", true, false); return a; @@ -226,8 +218,6 @@ private string DecryptNVerifyInMemory(MemoryStream text, string pass) { using (MemoryStream a = new System.IO.MemoryStream()) { - - //pgp.Decrypt(text, a, "Sample_Pri.asc", "Test123"); Stream message; message = pgp.DecryptAndVerifyInMemory(text, a, "Sample_Pub.asc", "Sample_Pri.asc", pass); MemoryStream ms = new MemoryStream(); ;