# Is there a correct way to generate a symmetric key?

Looking in some cryptographic algorithms, I've realized that: The way the plain text is encrypted/decrypted is always specified, but what about the key? Every paper I've seen describing the algorithm never show a way to generate a key, but show the available key sizes.

This leads me to a question: How should I generate a key as someone who is implementing an algorithm? For example: The Twofish paper (https://www.schneier.com/paper-twofish-paper.pdf) says that Twofish has available key sizes of 128, 192 and 256 bits, but how should I create a 128 bit key? Not even the reference implementations I found contains code that seems to be aimed to be a key generation algorithm (again speaking of Twofish).

For a university work, I'll write a simple implementation of Twofish in C#, and I need a way to create a key but I don't know how to do it or even if there's a correct way to do this, that's why I ask this question.

A real example: When I encrypt anything with GnuPG (https://www.gnupg.org/) using symmetric keys, it does not generate any key or the like, it just asks for a password and does it. What's happening behind the scenes? How does GPG uses this password and how it's is related to the key generation?

• crypto.stackexchange.com/questions/3332/… Oct 6, 2015 at 23:22
• crypto.stackexchange.com/questions/25485/… Oct 6, 2015 at 23:22
• TL;DR, symmetric keys for modern ciphers are nothing more than cryptographically random blobs of the appropriate length. Oct 7, 2015 at 0:07
• The easiest low-tech way for a cryptographically secure key is to get one of those bingo drums. Leave in the chits for 1 through 63, and put another one in that you designate 0. Each draw of a chit is 6 bits of entropy. Keep drawing until you fill your key, placing the chit back in the drum for each spin. As long as only the 2 people sharing secrets know those numbers, their secret is secure. But it's much more common to hash a password millions of times. Check PBKDF2 for a formal specification to make passwords into keys.
– WDS
Oct 7, 2015 at 2:23
• PGP is using a Key Derivation to produce a symmetric session key from a passphrase. It has even two modes, it can genrate a randome symmetric key and wrap this with the derived key from the password or it can use the key derived from the password directly. Typically langages offer PBE ciphers to do this yourself. Aug 22, 2016 at 18:42

• There are many ciphers which have restrictions on weak keys. I would not blindly use a RNG if the programming environment actually has a function for key generation (or key derivation). In Java you would use the KeyGenerator.getInstance("Algo") method to get a matching generator. Aug 22, 2016 at 18:39