How should I create my DES key? Why is an 7-character string not enough?

djdd87 picture djdd87 · Jun 8, 2009 · Viewed 23k times · Source

I'm having a bit of difficulty getting an understand of key length requirements in cryptography. I'm currently using DES which I believe is 56 bits... now, by converting an 8 character password to a byte[] my cryptography works. If I use a 7 digit password, it doesn't.

Now, forgive me if I'm wrong, but is that because ASCII characters are 7 bits, therefor 8 * 7 = 56bits?

That just doesn't seem right to me. If I want to use a key, why can I not just pass in a salted hash of my secret key, i.e. an MD5 hash?

I'm sure this is very simple, but I can't get a clear understanding of what's going on.

Answer

erickson picture erickson · Jun 8, 2009

DES uses a 56-bit key: 8 bytes where one bit in each byte is a parity bit.

In general, however, it is recommended to use an accepted, well-known key derivation algorithm to convert a text password to a symmetric cipher key, regardless of the algorithm.

The PBKDF2 algorithm described in PKCS #5 (RFC 2898) is a widely-used key derivation function that can generate a key of any length. At its heart, PBKDF2 is combining salt and the password through via a hash function to produce the actual key. The hash is repeated many times so that it will be expensive and slow for an attacker to try each entry in her "dictionary" of most common passwords.

The older version, PBKDF1, can generate keys for DES encryption, but DES and PBKDF1 aren't recommended for new applications.

Most platforms with cryptographic support include PKCS #5 key-derivation algorithms in their API.