The critical element (SO wanted to use "key" there) in establishing and maintaining codes, at least before mega-flop computers, was/is operator discipline. Enigma would never have been broken had operators not failed in proper procedure. A close second is regular replacement, followed by volume. The Reich made all THREE errors and did so on an ongoing basis. Using the same header/footer, sending the same text twice, non random settings, all greatly aided the code breakers. The decision to use plain text under Enigma was also a weakness. To give an opposite example: American "Code talkers" spoke Navajo, a language that was dying even among the tribes, making it almost impossible to understand, this was however, only the first layer. Code Talkers also used code words that only someone who had been trained to interpret the word from its code to actual meaning (this was aided since Navajo lacked words for things like machine gun, artillery, mortars, etc. so the USMC communications department had to come up with work around, some obvious, other utterly impenetrable). There were documented case of the Japanese capturing non Code Talkers, putting them on a tap, and ordering them to translate. What came out was such gibberish (sentences like "house hawk stream sun canyon flower") that the poor bastard who had the bayonet to his throat couldn't tell the Japanese even if he wanted to.
The last error, and it was a huge one, was to send almost everything using the same code setting. Weather report, attack order, location of super secret supply dump, all went out using the same code. There were a few messages, usually straight from Hitler, that didn't. That code was not broken (just like the IJN Flag Officer code resisted penetration due to lack of traffic to analyze).