Improve double encryption of keys by adding checks