PasswordDeriveBytes Class
.NET Framework 3.0
Derives a key from a password using an extension of the PBKDF1 algorithm.
Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
System.Security.Cryptography Namespace
Assembly: mscorlib (in mscorlib.dll)
This class uses an extension of the PBKDF1 algorithm defined in the PKCS#5 v2.0 standard to derive bytes suitable for use as key material from a password. The standard is documented in IETF RRC 2898.
Security Note: |
|---|
| Never hard-code a password within your source code. Hard coded passwords can be retrieved from an assembly using the MSIL Disassembler (Ildasm.exe) tool, a hex editor, or by simply opening up the assembly in a text editor like notepad.exe. |
The following code example creates a key from a password using the PasswordDeriveBytes class.
using System; using System.Security.Cryptography; using System.Text; public class PasswordDerivedBytesExample { public static void Main(String[] args) { // Get a password from the user. Console.WriteLine("Enter a password to produce a key:"); byte[] pwd = Encoding.Unicode.GetBytes(Console.ReadLine()); byte[] salt = createRandomSalt(7); // Create a TripleDESCryptoServiceProvider object. TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); try { Console.WriteLine("Creating a key with PasswordDeriveBytes..."); // Create a PasswordDeriveBytes object and then create // a TripleDES key from the password and salt. PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwd, salt); // Create the key and set it to the Key property // of the TripleDESCryptoServiceProvider object. tdes.Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV); Console.WriteLine("Operation complete."); } catch (Exception e) { Console.WriteLine(e.Message); } finally { // Clear the buffers clearBytes(pwd); clearBytes(salt); // Clear the key. tdes.Clear(); } Console.ReadLine(); } //******************************************************** //* Helper methods: //* createRandomSalt: Generates a random salt value of the //* specified length. //* //* clearBytes: Clear the bytes in a buffer so they can't //* later be read from memory. //******************************************************** public static byte[] createRandomSalt(int Length) { // Create a buffer byte[] randBytes; if (Length >= 1) { randBytes = new byte[Length]; } else { randBytes = new byte[1]; } // Create a new RNGCryptoServiceProvider. RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider(); // Fill the buffer with random bytes. rand.GetBytes(randBytes); // return the bytes. return randBytes; } public static void clearBytes(byte[] Buffer) { // Check arguments. if (Buffer == null) { throw new ArgumentException("Buffer"); } // Set each byte in the buffer to 0. for (int x = 0; x <= Buffer.Length - 1; x++) { Buffer[x] = 0; } } }
System.Object
System.Security.Cryptography.DeriveBytes
System.Security.Cryptography.PasswordDeriveBytes
System.Security.Cryptography.DeriveBytes
System.Security.Cryptography.PasswordDeriveBytes
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Reference
PasswordDeriveBytes MembersSystem.Security.Cryptography Namespace
Other Resources
Cryptographic ServicesCommunity Additions
ADD
Show:
Security Note: