implemented encryption and key logic