Example #1
0
        public void EncryptFileAndSign(string inputFilePath, string outputFilePath, string publicKeyFilePath,
                                       string privateKeyFilePath, string passPhrase, bool armor = true, bool withIntegrityCheck = true)
        {
            if (String.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentException(nameof(inputFilePath));
            }
            if (String.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentException(nameof(outputFilePath));
            }
            if (String.IsNullOrEmpty(publicKeyFilePath))
            {
                throw new ArgumentException(nameof(publicKeyFilePath));
            }
            if (String.IsNullOrEmpty(privateKeyFilePath))
            {
                throw new ArgumentException(nameof(privateKeyFilePath));
            }
            if (passPhrase == null)
            {
                passPhrase = String.Empty;
            }

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

            ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyFilePath, privateKeyFilePath, passPhrase);

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

            using (Stream outputStream = File.Create(outputFilePath))
            {
                if (armor)
                {
                    using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream))
                    {
                        OutputEncrypted(inputFilePath, armoredOutputStream, encryptionKeys, withIntegrityCheck);
                    }
                }
                else
                {
                    OutputEncrypted(inputFilePath, outputStream, encryptionKeys, withIntegrityCheck);
                }
            }
        }
Example #2
0
        private Stream ChainEncryptedOut(Stream outputStream, ChoPGPEncryptionKeys encryptionKeys, bool withIntegrityCheck)
        {
            PgpEncryptedDataGenerator encryptedDataGenerator;

            encryptedDataGenerator = new PgpEncryptedDataGenerator((SymmetricKeyAlgorithmTag)(int)SymmetricKeyAlgorithm, withIntegrityCheck, new SecureRandom());
            encryptedDataGenerator.AddMethod(encryptionKeys.PublicKey);
            return(encryptedDataGenerator.Open(outputStream, new byte[BufferSize]));
        }
Example #3
0
 private void OutputEncrypted(Stream inputStream, Stream outputStream, ChoPGPEncryptionKeys encryptionKeys, bool withIntegrityCheck, string name)
 {
     using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys, withIntegrityCheck))
     {
         using (Stream compressedOut = ChainCompressedOut(encryptedOut))
         {
             PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys);
             using (Stream literalOut = ChainLiteralStreamOut(compressedOut, inputStream, name))
             {
                 WriteOutputAndSign(compressedOut, literalOut, inputStream, signatureGenerator);
                 inputStream.Dispose();
             }
         }
     }
 }
Example #4
0
        private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, ChoPGPEncryptionKeys encryptionKeys)
        {
            PublicKeyAlgorithmTag tag = encryptionKeys.SecretKey.PublicKey.Algorithm;
            PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1);

            pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, encryptionKeys.PrivateKey);
            foreach (string userId in encryptionKeys.SecretKey.PublicKey.GetUserIds())
            {
                PgpSignatureSubpacketGenerator subPacketGenerator = new PgpSignatureSubpacketGenerator();
                subPacketGenerator.SetSignerUserId(false, userId);
                pgpSignatureGenerator.SetHashedSubpackets(subPacketGenerator.Generate());
                // Just the first one!
                break;
            }
            pgpSignatureGenerator.GenerateOnePassVersion(false).Encode(compressedOut);
            return(pgpSignatureGenerator);
        }
Example #5
0
        /// <summary>
        /// Encrypt and sign the file pointed to by unencryptedFileInfo and
        /// </summary>
        /// <param name="inputFilePath"></param>
        /// <param name="outputFilePath"></param>
        /// <param name="publicKeyFilePath"></param>
        /// <param name="privateKeyFilePath"></param>
        /// <param name="passPhrase"></param>
        /// <param name="armor"></param>
        public void EncryptAndSign(Stream inputStream, Stream outputStream, Stream publicKeyStream,
                                   Stream privateKeyStream, string passPhrase, bool armor = true, bool withIntegrityCheck = true)
        {
            if (inputStream == null)
            {
                throw new ArgumentException(nameof(inputStream));
            }
            if (outputStream == null)
            {
                throw new ArgumentException(nameof(outputStream));
            }
            if (publicKeyStream == null)
            {
                throw new ArgumentException(nameof(publicKeyStream));
            }
            if (privateKeyStream == null)
            {
                throw new ArgumentException(nameof(privateKeyStream));
            }
            if (passPhrase == null)
            {
                passPhrase = String.Empty;
            }

            ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyStream, privateKeyStream, passPhrase);

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

            if (armor)
            {
                using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream))
                {
                    OutputEncrypted(inputStream, armoredOutputStream, encryptionKeys, withIntegrityCheck, GetFileName(inputStream));
                }
            }
            else
            {
                OutputEncrypted(inputStream, outputStream, encryptionKeys, withIntegrityCheck, GetFileName(inputStream));
            }
        }
