/// <summary>
        /// Decrypts a stream using OpenPGP standards.  Only
        /// the organization holding the corresponding private key
        /// can decrypt the data.
        /// </summary>
        /// <param name="inStream">input stream to decrypt</param>
        /// <param name="outStream">decrypted output stream</param>
        /// <param name="privateKey">The name of the private key stored in a database</param>
        /// <param name="privateKeyName"></param>
        /// <param name="compressionType">none,gzip,zlib,bzip2,zip</param>
        public static void Decrypt(Stream inStream, Stream outStream,
                                   byte[] privateKey, string passphrase,
                                   CompressionType compressionType = CompressionType.None,
                                   bool armor = false)
        {
            var compressionAlgorithmTag = ParseCompressionAlgorithm(compressionType);

            using (var pgp = new PGP()) {
                using (var outputStream = new MemoryStream()) {
                    using (var privateKeyStream = new MemoryStream(privateKey)) {
                        pgp.CompressionAlgorithm = compressionAlgorithmTag;
                        pgp.DecryptStream(inStream, outputStream, privateKeyStream, passphrase);
                    }

                    outputStream.Seek(0, SeekOrigin.Begin);

                    if (compressionType == CompressionType.GZip)
                    {
                        UnGzip(outputStream, outStream);
                    }
                    else
                    {
                        outputStream.CopyTo(outStream);
                    }
                }
            }
        }
Beispiel #2
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();
        }
Beispiel #3
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();
        }
Beispiel #4
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");
                // Encrypt file
                pgp.EncryptFile(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted.pgp", @"C:\TEMP\keys\public.asc", 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);
                // Decrypt file
                pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted.pgp", @"C:\TEMP\keys\content__decrypted.txt", @"C:\TEMP\keys\private.asc", "password");
                // 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");

                // 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");
            }
        }
Beispiel #5
0
 public MemoryStream DescryptFileAsStream(string encryptedFile)
 {
     using var outputFileStream    = new MemoryStream();
     using Stream inputFileStream  = File.OpenRead(encryptedFile);
     using Stream privateKeyStream = File.OpenRead(_appSettings.Value.PgpPrivateKey);
     _pgpCore.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, _appSettings.Value.PgpPassShared);
     return(new MemoryStream(outputFileStream.ToArray()));
 }
Beispiel #6
0
        public void DecryptStream_DecryptEncryptedStreamWithMultipleKeys(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))
                            pgp.EncryptStream(inputFileStream, outputFileStream, new List <Stream>()
                            {
                                publicKeyStream1, publicKeyStream2
                            });

            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);

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

            string decryptedContent1 = File.ReadAllText(testFactory.DecryptedContentFilePath);
            string decryptedContent2 = File.ReadAllText(testFactory2.DecryptedContentFilePath);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));
            Assert.True(File.Exists(testFactory.DecryptedContentFilePath));
            Assert.True(File.Exists(testFactory2.DecryptedContentFilePath));
            Assert.Equal(testFactory.Content, decryptedContent1.Trim());
            Assert.Equal(testFactory.Content, decryptedContent2.Trim());

            // Teardown
            testFactory.Teardown();
        }
Beispiel #7
0
 public void DecryptFile(string inputFilePath, string outputFilePath, string privateKey, string password)
 {
     using (var pgp = new PGP())
     {
         using (FileStream inputFileStream =
                    new FileStream(inputFilePath, FileMode.Open))
             using (Stream outputFileStream = File.Create(outputFilePath))
                 using (Stream privateKeyStream = GenerateStreamFromString(privateKey))
                     pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, password);
     }
 }
Beispiel #8
0
        public void DecryptStream_DecryptEncryptedStreamWithMultipleKeys(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 publicKeyStream1 = new FileStream(publicKeyFilePath1, FileMode.Open))
                        using (Stream publicKeyStream2 = new FileStream(publicKeyFilePath2, FileMode.Open))
                            pgp.EncryptStream(inputFileStream, outputFileStream, new List <Stream>()
                            {
                                publicKeyStream1, publicKeyStream2
                            });

            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);

            using (FileStream inputFileStream = new FileStream(encryptedContentFilePath, FileMode.Open))
                using (Stream outputFileStream = File.Create(decryptedContentFilePath2))
                    using (Stream privateKeyStream = new FileStream(privateKeyFilePath2, FileMode.Open))
                        pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, password2);

            string decryptedContent1 = File.ReadAllText(decryptedContentFilePath1);
            string decryptedContent2 = File.ReadAllText(decryptedContentFilePath2);

            // Assert
            Assert.True(File.Exists(encryptedContentFilePath));
            Assert.True(File.Exists(decryptedContentFilePath1));
            Assert.True(File.Exists(decryptedContentFilePath2));
            Assert.Equal(content, decryptedContent1.Trim());
            Assert.Equal(content, decryptedContent2.Trim());

            // Teardown
            Teardown();
        }
Beispiel #9
0
        /// <summary> Decrypt the given <paramref name="encrypted"/> payload with the given <paramref name="privateKey"/>. </summary>
        /// <param name="payload"> Payload to decrypt </param>
        /// <param name="privateKey"> Key to use </param>
        /// <returns> Decrypted string </returns>
        public static string Decrypt(string encrypted, string privateKey, string password = "******")
        {
            MemoryStream ins  = new MemoryStream(encrypted.ToBytesUTF8());
            MemoryStream outs = new MemoryStream();
            MemoryStream keys = new MemoryStream(privateKey.ToBytesUTF8());

            DateTime start = DateTime.UtcNow;

            instance.DecryptStream(ins, outs, keys, password);
            DateTime end = DateTime.UtcNow;

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

            return(Encoding.UTF8.GetString(outs.ToArray()).Replace("\r\n", "\n"));
        }
        public string Decrypt(string tenant, string input, string output)
        {
            var securityKeySource = _secretManager.AcquireKeys(tenant);
            var inputStream       = File.Open(input, FileMode.Open, FileAccess.ReadWrite);
            var outStream         = new MemoryStream();
            var privateKeyStream  = new MemoryStream(Encoding.UTF8.GetBytes(Regex.Unescape(securityKeySource.PrivateKeySource)));

            securityKeySource.PassPhrase = securityKeySource.PassPhrase == "0" ? null : securityKeySource.PassPhrase; // signify it's null/empty in Azure (we can't store null values)
            _pgp.DecryptStream(inputStream, outStream, privateKeyStream, "");
            inputStream.Dispose();
            using (var outputStream = File.Open(output, FileMode.Create, FileAccess.Write))
                outStream.WriteTo(outputStream);
            outStream.Dispose();
            return(output);
        }
Beispiel #11
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);
                            }
                        }
                    }
                }
            }
        }