Fix the index out of range on genKey function.