/// <summary> /// Gets the OpenPGP encoded representation of the ESK Sequence. /// </summary> /// <returns>Returns a byte array that represents the encoded /// ESKSequence.</returns> /// <remarks>No remarks</remarks> public override byte[] GetEncoded() { byte[] bOutput = new byte[0]; if (!bUpdated) { //nothing was updated, we can reconstruct the message //in the exact same order IEnumerator ieKeys = alAllKeys.GetEnumerator(); while (ieKeys.MoveNext()) { Packet pKey = (Packet)ieKeys.Current; byte[] bKey = pKey.Generate(); byte[] bOldOutput = new byte[bOutput.Length]; bOutput.CopyTo(bOldOutput, 0); bOutput = new byte[bOldOutput.Length + bKey.Length]; bOldOutput.CopyTo(bOutput, 0); bKey.CopyTo(bOutput, bOldOutput.Length); } } else { // At first we will produce the Symmetrically encrypted // session key packets IEnumerator ieSymKeys = alSymKeys.GetEnumerator(); while (ieSymKeys.MoveNext()) { SymSessionKeyPacket sskpKey = (SymSessionKeyPacket)ieSymKeys.Current; byte[] bKey = sskpKey.Generate(); byte[] bOldOutput = new byte[bOutput.Length]; bOutput.CopyTo(bOldOutput, 0); bOutput = new byte[bOldOutput.Length + bKey.Length]; bOldOutput.CopyTo(bOutput, 0); bKey.CopyTo(bOutput, bOldOutput.Length); } // Now come the Public Key encrypted session key packets IEnumerator ieAsymKeys = alAsymKeys.GetEnumerator(); while (ieAsymKeys.MoveNext()) { AsymSessionKeyPacket askpKey = (AsymSessionKeyPacket)ieAsymKeys.Current; byte[] bKey = askpKey.Generate(); byte[] bOldOutput = new byte[bOutput.Length]; bOutput.CopyTo(bOldOutput, 0); bOutput = new byte[bOldOutput.Length + bKey.Length]; bOldOutput.CopyTo(bOutput, 0); bKey.CopyTo(bOutput, bOldOutput.Length); } } return(bOutput); }
/// <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!"); }
/// <summary> /// Adds an symmetrically encrypted session key to the ESK /// Sequence. /// </summary> /// <param name="sskpKey">A symmetrical Session key packet that /// is to be added the the ESKSequence.</param> /// <remarks>No remarks</remarks> public void AddSymSessionKey(SymSessionKeyPacket sskpKey) { bUpdated = true; alSymKeys.Add(sskpKey); }
/// <summary> /// Adds an symmetrically encrypted session key to the ESK /// Sequence. /// </summary> /// <param name="sskpKey">A symmetrical Session key packet that /// is to be added the the ESKSequence.</param> /// <remarks>No remarks</remarks> public void AddSymSessionKey(SymSessionKeyPacket sskpKey) { bUpdated = true; alSymKeys.Add(sskpKey); }