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();
public DataSet ExecuteQuery(string commandText)
{
DataSet ds = new DataSet();
using (var connection = new MySqlConnection(connString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = commandText;
using (var data = new MySqlDataAdapter { SelectCommand = command })
{
data.Fill(ds);
connection.Close();
}
}
return ds;
} |
public DataSet ExecuteQuery(string commandText)
{
DataSet ds = new DataSet();
using (var connection = new MySqlConnection(connString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = commandText;
using (var data = new MySqlDataAdapter { SelectCommand = command })
{
data.Fill(ds);
connection.Close();
}
}
return ds;
}
I use this code here.
Graphics backgroundGraphics;
backgroundImage = (Bitmap)Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "\images\Header.jpg");
backgroundGraphics = Graphics.FromImage(backgroundImage);
var font = new Font("Perpetua Titling MT", 24F, FontStyle.Regular);
backgroundGraphics.DrawString(authorname.ToUpper(), font, new SolidBrush(Color.FromArgb(100, 0, 0, 0)), 10, 5); |
Graphics backgroundGraphics;
backgroundImage = (Bitmap)Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "\images\Header.jpg");
backgroundGraphics = Graphics.FromImage(backgroundImage);
var font = new Font("Perpetua Titling MT", 24F, FontStyle.Regular);
backgroundGraphics.DrawString(authorname.ToUpper(), font, new SolidBrush(Color.FromArgb(100, 0, 0, 0)), 10, 5);
Now save – or output to the browser:
backgroundImage.Save(context.Response.OutputStream, ImageFormat.Jpeg); |
backgroundImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
n. 1: automatic, but with an element of magic. 2: too complex to understand and/or explain