Exemplo n.º 1
0
        public void Can_Encrypt_And_Decrypt_Text_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalText.txt");

            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    AlwaysTrustPublicKey = false,
                    InputData            = origFs,
                    Operation            = DataOperation.Encrypt,
                    Recipient            = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData  = encryptStream,
                        Operation  = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                        using (StreamReader reader = new StreamReader(decryptedStream))
                        {
                            string origText  = File.ReadAllText(origFile);
                            string finalText = reader.ReadToEnd();
                            Assert.AreEqual(origText, finalText, "Roundtrip got diffent text.");
                        }
                }
            }
        }
Exemplo n.º 2
0
        public void List_Secret_Keys_Gets_Tester_Id()
        {
            IPgpTool tool = new GnuPGTool();
            var      keys = tool.ListKeys(KeyTarget.Secret).ToList();

            var hit = keys.FirstOrDefault(k => k.UserIds.Contains(TESTER_NAME));

            Assert.IsNotNull(hit, "Didn't find tester key id.");
        }
Exemplo n.º 3
0
        public void Can_Encrypt_And_Decrypt_Text_File()
        {
            string origFile      = Path.Combine(__samplesFolder, "OriginalText.txt");
            string encryptedFile = Path.Combine(__samplesFolder, "OriginalText_Encrypted.pgp");
            string decryptedFile = Path.Combine(__samplesFolder, "OriginalText_Decrypted.txt");

            IOUtility.DeleteFiles(encryptedFile, decryptedFile);

            var encryptArg = new FileDataInput
            {
                Armor = true,
                AlwaysTrustPublicKey = false,
                InputFile            = origFile,
                OutputFile           = encryptedFile,
                Operation            = DataOperation.Encrypt,
                Recipient            = TESTER_NAME,
            };

            IPgpTool tool = new GnuPGTool();

            tool.ProcessData(encryptArg);

            Assert.IsTrue(File.Exists(encryptedFile), "Encrypted file not found.");


            var decryptArg = new FileDataInput
            {
                InputFile  = encryptedFile,
                OutputFile = decryptedFile,
                Operation  = DataOperation.Decrypt,
                Passphrase = __passphrase
            };

            tool.ProcessData(decryptArg);

            Assert.IsTrue(File.Exists(decryptedFile), "Decrypted file not found.");

            string origText  = File.ReadAllText(origFile);
            string finalText = File.ReadAllText(decryptedFile);

            Assert.AreEqual(origText, finalText, "Roundtrip got diffent file.");
            IOUtility.DeleteFiles(encryptedFile, decryptedFile);
        }
Exemplo n.º 4
0
        public void Can_Encrypt_And_Decrypt_Binary_File()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalBinary.png");
            string encryptedFile = Path.Combine(__samplesFolder, "OriginalBinary_Encrypted.pgp");
            string decryptedFile = Path.Combine(__samplesFolder, "OriginalBinary_Decrypted.png");
            IOUtility.DeleteFiles(encryptedFile, decryptedFile);

            var encryptArg = new FileDataInput
            {
                Armor = true,
                AlwaysTrustPublicKey = false,
                InputFile = origFile,
                OutputFile = encryptedFile,
                Operation = DataOperation.Encrypt,
                Recipient = TESTER_NAME,
            };

            IPgpTool tool = new GnuPGTool();
            tool.ProcessData(encryptArg);

            Assert.IsTrue(File.Exists(encryptedFile), "Encrypted file not found.");

            var decryptArg = new FileDataInput
            {
                InputFile = encryptedFile,
                OutputFile = decryptedFile,
                Operation = DataOperation.Decrypt,
                Passphrase = __passphrase
            };
            tool.ProcessData(decryptArg);

            Assert.IsTrue(File.Exists(decryptedFile), "Decrypted file not found.");

            byte[] origBytes = File.ReadAllBytes(origFile);
            byte[] finalBytes = File.ReadAllBytes(decryptedFile);
            CollectionAssert.AreEqual(origBytes, finalBytes, "Roundtrip got diffent file.");
            IOUtility.DeleteFiles(encryptedFile, decryptedFile);
        }
