Exemple #1
0
        private Stream ChainEncryptedOut(Stream outputStream, PGP.EncryptionKeys 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]));
        }
Exemple #2
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 EncryptFileAndSign(string inputFilePath, string outputFilePath, string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor = true, bool withIntegrityCheck = true)
        {
            if (string.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentException("InputFilePath");
            }
            if (string.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentException("OutputFilePath");
            }
            if (string.IsNullOrEmpty(publicKeyFilePath))
            {
                throw new ArgumentException("PublicKeyFilePath");
            }
            if (string.IsNullOrEmpty(privateKeyFilePath))
            {
                throw new ArgumentException("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));
            }

            PGP.EncryptionKeys encryptionKeys = new PGP.EncryptionKeys(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);
                }
            }
        }
Exemple #3
0
        private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, PGP.EncryptionKeys 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);
        }
Exemple #4
0
 private void OutputEncrypted(string inputFilePath, Stream outputStream, PGP.EncryptionKeys 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.Close();
                 }
             }
         }
     }
 }
        internal static PgpObject GetClearCompressedMessage(PgpPublicKeyEncryptedData publicKeyED, PGP.EncryptionKeys encryptionKeys)
        {
            PgpObjectFactory clearFactory = GetClearDataStream(encryptionKeys.PrivateKey, publicKeyED);
            PgpObject        message      = clearFactory.NextPgpObject();

            if (message is PgpOnePassSignatureList)
            {
                message = clearFactory.NextPgpObject();
            }
            return(message);
        }
Exemple #6
0
        public void DecryptFileAndVerify(string inputFilePath, string outputFilePath, string publicKeyFilePath, string privateKeyFilePath, string passPhrase)
        {
            if (string.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentException("InputFilePath");
            }
            if (string.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentException("OutputFilePath");
            }
            if (string.IsNullOrEmpty(publicKeyFilePath))
            {
                throw new ArgumentException("PublicKeyFilePath");
            }
            if (string.IsNullOrEmpty(privateKeyFilePath))
            {
                throw new ArgumentException("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));
            }

            PGP.EncryptionKeys encryptionKeys = new PGP.EncryptionKeys(publicKeyFilePath, privateKeyFilePath, passPhrase);

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

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

                PgpObject message = PGPObjectHelper.GetClearCompressedMessage(publicKeyED, encryptionKeys);

                if (message is PgpCompressedData)
                {
                    message = PGPObjectHelper.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;
        }