I did a presentation last week on AES encryption techniques in .Net.
I’ll post some details here later, but for now, I’ve uploaded a zip file with the project code.
Here’s the key bit:
string key = "1234567891123456";
string secret = @"This is a secret.";
Console.WriteLine("basic:");
EncryptString(key, secret);
Console.ReadKey();
Console.WriteLine("salt the secret:");
// good when there are multiple machines but a dynamic global shared secret (for example, Profile Create Date or User ID)
string secret2 = secret + " ###" + DateTime.Now.Millisecond;
EncryptString(key, secret2);
secret2 = secret + " ###" + DateTime.Now.Millisecond;
EncryptString(key, secret2);
Console.ReadKey();
Console.WriteLine("salt the key:");
// good when the same machine encrypts/decrepts
string uniqueMachineIdentifier = MachineId.GetProcessorID();
Console.WriteLine("MachineId: " + uniqueMachineIdentifier);
EncryptString(key + uniqueMachineIdentifier, secret);
Console.ReadKey();
Console.WriteLine("SHA1 hash the passphrase with a salt:");
// note: talk about why hashing is good
SHA1 sha = new SHA1CryptoServiceProvider();
// This is one implementation of the abstract class SHA1.
string password = "this is my user password and/or userid";
byte[] saltedKey = Encoding.Default.GetBytes(key + password);
byte[] result = sha.ComputeHash(saltedKey);
EncryptString(Convert.ToBase64String(result), secret);
Console.ReadKey();
|