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();
|
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();
One of my projects requires encrypting data on the iPhone and decrypting it using .Net. This is easy to do with the Common Crypto library in the iPhone SDK and the AesCryptoServiceProvider class in .Net, but the encryption parameters have to be the same for it to work.
I couldn’t figure it out, but the geniuses at StackOverflow did, so I am posting my results here. The zip file includes a basic iPhone app and a .Net console project with helpful classes to do the encryption/decryption and base64 conversion. I didn’t write most of the code – thanks to Blue Beetle for the .Net code and Greg Haygood for the Objective C.
Download zip.
n. 1: automatic, but with an element of magic. 2: too complex to understand and/or explain