Beispiel #1
0
 /// <summary>
 /// Adds an asymmetrically encrypted session key to the ESK
 /// Sequence.
 /// </summary>
 /// <param name="askpKey">An asymmetrical session key packet
 /// that is to be added to the ESKSequence.</param>
 /// <remarks>No remarks</remarks>
 public void AddAsymSessionKey(AsymSessionKeyPacket askpKey)
 {
     bUpdated = true;
     alAsymKeys.Add(askpKey);
 }
Beispiel #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!");
        }
        private static ESKSequence CreateESKSequence(ArrayList alKeys, AsymActions aaAction, SymAlgorithms saAlgo, byte[] bSymKey)
        {
            IEnumerator ieKeys = alKeys.GetEnumerator();
            ESKSequence esksReturn = new ESKSequence();

            while (ieKeys.MoveNext()) {
                TransportablePublicKey tpkKey = (TransportablePublicKey)ieKeys.Current;
                PublicKeyPacket pkpKey = tpkKey.FindKey(aaAction);

                if (pkpKey == null)
                    throw new Exception("Could not find subkey fitting to the selected action. Concerned Key: " + tpkKey.PrimaryUserID);

                AsymSessionKeyPacket skpKey = new AsymSessionKeyPacket();
                skpKey.KeyID = pkpKey.KeyID;
                skpKey.PublicAlgorithm = pkpKey.Algorithm;
                skpKey.SymmetricAlgorithm = saAlgo;
                skpKey.SessionKey = bSymKey;

                skpKey.EncryptSessionKey(pkpKey);

                esksReturn.AddAsymSessionKey(skpKey);
            }

            return esksReturn;
        }
Beispiel #4
0
 /// <summary>
 /// Adds an asymmetrically encrypted session key to the ESK
 /// Sequence.
 /// </summary>
 /// <param name="askpKey">An asymmetrical session key packet 
 /// that is to be added to the ESKSequence.</param>
 /// <remarks>No remarks</remarks>
 public void AddAsymSessionKey(AsymSessionKeyPacket askpKey)
 {
     bUpdated = true;
     alAsymKeys.Add(askpKey);
 }
        /// <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!");
        }