byte[] passwordBytes = Encoding.UTF8.GetBytes("MyPassword"); byte[] salt = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; int iterations = 10000; int keySize = 256; Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(passwordBytes, salt, iterations); byte[] key = derivedBytes.GetBytes(keySize);
using System.Security.Cryptography; string password = "MyPassword"; byte[] salt = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; int iterations = 20000; int keySize = 128; using (var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations)) { byte[] key = pbkdf2.GetBytes(keySize); }This example is similar to the first example, but instead of using a byte array to represent the password, we use a string, and we wrap the call to Rfc2898DeriveBytes with a using statement to ensure that the IDisposable interface is properly implemented. The package library for Rfc2898DeriveBytes is System.Security.Cryptography.