Пример #1
0
        /// <summary>
        /// Encrypts an input file given the provided Public Key File.
        /// </summary>
        /// <param name="outputFilePath">Path of new encrypted output file.</param>
        /// <param name="inputFilePath">Path of existing unencrypted input file.</param>
        /// <param name="publicKeyFilePath">Path of existing Pgp Public Key file.</param>
        /// <param name="armor">Use ASCII Armor</param>
        /// <param name="withIntegrityCheck">Include Integrity Check</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        public static void EncryptFile(string outputFilePath, string inputFilePath, string publicKeyFilePath, bool armor = true, bool withIntegrityCheck = true)
        {
            // Parameter Checks
            if (String.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentException("Output File Name Parameter is invalid.");
            }
            if (String.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentException("Input File Name Parameter is invalid.");
            }
            if (String.IsNullOrEmpty(publicKeyFilePath))
            {
                throw new ArgumentException("Public Key File Name Parameter is invalid.");
            }

            if (!File.Exists(inputFilePath))
            {
                throw new FileNotFoundException("Input File does not exist.");
            }
            if (!File.Exists(publicKeyFilePath))
            {
                throw new FileNotFoundException("Public Key File does not exist");
            }

            PgpPublicKey _publicKey = PgpCustomUtilities.ReadPublicKey(publicKeyFilePath);

            using (Stream output = File.Create(outputFilePath))
            {
                EncryptFile(output, inputFilePath, _publicKey, armor, withIntegrityCheck);
            }
        }
Пример #2
0
        /// <summary>
        /// Decrypts the Input File stream, given the Private Key File stream, to the specified Decrypted File Path.
        /// </summary>
        /// <param name="inputFileStream">File Stream of encrypted file.</param>
        /// <param name="privateKeyFileStream">File Stream of Private Key file.</param>
        /// <param name="password">Password that was used to encrypt the file.</param>
        /// <param name="decryptedFilePath">Path of the new decrypted file.</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="PgpException"></exception>
        public static void DecryptFile(Stream inputFileStream, Stream privateKeyFileStream, string password, string decryptedFilePath, PgpSecretKeyRingBundle bundle = null)
        {
            // Parameter Checks
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Password Parameter is invalid.");
            }
            if (String.IsNullOrEmpty(decryptedFilePath))
            {
                throw new ArgumentException("Decrypted File Name Parameter is invalid.");
            }

            inputFileStream = PgpUtilities.GetDecoderStream(inputFileStream);

            PgpObjectFactory     _pgpObjectFactory = new PgpObjectFactory(inputFileStream);
            PgpEncryptedDataList _pgpEncryptedDataList;

            PgpObject _pgpObject = _pgpObjectFactory.NextPgpObject();

            if (_pgpObject is PgpEncryptedDataList)
            {
                _pgpEncryptedDataList = (PgpEncryptedDataList)_pgpObject;
            }
            else
            {
                _pgpEncryptedDataList = (PgpEncryptedDataList)_pgpObjectFactory.NextPgpObject();
            }

            PgpPrivateKey             _pgpPrivateKey          = null;
            PgpPublicKeyEncryptedData _pgpPubKeyEncryptedData = null;
            PgpSecretKeyRingBundle    _pgpSecKeyRingBundle    = bundle ?? new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyFileStream));

            foreach (PgpPublicKeyEncryptedData _pgpPubKeyEncData in _pgpEncryptedDataList.GetEncryptedDataObjects())
            {
                _pgpPrivateKey = PgpCustomUtilities.FindSecretKey(_pgpSecKeyRingBundle, _pgpPubKeyEncData.KeyId, password.ToCharArray());

                if (_pgpPrivateKey != null)
                {
                    _pgpPubKeyEncryptedData = _pgpPubKeyEncData;
                    break;
                }
            }

            if (_pgpPrivateKey == null)
            {
                throw new ArgumentException("secret key for message not found.");
            }

            Stream _privateKeyFileStream = _pgpPubKeyEncryptedData.GetDataStream(_pgpPrivateKey);

            PgpObjectFactory _pgpObjectFactoryPrivateKey = new PgpObjectFactory(_privateKeyFileStream);

            PgpObject _pgpObjectMessage = null;

            var po = _pgpObjectFactoryPrivateKey.NextPgpObject();

            if (po is PgpLiteralData)
            {
                _pgpObjectMessage = (PgpLiteralData)po;
            }
            else if (po is PgpCompressedData)
            {
                var _pgpCompressedData = (PgpCompressedData)po;

                var _pgpObjectFactoryCompressedData = new PgpObjectFactory(_pgpCompressedData.GetDataStream());

                _pgpObjectMessage = _pgpObjectFactoryCompressedData.NextPgpObject();
            }


            if (_pgpObjectMessage is PgpLiteralData)
            {
                PgpLiteralData _pgpLiteralData = (PgpLiteralData)_pgpObjectMessage;

                //                string _outputFileName = _pgpLiteralData.FileName;
                using (var st = _pgpLiteralData.GetInputStream())
                {
                    using (var dst = File.Create(decryptedFilePath))
                    {
                        st.CopyTo(dst);
                    }
                }

                /*
                 *  string _outputFileDirectoryPath;
                 * Stream _outputFileStream;
                 * if (_outputFileName.Length == 0)
                 * {
                 *  _outputFileName = decryptedFilePath;
                 *  _outputFileStream = File.Create(_outputFileName);
                 * }
                 * else
                 * {
                 *  FileInfo _decryptedFileInfo = new FileInfo(decryptedFilePath);
                 *  _outputFileDirectoryPath = _decryptedFileInfo.DirectoryName;
                 *  _outputFileStream = File.Create(Path.Combine(_outputFileDirectoryPath, _outputFileName));
                 * }
                 *
                 * Stream _dataInputStream = _pgpLiteralData.GetInputStream();
                 * _dataInputStream.CopyTo(_outputFileStream);
                 * //                Streams.PipeAll(_dataInputStream, _outputFileStream);
                 * _outputFileStream.Close();
                 */
            }
            else if (_pgpObjectMessage is PgpOnePassSignatureList)
            {
                throw new PgpException("Encrypted message contains a signed message - not literal data.");
            }
            else
            {
                throw new PgpException("Message is not a simple encrypted file - type unknown.");
            }

            if (_pgpPubKeyEncryptedData.IsIntegrityProtected())
            {
                if (!_pgpPubKeyEncryptedData.Verify())
                {
                    throw new PgpException("Message failed integrity check.");
                }
            }
        }