Generalized encryption function