Example #1
0
        /*
         * 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)
        {
            EncryptionKeys encryptionKeys = new EncryptionKeys(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 (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream))
                    {
                        OutputEncrypted(inputFile, armoredOutputStream, encryptionKeys);
                    }
                }
                else
                {
                    OutputEncrypted(inputFile, outputStream, encryptionKeys);
                }
            }
        }
Example #2
0
 private static void OutputEncrypted(string inputFile, Stream outputStream, EncryptionKeys encryptionKeys)
 {
     using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys))
     {
         FileInfo 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();
                 }
             }
         }
     }
 }
Example #3
0
        private static PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, EncryptionKeys m_encryptionKeys)
        {
            const bool            IsCritical            = false;
            const bool            IsNested              = false;
            PublicKeyAlgorithmTag tag                   = m_encryptionKeys.SecretKey.PublicKey.Algorithm;
            PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1);

            pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, m_encryptionKeys.PrivateKey);
            foreach (string userId in m_encryptionKeys.SecretKey.PublicKey.GetUserIds())
            {
                PgpSignatureSubpacketGenerator subPacketGenerator = new PgpSignatureSubpacketGenerator();
                subPacketGenerator.SetSignerUserId(IsCritical, userId);
                pgpSignatureGenerator.SetHashedSubpackets(subPacketGenerator.Generate());
                // Just the first one!
                break;
            }
            pgpSignatureGenerator.GenerateOnePassVersion(IsNested).Encode(compressedOut);
            return(pgpSignatureGenerator);
        }