Handshake: Rewrite of encapsulate() & decapsulate() functions