Example #6
0
 private void OutputEncrypted(string inputFilePath, Stream outputStream, ChoPGPEncryptionKeys encryptionKeys, bool withIntegrityCheck)
 {
     using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys, withIntegrityCheck))
     {
         FileInfo unencryptedFileInfo = new FileInfo(inputFilePath);
         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.Dispose();
                 }
             }
         }
     }
 }
Example #7
0
        public void DecryptAndVerify(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase)
        {
            if (inputStream == null)
            {
                throw new ArgumentException(nameof(inputStream));
            }
            if (outputStream == null)
            {
                throw new ArgumentException(nameof(outputStream));
            }
            if (publicKeyStream == null)
            {
                throw new ArgumentException(nameof(publicKeyStream));
            }
            if (privateKeyStream == null)
            {
                throw new ArgumentException(nameof(privateKeyStream));
            }
            if (passPhrase == null)
            {
                passPhrase = String.Empty;
            }

            ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyStream, privateKeyStream, passPhrase);

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

            PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKeyEncryptedData(inputStream);

            if (publicKeyED.KeyId != encryptionKeys.PublicKey.KeyId)
            {
                throw new PgpException(String.Format("Failed to verify file."));
            }

            PgpObject message = GetClearCompressedMessage(publicKeyED, encryptionKeys);

            if (message is PgpCompressedData)
            {
                message = ProcessCompressedMessage(message);
                PgpLiteralData literalData = (PgpLiteralData)message;
                using (Stream literalDataStream = literalData.GetInputStream())
                {
                    Streams.PipeAll(literalDataStream, outputStream);
                }
            }
            else if (message is PgpLiteralData)
            {
                PgpLiteralData literalData = (PgpLiteralData)message;
                using (Stream literalDataStream = literalData.GetInputStream())
                {
                    Streams.PipeAll(literalDataStream, outputStream);
                }
            }
            else
            {
                throw new PgpException("Message is not a simple encrypted file.");
            }

            return;
        }
Example #8
0
        public void DecryptFileAndVerify(string inputFilePath, string outputFilePath, string publicKeyFilePath, string privateKeyFilePath, string passPhrase)
        {
            if (String.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentException(nameof(inputFilePath));
            }
            if (String.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentException(nameof(outputFilePath));
            }
            if (String.IsNullOrEmpty(publicKeyFilePath))
            {
                throw new ArgumentException(nameof(publicKeyFilePath));
            }
            if (String.IsNullOrEmpty(privateKeyFilePath))
            {
                throw new ArgumentException(nameof(privateKeyFilePath));
            }
            if (passPhrase == null)
            {
                passPhrase = String.Empty;
            }

            if (!File.Exists(inputFilePath))
            {
                throw new FileNotFoundException(String.Format("Encrypted File [{0}] not found.", inputFilePath));
            }
            if (!File.Exists(publicKeyFilePath))
            {
                throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath));
            }
            if (!File.Exists(privateKeyFilePath))
            {
                throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath));
            }

            ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyFilePath, privateKeyFilePath, passPhrase);

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

            using (Stream inputStream = File.OpenRead(inputFilePath))
            {
                PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKeyEncryptedData(inputStream);
                if (publicKeyED.KeyId != encryptionKeys.PublicKey.KeyId)
                {
                    throw new PgpException(String.Format("Failed to verify file."));
                }

                PgpObject message = GetClearCompressedMessage(publicKeyED, encryptionKeys);

                if (message is PgpCompressedData)
                {
                    message = ProcessCompressedMessage(message);
                    PgpLiteralData literalData = (PgpLiteralData)message;
                    using (Stream outputFile = File.Create(outputFilePath))
                    {
                        using (Stream literalDataStream = literalData.GetInputStream())
                        {
                            Streams.PipeAll(literalDataStream, outputFile);
                        }
                    }
                }
                else if (message is PgpLiteralData)
                {
                    PgpLiteralData literalData = (PgpLiteralData)message;
                    using (Stream outputFile = File.Create(outputFilePath))
                    {
                        using (Stream literalDataStream = literalData.GetInputStream())
                        {
                            Streams.PipeAll(literalDataStream, outputFile);
                        }
                    }
                }
                else
                {
                    throw new PgpException("Message is not a simple encrypted file.");
                }
            }

            return;
        }
Example #9
0
        private PgpObject GetClearCompressedMessage(PgpPublicKeyEncryptedData publicKeyED, ChoPGPEncryptionKeys encryptionKeys)
        {
            PgpObjectFactory clearFactory = GetClearDataStream(encryptionKeys.PrivateKey, publicKeyED);
            PgpObject        message      = clearFactory.NextPgpObject();

            if (message is PgpOnePassSignatureList)
            {
                message = clearFactory.NextPgpObject();
            }
            return(message);
        }