/// <summary> /// Finds a secret key out of the given secret keyring that is able /// to decrypt the current encrypted message and returns its KeyID. /// If such a key is not found, 0 is returned. /// </summary> /// <param name="skrRing">Secret keyring containing all secret keys /// known to the system.</param> /// <returns>Returns the KeyID of the key that is able to decrypt the /// encrypted message.</returns> /// <remarks>No remarks</remarks> public ulong GetFittingKeyID(SecretKeyRing skrRing) { bool bFound = false; 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 = skrRing.Find(lKeyID); if (tskKey != null) { return(lKeyID); } } if (!bFound) { throw new Exception("No fitting secret key was found to decrypt the message!"); } return(0); }
/// <summary> /// Returns true if the given object is the "this" key. /// </summary> /// <remarks> /// The keys are compared by there fingerprint. If the fingerprint /// is the same, the keys are said to be the same. /// </remarks> /// <param name="o">An object that shall be compared against /// this.</param> /// <returns>True if the giben object o is the same as the /// "this" key.</returns> public override bool Equals(object o) { if (o is TransportableSecretKey) { TransportableSecretKey tskKey = (TransportableSecretKey)o; return(tskKey.PrimaryKey.PublicKey.Fingerprint == this.PrimaryKey.PublicKey.Fingerprint); } return(false); }
public static TransportableSecretKey[] SplitKeys(string strRadix64) { ArrayList alKeys = new ArrayList(); Packet[] pPackets = Packet.ParsePackets(strRadix64); byte[] bOneKey = new byte[0]; for (int i = 0; i < pPackets.Length; i++) { if (pPackets[i] is PublicKeyPacket) { SecretKeyPacket skpKey = (SecretKeyPacket)pPackets[i]; if ((skpKey.Content == ContentTypes.SecretKey) && (bOneKey.Length > 10)) { TransportableSecretKey tskKey = new TransportableSecretKey(Radix64.Encode(bOneKey, true)); alKeys.Add(tskKey); bOneKey = new byte[0]; } } byte[] bPacket = pPackets[i].Generate(); byte[] bTmpKey = new byte[bOneKey.Length]; bOneKey.CopyTo(bTmpKey, 0); bOneKey = new byte[bOneKey.Length + bPacket.Length]; Array.Copy(bTmpKey, 0, bOneKey, 0, bTmpKey.Length); Array.Copy(bPacket, 0, bOneKey, bTmpKey.Length, bPacket.Length); } if (bOneKey.Length > 10) { TransportableSecretKey tskKey = new TransportableSecretKey(Radix64.Encode(bOneKey, true)); alKeys.Add(tskKey); } TransportableSecretKey[] tskKeys = new TransportableSecretKey[alKeys.Count]; int iCount = 0; IEnumerator ieKeys = alKeys.GetEnumerator(); while (ieKeys.MoveNext()) { if (!(ieKeys.Current is TransportableSecretKey)) { continue; } tskKeys[iCount++] = (TransportableSecretKey)ieKeys.Current; } return(tskKeys); }
/// <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!"); }