示例#1
0
        /// <summary>
        /// Uncompresses the current compressed message and returns the message that
        /// was inside of the compressed message. Usually this should be a literal
        /// data message.
        /// </summary>
        /// <returns>Returns the message that was inside of the compressed message.
        /// Usually this should be a literal data message.</returns>
        /// <exception cref="System.Exception">Throws an exception if the content
        /// of the compressed message is not another valid message.</exception>
        /// <remarks>No remarks</remarks>
        public Message Uncompress()
        {
            if (!(pPackets[0] is CompressedDataPacket))
            {
                throw new System.Exception("You should never see this message. If you do, something in CompressedMessage went terribly wrong!");
            }

            CompressedDataPacket cdpPacket = (CompressedDataPacket)pPackets[0];

            Packet[] pContent = cdpPacket.Uncompress();

            // A compressed data packet can contain:

            // - a literal message
            LiteralMessage lmMessage = new LiteralMessage();

            try {
                int iPos = lmMessage.ParseMessage(pContent);
                return(lmMessage);
            } catch (Exception) {}

            // - a signed message
            SignedMessage smMessage = new SignedMessage();

            try {
                int iPos = smMessage.ParseMessage(pContent);
                return(smMessage);
            } catch (Exception) {}

            // TODO: Try to think of other packets that might
            // occur in a compressed data packet

            throw new Exception("The content of the compressed message does not appear to be a valid OpenPGP message!");
        }
示例#2
0
        /// <summary>
        /// Decrypts the encrypted message if it is a symmetrically encrypted
        /// message with the passphrase given as argument.
        /// </summary>
        /// <param name="strPassphrase">The passphrase that was used to encrypt
        /// the message</param>
        /// <returns>Returns the message that was encrypted. Usually this is
        /// an compressed or literal message.</returns>
        /// <remarks>No remarks</remarks>
        public Message Decrypt(string strPassphrase)
        {
            if (esKeys.SymKeys.Count == 0)
            {
                throw new Exception("This message is not symmetrically encrypted. Please provide a keyring rather than a passphrase!");
            }

            Packet[] pContent = new Packet[0];
            Packet[] pReturn  = new Packet[0];

            IEnumerator ieKeys = esKeys.SymKeys.GetEnumerator();

            while (ieKeys.MoveNext())
            {
                SymSessionKeyPacket skpKey = (SymSessionKeyPacket)ieKeys.Current;
                byte[] key = skpKey.S2KSpecifier.GetKey(strPassphrase, CipherHelper.CipherKeySize(skpKey.Algorithm));

                try {
                    SymmetricAlgorithm saAlgo = CipherHelper.CreateSymAlgorithm(skpKey.Algorithm);
                    pContent = sepData.Decrypt(key, saAlgo);
                } catch (System.Security.Cryptography.CryptographicException) {}
                if (pContent.Length > 0)
                {
                    pReturn = pContent;
                }
            }

            if (pReturn.Length == 0)
            {
                throw new System.Security.Cryptography.CryptographicException("Wrong passphrase!");
            }

            // now we need to look what kind of message was hidden in the
            // encrypted data

            // it can be either a literal message
            LiteralMessage lmLiteral = new LiteralMessage();

            try {
                int iPos = lmLiteral.ParseMessage(pReturn);
                return(lmLiteral);
            } catch (Exception) {}

            // or an compressed Message
            CompressedMessage cmCompressed = new CompressedMessage();

            try {
                int iPos = cmCompressed.ParseMessage(pReturn);
                return(cmCompressed);
            } catch (Exception) {}

            throw new System.ArgumentException("Encrypted package content is not a valid message!");
        }
