Refactored encrypt functionality