How to choose keys for a block cipher?

AES and DES are block ciphers. Mathematically, its the mapping from plaintext space to ciphertext space using the keys i.e. $\{{0,1}\}^k$ x $\{{0,1}\}^l \longrightarrow \{{0,1}\}^l$

I know that these keys are random. What I want to know is how these are calculated in real world and is there any way to change these keys meaning that is it possible for me to use my own keys in these block ciphers?

-
The keys $k$, $l$, etc.. are just variables. You can put whatever key you want in there (as long as it is 56 bits long, or 128 bits long, or whatever the key length for the given cipher is) and you will get one of the possible permutations of the cipher (parameterized by the key). Are you asking how those keys are generated e.g. how they are derived from passwords for instance and so on? –  Thomas Sep 18 '13 at 12:05
DES keys, at least not if you look at the complete encoded binary form, are not random. They contain parity bits. This was one of the drawbacks of DES that was removed in AES. And actually, block cipher keys should consist of bits indistinguishable from random. If they were completely random then keys generated by a KDF (e.g. from a password) would be invalid. –  owlstead Sep 18 '13 at 14:59
@Thomas - I know that functions like PBKDF2 are used to derive keys from Passwords. However, I didn't know that these functions are also used to derive keys for the block cipher. I always thought that these keys were hard coded/pre-programmed in to the circuit. Are you asking how those keys are generated e.g. how they are derived from passwords for instance and **so on** ? (This could be a separate question but) What are the other methods to derive keys other than passwords? –  TheRookierLearner Sep 18 '13 at 16:58
@owlstead - +1 Thanks for the info. That is something I didn't really know. –  TheRookierLearner Sep 18 '13 at 17:01

1 Answer

In contrast to asymmetric schemes (notably RSA and El Gamal) which require some sort of computation to generate the key, the only constraint one has when selecting a key for DES or AES (or 3DES) is to make it look indistinguishible from a random stream. That said both El Gamal and RSA require some randomness in key generation, but that phase does not depend just on that.

You could however use any byte stream of sufficient length as a key, irrespective or randomness, at your peril. That's because the security of these schemes (of all schemes really) depends on the inability of some adversary to compute or guess the key in some practical timeframe. It's worth mentioning here that DES keys can be (and have been) efficiently brute-forced because they are short in length. Search this site for randomness test or take a look at the randomness tag to get a better idea.

In practice it's possible to derive a key from a password, passphrase or a random stream of smaller length by using a key derivation function. You may also use any CSPRNG to generate a long enough stream, which you can use as a key.

-
(Triple) DES keys contain parity. At least officially you need to calculate the parity of 3DES keys, even if they aren't directly used during decryption. But it can lead to interesting issues none-the-less, e.g. when you use a Key Check Value that uses SHA-1 over the DES key itself. Some libraries and even HSM's don't accept DES keys that don't have the correct parity bits set. –  owlstead Sep 18 '13 at 14:56
@rath - So, is there anyway to determine randomness? –  TheRookierLearner Sep 18 '13 at 17:20
Yup. Take a look at the resource in this answer and also on the relevant Wikipedia page. Also the output of block ciphers is (or should be) considered indistinguishable from a random source. Take a look at counter mode for example. Cheers @TheRookierLearner –  rath Sep 18 '13 at 17:39
@TheRookierLearner ...but to run a block cipher in any mode you need to use a proper (read: sufficiently random as described above) key so my last remark is a cyclical argument. You may also find this article on snake-oil crypto useful. Cheers –  rath Sep 18 '13 at 17:52