Esempio n. 1
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!");
        }
Esempio n. 2
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!");
        }