11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7: Building Secure Assemblies 175Generate R<strong>and</strong>om KeysIf you need to generate encryption keys programmatically, useRNGCryptoServiceProvider for creating keys <strong>and</strong> initialization vectors <strong>and</strong> donot use the R<strong>and</strong>om class. Unlike the R<strong>and</strong>om class, RNGCryptoServiceProvidercreates cryptographically strong r<strong>and</strong>om numbers which are FIPS-140 compliant.The following code shows how to use this function.using System.<strong>Security</strong>.Cryptography;. . .RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();byte[] key = new byte[keySize];rng.GetBytes(key);Use PasswordDeriveBytes for Password-Based EncryptionThe System.<strong>Security</strong>.Cryptography.DeriveBytes namespace providesPasswordDeriveBytes for use when encrypting data based on a password the usersupplies. To decrypt, the user must supply the same password used to encrypt.Note that this approach is not for password authentication. Store a password verifierin the form of a hash value with a salt value order to authenticate a user’s password.Use PasswordDeriveBytes to generate keys for password-based encryption.PasswordDeriveBytes accepts a password, salt, an encryption algorithm, a hashingalgorithm, key size (in bits), <strong>and</strong> initialization vector data to create a symmetric key tobe used for encryption.After the key is used to encrypt the data, clear it from memory but persist the salt <strong>and</strong>initialization vector. These values should be protected <strong>and</strong> are needed to re-generatethe key for decryption.For more information about storing password hashes with salt, see Chapter 14,“Building Secure Data Access.”Prefer Large KeysWhen generating an encryption key or key pair, use the largest key size possible forthe algorithm. This does not necessarily make the algorithm more secure butdramatically increases the time needed to successfully perform a brute force attack onthe key. The following code shows how to find the largest supported key size for aparticular algorithm.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!