コード例 #1
0
        public void TestNoSecretKeyToDecrypt()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            //Encrypt
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _vantivPublicKeyId);

            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, _passphrase);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure that your merchant secret key is added to your gpg keyring."));
            }
        }
コード例 #2
0
        public void TestInvalidPublicKeyId()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            try
            {
                PgpHelper.EncryptFile(testFilepath, encryptedFilePath, "BadPublicKeyId");
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure that the recipient Key ID is correct and is added to your gpg keyring"));
            }
        }
コード例 #3
0
        public void TestInvalidPassphrase()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            //Encrypt
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _merchantPublickeyId);

            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, "bad_passphrase");
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Console.WriteLine(e.Message);
                Assert.True(e.Message.Contains("Please make sure that the passphrase is correct."));
            }
        }
コード例 #4
0
        public PgpFileTests()
        {
            var keyGenerator = PgpHelper.GenerateKeyRingGenerator("test", "test");

            _publicKey = keyGenerator
                         .GeneratePublicKeyRing()
                         .GetPublicKeys()
                         .OfType <PgpPublicKey>()
                         .FirstOrDefault(p => p.IsEncryptionKey);

            _privateKey = keyGenerator.GenerateSecretKeyRing();
        }
コード例 #5
0
        public void TestNonExistantFileToDecrypt()
        {
            var encryptedFilePath = "bad_file_path";
            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, _passphrase);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure the input file exists and has read permission."));
            }
        }
コード例 #6
0
        public void TestNonExistantFileToEncrypt()
        {
            var testFilepath      = "bad_file_path";
            var encryptedFilePath = Path.Combine(_testDir, "test_pgp.asc");

            try
            {
                PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _merchantPublickeyId);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure the input file exists and has read permission."));
            }
        }
コード例 #7
0
        public void TestNonExistentFileToDecrypt()
        {
            var encryptedFilePath = "bad_file_path";
            var decryptedFilePath = Path.Combine(this.testDir, "test_pgp_decrypted.txt");

            // Assert that encrypting a file throws the correct exception.
            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, this.passphrase);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure the input file exists and has read permission."), "Actual error message: " + e.Message);
            }
        }
コード例 #8
0
        public static GpgKey FromFile(string publicKeyPath, string privateKeyPath = null)
        {
            var key = new GpgKey {
                ArmoredKey = File.ReadAllText(publicKeyPath)
            };

            // retrieve the fingerprint (why does it have to be so hard?)
            // TODO: find out a way to do this and avoid file path part
            var contents = PgpCore.Utilities.ReadPublicKey(publicKeyPath).PublicKeyPacket.GetEncodedContents();

            key.Fingerprint = PgpHelper.FingerprintFromKeyPacket(contents);

            if (!string.IsNullOrWhiteSpace(privateKeyPath))
            {
                key.PrivateKey = File.ReadAllText(privateKeyPath);
            }

            return(key);
        }
コード例 #9
0
        public void TestEncryptionDecryption()
        {
            // Delete the existing file.
            var testFilepath = Path.Combine(this.testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            // Write a new file.
            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };
            File.WriteAllLines(testFilepath, lines);

            // Encrypt the file.
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, this.merchantPublickeyId);

            // Assert that the encrypted file has been created.
            var entries = Directory.EnumerateFiles(this.testDir);

            Assert.True(entries.Contains(encryptedFilePath));

            // Decrypt the file.
            var decryptedFilePath = Path.Combine(this.testDir, "test_pgp_decrypted.txt");

            PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, this.passphrase);

            // Assert that the decrypted file has been created.
            entries = Directory.EnumerateFiles(this.testDir);
            Assert.True(entries.Contains(decryptedFilePath));

            // Assert the file was encrypted and decrypted correctly.
            var original  = File.ReadAllLines(testFilepath);
            var decrypted = File.ReadAllLines(decryptedFilePath);

            Assert.AreEqual(original, decrypted);
        }
コード例 #10
0
        public void TestEncryptionDecryption()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            //Encrypt
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _merchantPublickeyId);

            // Check if encrypted file is created
            var entries = Directory.EnumerateFiles(_testDir);

            Assert.True(entries.Contains(encryptedFilePath));

            //Decrypt
            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, _passphrase);

            // Check if decrypted file is created
            entries = Directory.EnumerateFiles(_testDir);
            Assert.True(entries.Contains(decryptedFilePath));

            // Compare decrypted file with original file
            string[] original  = File.ReadAllLines(testFilepath);
            string[] decrypted = File.ReadAllLines(decryptedFilePath);
            Assert.AreEqual(original, decrypted);
        }