Refactor encrypt/decrypt commands

This reduces duplication since both commands operate very similarly. It
also had the byproduct of flushing out a few remaining bugs.