using System.Security.Cryptography; string password = "MyPassword"; byte[] salt = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt); byte[] key = pdb.GetBytes(32); //Generate 256-bit key
using System.Security.Cryptography; string password = "MyPassword"; byte[] salt = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; byte[] plainText = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt); byte[] key = pdb.GetBytes(32); using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { aes.Key = key; aes.IV = new byte[aes.BlockSize / 8]; ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); byte[] cipherText = new byte[plainText.Length]; int bytesEncrypted = encryptor.TransformBlock(plainText, 0, plainText.Length, cipherText, 0); // ... }This example generates a 256-bit key using the PasswordDeriveBytes class and uses it to encrypt the plaintext using the AES encryption algorithm. Package Library: System.Security.Cryptography