Made the decrypt/encrypt functions more pure, and fixed a bug causes improper key generation.