Exemplo n.º 5
0
        public void Can_Encrypt_And_Decrypt_Binary_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalBinary.png");

            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    AlwaysTrustPublicKey = false,
                    InputData            = origFs,
                    Operation            = DataOperation.Encrypt,
                    Recipient            = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData  = encryptStream,
                        Operation  = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                        using (MemoryStream testStream = new MemoryStream())
                        {
                            decryptedStream.CopyTo(testStream);

                            byte[] origBytes  = File.ReadAllBytes(origFile);
                            byte[] finalBytes = testStream.ToArray();
                            CollectionAssert.AreEqual(origBytes, finalBytes, "Roundtrip got diffent bytes.");
                        }
                }
            }
        }
Exemplo n.º 6
0
        public void Can_Encrypt_And_Decrypt_Binary_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalBinary.png");
            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    InputData = origFs,
                    Operation = DataOperation.Encrypt,
                    Recipient = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData = encryptStream,
                        Operation = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                    using (MemoryStream testStream = new MemoryStream())
                    {
                        decryptedStream.CopyTo(testStream);

                        byte[] origBytes = File.ReadAllBytes(origFile);
                        byte[] finalBytes = testStream.ToArray();
                        CollectionAssert.AreEqual(origBytes, finalBytes, "Roundtrip got diffent bytes.");
                    }
                }
            }
        }
Exemplo n.º 7
0
        public void List_Secret_Keys_Gets_Tester_Id()
        {
            IPgpTool tool = new GnuPGTool();
            var keys = tool.ListKeys(KeyTarget.Secret).ToList();

            var hit = keys.FirstOrDefault(k => k.UserIds.Contains(TESTER_NAME));

            Assert.IsNotNull(hit, "Didn't find tester key id.");
        }
Exemplo n.º 8
0
        public void Can_Encrypt_And_Decrypt_Text_Stream()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalText.txt");
            using (var origFs = File.OpenRead(origFile))
            {
                var encryptArg = new StreamDataInput
                {
                    Armor = true,
                    InputData = origFs,
                    Operation = DataOperation.Encrypt,
                    Recipient = TESTER_NAME,
                };

                IPgpTool tool = new GnuPGTool();

                using (var encryptStream = tool.ProcessData(encryptArg))
                {
                    var decryptArg = new StreamDataInput
                    {
                        InputData = encryptStream,
                        Operation = DataOperation.Decrypt,
                        Passphrase = __passphrase
                    };
                    using (var decryptedStream = tool.ProcessData(decryptArg))
                    using (StreamReader reader = new StreamReader(decryptedStream))
                    {
                        string origText = File.ReadAllText(origFile);
                        string finalText = reader.ReadToEnd();
                        Assert.AreEqual(origText, finalText, "Roundtrip got diffent text.");
                    }
                }
            }
        }
Exemplo n.º 9
0
        public void Can_Encrypt_And_Decrypt_Text_File()
        {
            string origFile = Path.Combine(__samplesFolder, "OriginalText.txt");
            string encryptedFile = Path.Combine(__samplesFolder, "OriginalText_Encrypted.pgp");
            string decryptedFile = Path.Combine(__samplesFolder, "OriginalText_Decrypted.txt");
            IOUtility.DeleteFiles(encryptedFile, decryptedFile);

            var encryptArg = new FileDataInput
            {
                Armor = true,
                InputFile = origFile,
                OutputFile = encryptedFile,
                Operation = DataOperation.Encrypt,
                Recipient = TESTER_NAME,
            };

            IPgpTool tool = new GnuPGTool();
            tool.ProcessData(encryptArg);

            Assert.IsTrue(File.Exists(encryptedFile), "Encrypted file not found.");

            var decryptArg = new FileDataInput
            {
                InputFile = encryptedFile,
                OutputFile = decryptedFile,
                Operation = DataOperation.Decrypt,
                Passphrase = __passphrase
            };
            tool.ProcessData(decryptArg);

            Assert.IsTrue(File.Exists(decryptedFile), "Decrypted file not found.");

            string origText = File.ReadAllText(origFile);
            string finalText = File.ReadAllText(decryptedFile);
            Assert.AreEqual(origText, finalText, "Roundtrip got diffent file.");
            IOUtility.DeleteFiles(encryptedFile, decryptedFile);
        }