Refactor codec logic to separate I/O

Add a method that expects a key file handle and a text string for
encoding and decoding. Use these methods from the GUI instead of
spurious temporary files or StringIO.

Fixes #3