Move encryption code into template method