示例#3
0
        /// <summary>
        /// Parses a signed message out of the given array of packets.
        /// In this special case, the first packet must be a signature
        /// packet or a one-pass signature packet. This is followed by
        /// an OpenPGP message and optionally (if the first packet was
        /// a one-pass signature packet) and ordinary signature packet.
        /// </summary>
        /// <returns>Returns the number of packets used by the function.
        /// </returns>
        /// <param name="packets">Array of packets that contains the
        /// signed message.</param>
        /// <remarks>No remarks</remarks>
        public override int ParseMessage(Packet[] packets)
        {
            if (packets[0] is OnePassSignaturePacket)
            {
                bOnePassSigned = true;
                opsOnePass     = (OnePassSignaturePacket)packets[0];
            }
            else if (packets[0] is SignaturePacket)
            {
                bOnePassSigned = false;
                spSignature    = (SignaturePacket)packets[0];
            }
            else
            {
                throw new System.ArgumentException("This does not appear to be a valid OpenPGP signed message!");
            }

            Packet[] pMessage = new Packet[packets.Length - 1];
            Array.Copy(packets, 1, pMessage, 0, pMessage.Length);
            lmSignedMessage = new LiteralMessage();
            int iPos = 0;

            try {
                iPos = lmSignedMessage.ParseMessage(pMessage);
            } catch (Exception) {}

            if (iPos == 0)
            {
                throw new System.ArgumentException("This does not appear to be a valid OpenPGP signed message!");
            }

            iPos++;
            if (bOnePassSigned)
            {
                if (packets[iPos] is SignaturePacket)
                {
                    spSignature = (SignaturePacket)packets[iPos];
                    iPos++;
                }
                else
                {
                    throw new System.ArgumentException("This does not appear to be a valid OpenPGP signed message!");
                }
            }

            return(iPos);
        }
示例#4
0
        /// <summary>
        /// Decrypts the current encrypted message using the secret keys
        /// in skrKeyRing and the given passphrase.
        /// </summary>
        /// <param name="skrKeyRing">The secret keyring containing all the
        /// secret keys know to the sytem.</param>
        /// <param name="strPassphrase">The passphrase that was used to
        /// encrypt the secret key material in the key that decrypts
        /// the message.</param>
        /// <returns>Returns the message that was encrypted. Usually this is
        /// an compressed or literal message.</returns>
        /// <remarks>No remarks</remarks>
        public Message Decrypt(SecretKeyRing skrKeyRing, string strPassphrase)
        {
            TransportableSecretKey tskSecretKey   = new TransportableSecretKey();
            AsymSessionKeyPacket   askpSessionKey = new AsymSessionKeyPacket();
            bool bFound = false;

            // let's see, if we can find a fitting Sessionkey packet
            IEnumerator ieSessionkeys = esKeys.AsymKeys.GetEnumerator();

            while (ieSessionkeys.MoveNext())
            {
                if (!(ieSessionkeys.Current is AsymSessionKeyPacket))
                {
                    throw new Exception("Strange Error!");
                }

                AsymSessionKeyPacket askpKey = (AsymSessionKeyPacket)ieSessionkeys.Current;
                ulong lKeyID = askpKey.KeyID;

                TransportableSecretKey tskKey = skrKeyRing.Find(lKeyID);
                if (tskKey != null)
                {
                    bFound         = true;
                    tskSecretKey   = tskKey;
                    askpSessionKey = askpKey;
                }
            }

            if (!bFound)
            {
                throw new Exception("No fitting secret key was found to decrypt the message!");
            }

            askpSessionKey.DecryptSessionKey(tskSecretKey, strPassphrase);
            byte[] bKey = askpSessionKey.SessionKey;

            Packet[] pContent = new Packet[0];
            try {
                SymmetricAlgorithm saAlgo = CipherHelper.CreateSymAlgorithm(askpSessionKey.SymmetricAlgorithm);
                pContent = sepData.Decrypt(bKey, saAlgo);
            } catch (Exception e) {
                throw new System.Exception("Decryption of the Message failed: " + e.Message);
            }

            // now we need to look what kind of message was hidden in the
            // encrypted data

            // it can be either a literal message
            LiteralMessage lmLiteral = new LiteralMessage();

            try {
                int iPos = lmLiteral.ParseMessage(pContent);
                return(lmLiteral);
            } catch (Exception) {}

            // or an compressed Message
            CompressedMessage cmCompressed = new CompressedMessage();

            try {
                int iPos = cmCompressed.ParseMessage(pContent);
                return(cmCompressed);
            } catch (Exception) {}

            throw new System.ArgumentException("Encrypted package content is not a valid message!");
        }