private static Stream ChainEncryptedOut(Stream outputStream, PgpEncryptionKeys mEncryptionKeys)
        {
            PgpEncryptedDataGenerator encryptedDataGenerator;

            encryptedDataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.TripleDes,
                                                                   new SecureRandom());
            encryptedDataGenerator.AddMethod(mEncryptionKeys.PublicKey);
            return(encryptedDataGenerator.Open(outputStream, new byte[BufferSize]));
        }
        /*
         * Encrypt and sign the file pointed to by unencryptedFileInfo and
         */

        public static void EncryptAndSign(string inputFile, string outputFile, string publicKeyFile,
                                          string privateKeyFile, string passPhrase, bool armor)
        {
            var encryptionKeys = new PgpEncryptionKeys(publicKeyFile, privateKeyFile, passPhrase);

            if (!File.Exists(inputFile))
            {
                throw new FileNotFoundException($"Input file [{inputFile}] does not exist.");
            }

            if (!File.Exists(publicKeyFile))
            {
                throw new FileNotFoundException($"Public Key file [{publicKeyFile}] does not exist.");
            }

            if (!File.Exists(privateKeyFile))
            {
                throw new FileNotFoundException($"Private Key file [{privateKeyFile}] does not exist.");
            }

            if (String.IsNullOrEmpty(passPhrase))
            {
                throw new ArgumentNullException("Invalid Pass Phrase.");
            }

            if (encryptionKeys == null)
            {
                throw new ArgumentNullException("Encryption Key not found.");
            }

            using (Stream outputStream = File.Create(outputFile))
            {
                if (armor)
                {
                    using (var armoredOutputStream = new ArmoredOutputStream(outputStream))
                    {
                        OutputEncrypted(inputFile, armoredOutputStream, encryptionKeys);
                    }
                }
                else
                {
                    OutputEncrypted(inputFile, outputStream, encryptionKeys);
                }
            }
        }
 private static void OutputEncrypted(string inputFile, Stream outputStream, PgpEncryptionKeys encryptionKeys)
 {
     using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys))
     {
         var unencryptedFileInfo = new FileInfo(inputFile);
         using (Stream compressedOut = ChainCompressedOut(encryptedOut))
         {
             PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys);
             using (Stream literalOut = ChainLiteralOut(compressedOut, unencryptedFileInfo))
             {
                 using (FileStream inputFileStream = unencryptedFileInfo.OpenRead())
                 {
                     WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator);
                     inputFileStream.Close();
                 }
             }
         }
     }
 }
        private static PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut,
                                                                    PgpEncryptionKeys mEncryptionKeys)
        {
            const bool            isCritical = false;
            const bool            isNested   = false;
            PublicKeyAlgorithmTag tag        = mEncryptionKeys.SecretKey.PublicKey.Algorithm;
            var pgpSignatureGenerator        = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1);

            pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, mEncryptionKeys.PrivateKey);
            foreach (string userId in mEncryptionKeys.SecretKey.PublicKey.GetUserIds())
            {
                var subPacketGenerator = new PgpSignatureSubpacketGenerator();
                subPacketGenerator.SetSignerUserId(isCritical, userId);
                pgpSignatureGenerator.SetHashedSubpackets(subPacketGenerator.Generate());
                // Just the first one!
                break;
            }
            pgpSignatureGenerator.GenerateOnePassVersion(isNested).Encode(compressedOut);
            return(pgpSignatureGenerator);
        }
 private static void OutputEncrypted(string inputFile, Stream outputStream, PgpEncryptionKeys encryptionKeys)
 {
     using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys))
     {
         var unencryptedFileInfo = new FileInfo(inputFile);
         using (Stream compressedOut = ChainCompressedOut(encryptedOut))
         {
             PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys);
             using (Stream literalOut = ChainLiteralOut(compressedOut, unencryptedFileInfo))
             {
                 using (FileStream inputFileStream = unencryptedFileInfo.OpenRead())
                 {
                     WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator);
                     inputFileStream.Close();
                 }
             }
         }
     }
 }
 private static PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut,
     PgpEncryptionKeys mEncryptionKeys)
 {
     const bool isCritical = false;
     const bool isNested = false;
     PublicKeyAlgorithmTag tag = mEncryptionKeys.SecretKey.PublicKey.Algorithm;
     var pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1);
     pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, mEncryptionKeys.PrivateKey);
     foreach (string userId in mEncryptionKeys.SecretKey.PublicKey.GetUserIds())
     {
         var subPacketGenerator = new PgpSignatureSubpacketGenerator();
         subPacketGenerator.SetSignerUserId(isCritical, userId);
         pgpSignatureGenerator.SetHashedSubpackets(subPacketGenerator.Generate());
         // Just the first one!
         break;
     }
     pgpSignatureGenerator.GenerateOnePassVersion(isNested).Encode(compressedOut);
     return pgpSignatureGenerator;
 }
 private static Stream ChainEncryptedOut(Stream outputStream, PgpEncryptionKeys mEncryptionKeys)
 {
     PgpEncryptedDataGenerator encryptedDataGenerator;
     encryptedDataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.TripleDes,
         new SecureRandom());
     encryptedDataGenerator.AddMethod(mEncryptionKeys.PublicKey);
     return encryptedDataGenerator.Open(outputStream, new byte[BufferSize]);
 }
        /*
         * Encrypt and sign the file pointed to by unencryptedFileInfo and
         */
        public static void EncryptAndSign(string inputFile, string outputFile, string publicKeyFile,
            string privateKeyFile, string passPhrase, bool armor)
        {
            var encryptionKeys = new PgpEncryptionKeys(publicKeyFile, privateKeyFile, passPhrase);

            if (!File.Exists(inputFile))
                throw new FileNotFoundException(String.Format("Input file [{0}] does not exist.", inputFile));

            if (!File.Exists(publicKeyFile))
                throw new FileNotFoundException(String.Format("Public Key file [{0}] does not exist.", publicKeyFile));

            if (!File.Exists(privateKeyFile))
                throw new FileNotFoundException(String.Format("Private Key file [{0}] does not exist.", privateKeyFile));

            if (String.IsNullOrEmpty(passPhrase))
                throw new ArgumentNullException("Invalid Pass Phrase.");

            if (encryptionKeys == null)
                throw new ArgumentNullException("Encryption Key not found.");

            using (Stream outputStream = File.Create(outputFile))
            {
                if (armor)
                    using (var armoredOutputStream = new ArmoredOutputStream(outputStream))
                    {
                        OutputEncrypted(inputFile, armoredOutputStream, encryptionKeys);
                    }
                else
                    OutputEncrypted(inputFile, outputStream, encryptionKeys);
            }
        }