Ejemplo n.º 1
0
        public PgpEncryptedDataList(
            BcpgInputStream bcpgInput)
        {
            while (bcpgInput.NextPacketTag() == PacketTag.PublicKeyEncryptedSession ||
                   bcpgInput.NextPacketTag() == PacketTag.SymmetricKeyEncryptedSessionKey)
            {
                list.Add(bcpgInput.ReadPacket());
            }

            data = (InputStreamPacket)bcpgInput.ReadPacket();

            for (int i = 0; i != list.Count; i++)
            {
                if (list[i] is SymmetricKeyEncSessionPacket)
                {
                    list[i] = new PgpPbeEncryptedData((SymmetricKeyEncSessionPacket)list[i], data);
                }
                else
                {
                    list[i] = new PgpPublicKeyEncryptedData((PublicKeyEncSessionPacket)list[i], data);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decrypt the stream.
        /// </summary>
        /// <param name="decrypted">The stream containing the decrypted data.</param>
        /// <param name="input">The data to decrypt.</param>
        /// <param name="password">The password used for decryption.</param>
        /// <returns>Returns null if no integrity packet exists; false if message failed integrity check; true if message integrity check passed.</returns>
        public bool?Decrypt(System.IO.Stream decrypted, System.IO.Stream input, string password)
        {
            // Get decorder stream.
            input = Key.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(input);

            System.IO.Stream clear = null;
            System.IO.Stream unc   = null;

            try
            {
                // Load the encrypted input stream.
                Key.Bcpg.OpenPgp.PgpEncryptedDataList encryptedDataList = null;
                Key.Bcpg.OpenPgp.PgpObjectFactory     objectFactory     = new Key.Bcpg.OpenPgp.PgpObjectFactory(input);
                Key.Bcpg.OpenPgp.PgpObject            o = objectFactory.NextPgpObject();

                // The first object might be a PGP marker packet.
                if (o is Key.Bcpg.OpenPgp.PgpEncryptedDataList)
                {
                    // Get the data list.
                    encryptedDataList = (Key.Bcpg.OpenPgp.PgpEncryptedDataList)o;
                }
                else
                {
                    // Get the next object.
                    encryptedDataList = (Key.Bcpg.OpenPgp.PgpEncryptedDataList)objectFactory.NextPgpObject();
                }

                // Load the password.
                Key.Bcpg.OpenPgp.PgpPbeEncryptedData pbe = (Key.Bcpg.OpenPgp.PgpPbeEncryptedData)encryptedDataList[0];
                clear = pbe.GetDataStream(password.ToArray());
                Key.Bcpg.OpenPgp.PgpObjectFactory plainFact = new Key.Bcpg.OpenPgp.PgpObjectFactory(clear);
                Key.Bcpg.OpenPgp.PgpObject        message   = plainFact.NextPgpObject();

                // If message is compressed.
                if (message is Key.Bcpg.OpenPgp.PgpCompressedData)
                {
                    // Decompress the message.
                    Key.Bcpg.OpenPgp.PgpCompressedData cData   = (Key.Bcpg.OpenPgp.PgpCompressedData)message;
                    Key.Bcpg.OpenPgp.PgpObjectFactory  pgpFact = new Key.Bcpg.OpenPgp.PgpObjectFactory(cData.GetDataStream());
                    message = pgpFact.NextPgpObject();
                }

                // If the message is literal data.
                if (message is Key.Bcpg.OpenPgp.PgpLiteralData)
                {
                    Key.Bcpg.OpenPgp.PgpLiteralData ld = (Key.Bcpg.OpenPgp.PgpLiteralData)message;

                    // Get the file name of the embedded encrypted file.
                    string outFileName = ld.FileName;

                    // Write the ecrypted data file to the decrypted stream.
                    unc = ld.GetInputStream();
                    Key.Utilities.IO.Streams.PipeAll(unc, decrypted);
                }
                else if (message is Key.Bcpg.OpenPgp.PgpOnePassSignatureList)
                {
                    throw new Exception("encrypted message contains a signed message - not literal data.");
                }
                else
                {
                    throw new Exception("message is not a simple encrypted file - type unknown.");
                }

                // If the ecrypted file contains an integrity packet associated with it.
                if (pbe.IsIntegrityProtected())
                {
                    // If it has been verified.
                    if (!pbe.Verify())
                    {
                        // Message failed integrity check.
                        return(false);
                    }
                    else
                    {
                        // Message integrity check passed.
                        return(true);
                    }
                }

                // No integrity packet exists.
                return(null);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (clear != null)
                {
                    clear.Close();
                }

                if (unc != null)
                {
                    unc.Close();
                }
            }
        }