Wednesday, February 25, 2015

Create Key from Password Using PBKDF2

In the previous post I explained the usage of the AES encryption. For that we created the needed key and IV manually by converting strings chosen by us into Byte arrays. This is not how it should be done. Therefore here a tutorial how to create a key and IV in a safer way.
For this we use the key derivation function PBKDF2. This is implemented in the .Net class Rfc2898DeriveBytes.
First again some theory: PBKDF2 is short for Password-Based Key Derivation Function 2. Its purpose is the creation of a byte sequence (from which then a part can be used as key / IV) out of a chosen password. The byte sequence is created after the initialization (amongst others with the password) via a pseudo random function and thus can be extended to nearly arbitrary length.
So instead of chosing a password, and then creating e.g. via System.Text.Encoding.UTF8.GetBytes() a byte sequence as key / IV directly, we let the above mentioned function do this step. What does this help us now? For this we have to take a closer look at its mode of operation.
In the PBKDF2 method the chosen password is combined with a chosen salt and then hashed multiple times. This forms the basis for the pseudorandom generator, with which the byte sequence is generated. So we must / can choose the password, the hash and the number of iterations. This has many advantages compared to the naive method, here are some of them (they are not ordered by their importance, but nearly the other way around!):

  • As already recognized in the previous post the conversion between String and Byte Array can cause problems, since the size of single characters varies and we thus have to use a string of the correct length, so that it satisfies the requirements of e.g. AES regarding key size etc. With PBKDF2 we can choose arbitrary passwords, and then simply take the needed number of bytes from the result.
  • Amongst others the hashing creates an almost equal distribution of the resulting bytes. The direct byte representation of chosen passwords (mostly words or numbers) is often predictable and thus the resulting key more vulnerable to Brute-Force attacks.
  • Multiple rounds of hashing hinder and slow down a Brute-Force attack on the key, since the attacker now not only has to guess the password but also the number of rounds n, further the hash function has to be applied n times.
  • The additional selection of a salt hinders the usage of precalculated rainbow tables.
Now to the code, the following lines calculate out of the given password and salt via the PBKDF2 method with 10000 iterations a byte sequence. With the function GetBytes(m) we get the next m of the sequence (here 16 for AES 128 bit):


Rfc2898DeriveBytes Generator = new Rfc2898DeriveBytes("Any password", System.Text.Encoding.UTF8.GetBytes("some salt"), 10000);
byte[] Key = Generator.GetBytes(16);
byte[] IV = Generator.GetBytes(16);

No comments:

Post a Comment