Esempio n. 1
0
        public void DecryptStream_DecryptSignedAndEncryptedStream(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory = new TestFactory();

            testFactory.Arrange(keyType, FileType.Known);
            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        using (Stream privateKeyStream = new FileStream(testFactory.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                            pgp.EncryptStreamAndSign(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, testFactory.Password);

            using (FileStream inputFileStream = new FileStream(testFactory.EncryptedContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.DecryptedContentFilePath))
                    using (Stream privateKeyStream = new FileStream(testFactory.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                        pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, testFactory.Password);

            string decryptedContent = File.ReadAllText(testFactory.DecryptedContentFilePath);

            bool verified = pgp.VerifyFile(testFactory.EncryptedContentFilePath, testFactory.PublicKeyFilePath);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));
            Assert.True(File.Exists(testFactory.DecryptedContentFilePath));
            Assert.Equal(testFactory.Content, decryptedContent.Trim());
            Assert.True(verified);

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 2
0
        public void Verify_DoNotVerifyEncryptedAndSignedStream(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory  = new TestFactory();
            TestFactory testFactory2 = new TestFactory();

            testFactory.Arrange(keyType, FileType.Known);
            testFactory2.Arrange(KeyType.Generated, FileType.Known);

            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        using (Stream privateKeyStream = new FileStream(testFactory.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                            pgp.EncryptStreamAndSign(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, testFactory.Password);

            bool verified = pgp.VerifyFile(testFactory.EncryptedContentFilePath, testFactory2.PublicKeyFilePath);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));
            Assert.False(verified);

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 3
0
        public void EncryptStreamAndSign_CreateEncryptedAndSignedStreamWithMultipleKeys(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory  = new TestFactory();
            TestFactory testFactory2 = new TestFactory();

            testFactory.Arrange(keyType, FileType.Known);
            testFactory2.Arrange(KeyType.Generated, FileType.Known);

            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream1 = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        using (Stream publicKeyStream2 = new FileStream(testFactory2.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                            using (Stream privateKeyStream = new FileStream(testFactory.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                                pgp.EncryptStreamAndSign(inputFileStream, outputFileStream, new List <Stream>()
                                {
                                    publicKeyStream1, publicKeyStream2
                                }, privateKeyStream, testFactory.Password);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 4
0
        public void DecryptStream_DecryptSignedAndEncryptedStream(KeyType keyType)
        {
            // Arrange
            Arrange(keyType);
            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(contentFilePath, FileMode.Open))
                using (Stream outputFileStream = File.Create(encryptedContentFilePath))
                    using (Stream publicKeyStream = new FileStream(publicKeyFilePath1, FileMode.Open))
                        using (Stream privateKeyStream = new FileStream(privateKeyFilePath1, FileMode.Open))
                            pgp.EncryptStreamAndSign(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, password1);

            using (FileStream inputFileStream = new FileStream(encryptedContentFilePath, FileMode.Open))
                using (Stream outputFileStream = File.Create(decryptedContentFilePath1))
                    using (Stream privateKeyStream = new FileStream(privateKeyFilePath1, FileMode.Open))
                        pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, password1);

            string decryptedContent = File.ReadAllText(decryptedContentFilePath1);

            bool verified = pgp.VerifyFile(encryptedContentFilePath, publicKeyFilePath1);

            // Assert
            Assert.True(File.Exists(encryptedContentFilePath));
            Assert.True(File.Exists(decryptedContentFilePath1));
            Assert.Equal(content, decryptedContent.Trim());
            Assert.True(verified);

            // Teardown
            Teardown();
        }
Esempio n. 5
0
        /// <summary> Encrypt and sign the given <paramref name="payload"/> with the given <paramref name="publicKey"/> and <paramref name="privateKey"/>. </summary>
        /// <param name="payload"> Payload to encrypt/sign</param>
        /// <param name="publicKey"> Public key to encrypt with </param>
        /// <param name="privateKey"> Private key to sign with </param>
        /// <param name="password"> password to use.</param>
        /// <returns> Encrypted and signed payload. </returns>
        public static string EncryptAndSign(string payload, string publicKey, string privateKey, string password = "******")
        {
            MemoryStream ins         = new MemoryStream(payload.ToBytesUTF8());
            MemoryStream outs        = new MemoryStream();
            MemoryStream publicKeys  = new MemoryStream(publicKey.ToBytesUTF8());
            MemoryStream privateKeys = new MemoryStream(privateKey.ToBytesUTF8());

            DateTime start = DateTime.UtcNow;

            instance.EncryptStreamAndSign(ins, outs, publicKeys, privateKeys, password, true, true);
            DateTime end = DateTime.UtcNow;

            Log.Debug($"Pgp.EncryptAndSign took {(end - start).TotalMilliseconds}ms");

            return(Encoding.UTF8.GetString(outs.ToArray()).Replace("\r\n", "\n"));
        }
Esempio n. 6
0
        public void EncryptStreamAndSign_CreateEncryptedAndSignedStream(KeyType keyType)
        {
            // Arrange
            Arrange(keyType);
            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(contentFilePath, FileMode.Open))
                using (Stream outputFileStream = File.Create(encryptedContentFilePath))
                    using (Stream publicKeyStream = new FileStream(publicKeyFilePath1, FileMode.Open))
                        using (Stream privateKeyStream = new FileStream(privateKeyFilePath1, FileMode.Open))
                            pgp.EncryptStreamAndSign(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, password1);

            // Assert
            Assert.True(File.Exists(encryptedContentFilePath));

            // Teardown
            Teardown();
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            using (PGP pgp = new PGP())
            {
                // Generate keys
                pgp.GenerateKey(@"C:\TEMP\keys\public.asc", @"C:\TEMP\keys\private.asc", "*****@*****.**", "password");
                pgp.GenerateKey(@"C:\TEMP\keys\public2.asc", @"C:\TEMP\keys\private2.asc", "*****@*****.**", "password2");
                // Encrypt file
                pgp.EncryptFile(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted.pgp", @"C:\TEMP\keys\public.asc", true, true);
                // Encrypt file with multiple keys
                string[] publicKeys = { @"C:\TEMP\keys\public.asc", @"C:\TEMP\keys\public2.asc" };
                pgp.EncryptFile(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted_multiple.pgp", publicKeys, true, true);
                // Encrypt and sign file
                pgp.EncryptFileAndSign(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted_signed.pgp", @"C:\TEMP\keys\public.asc", @"C:\TEMP\keys\private.asc", "password", true, true);
                // Encrypt and sign multiple file
                pgp.EncryptFileAndSign(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted_signed_multiple.pgp", publicKeys, @"C:\TEMP\keys\private.asc", "password", true, true);
                // Decrypt file
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted.pgp", @"C:\TEMP\keys\content__decrypted.txt", @"C:\TEMP\keys\private.asc", "password");
                // Decrypt multiple file
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted_multiple.pgp", @"C:\TEMP\keys\content__decrypted_multiple.txt", @"C:\TEMP\keys\private.asc", "password");
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted_multiple.pgp", @"C:\TEMP\keys\content__decrypted_multiple2.txt", @"C:\TEMP\keys\private2.asc", "password2");
                // Decrypt signed file
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted_signed.pgp", @"C:\TEMP\keys\content__decrypted_signed.txt", @"C:\TEMP\keys\private.asc", "password");
                // Decrypt signed multiple file
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted_signed_multiple.pgp", @"C:\TEMP\keys\content__decrypted_signed_multiple.txt", @"C:\TEMP\keys\private.asc", "password");
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted_signed_multiple.pgp", @"C:\TEMP\keys\content__decrypted_signed_multiple2.txt", @"C:\TEMP\keys\private2.asc", "password2");

                // Encrypt stream
                using (FileStream inputFileStream = new FileStream(@"C:\TEMP\keys\content.txt", FileMode.Open))
                    using (Stream outputFileStream = File.Create(@"C:\TEMP\keys\content__encrypted2.pgp"))
                        using (Stream publicKeyStream = new FileStream(@"C:\TEMP\keys\public.asc", FileMode.Open))
                            pgp.EncryptStream(inputFileStream, outputFileStream, publicKeyStream, true, true);

                // Decrypt stream
                using (FileStream inputFileStream = new FileStream(@"C:\TEMP\keys\content__encrypted2.pgp", FileMode.Open))
                    using (Stream outputFileStream = File.Create(@"C:\TEMP\keys\content__decrypted2.txt"))
                        using (Stream privateKeyStream = new FileStream(@"C:\TEMP\keys\private.asc", FileMode.Open))
                            pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, "password");

                // Encrypt and decrypt streams
                using (Stream inputFileStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Streaming test message")))
                {
                    using (Stream publicKeyStream = new FileStream(@"C:\TEMP\keys\public.asc", FileMode.Open))
                    {
                        using (Stream encryptedMemoryStream = new MemoryStream())
                        {
                            pgp.EncryptStream(inputFileStream, encryptedMemoryStream, publicKeyStream);
                            encryptedMemoryStream.Seek(0, SeekOrigin.Begin);
                            StreamReader encryptedReader = new StreamReader(encryptedMemoryStream);
                            // Reset stream to beginning
                            encryptedMemoryStream.Seek(0, SeekOrigin.Begin);
                            string encryptedText = encryptedReader.ReadToEnd();
                            Console.WriteLine(encryptedText);

                            // Reset stream to beginning again
                            // Only necessary as stream read to end above for demo output
                            encryptedMemoryStream.Seek(0, SeekOrigin.Begin);

                            using (Stream decryptedMemoryStream = new MemoryStream())
                            {
                                using (Stream privateKeyStream = new FileStream(@"C:\TEMP\keys\private.asc", FileMode.Open))
                                {
                                    pgp.DecryptStream(encryptedMemoryStream, decryptedMemoryStream, privateKeyStream, "password");
                                    decryptedMemoryStream.Seek(0, SeekOrigin.Begin);
                                    StreamReader decryptedReader = new StreamReader(decryptedMemoryStream);
                                    string       decryptedText   = decryptedReader.ReadToEnd();
                                    Console.WriteLine(decryptedText);
                                }
                            }
                        }
                    }
                }

                // Encrypt key and sign stream
                using (Stream inputFileStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Streaming signed test message")))
                {
                    using (Stream publicKeyStream = new FileStream(@"C:\TEMP\keys\public.asc", FileMode.Open))
                    {
                        using (Stream privateKeyStream = new FileStream(@"C:\TEMP\keys\private.asc", FileMode.Open))
                        {
                            using (Stream encryptedMemoryStream = new MemoryStream())
                            {
                                pgp.EncryptStreamAndSign(inputFileStream, encryptedMemoryStream, publicKeyStream, privateKeyStream, "password");
                                // Reset stream to beginning
                                encryptedMemoryStream.Seek(0, SeekOrigin.Begin);
                                StreamReader encryptedReader = new StreamReader(encryptedMemoryStream);
                                string       encryptedText   = encryptedReader.ReadToEnd();
                                Console.WriteLine(encryptedText);
                            }
                        }
                    }
                }
            }
        }