avoid code duplication, move encrypt checking to lib
Merge pull request #171 from nilxam/update_1126

avoid code duplication, move encrypt checking to lib