Derive a Key from a Password

Problem

You want to change a string into a series of bytes suitable for use with symmetric encryption.

Solution

Use the CryptDeriveKey method of the PasswordDeriveBytes class.

Discussion

You can convert a string to a series of bytes using the techniques described in Chapter 1 and Chapter 2. However, this approach is insufficient for cryptography. For example, most passwords are relatively short and use a limited set of characters (alphabetic characters and numerals). This dramatically reduces the number and strength of the keys that you can generate.

You can, however, use a string value to seed a cryptographically strong random number generator, which can then generate a cryptographically strong sequence of bytes that you ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.