Encryption Tutorial
If you want to run an example EXE it is here [Click
here]
If you want a starting Solution [Click
here]
1. Create form. Initially create a form using the
TabControl control, with the tabs of MD5/SHA, RC2, RSA, 3DES and HMAC.
Also add a number of TextBox controls (named tbMessage, tbMD5 tbSHA,
tbSHA256, and so on), such as:

Next add a button, and double click on it to open the Click event,
and add the following code:
private void button1_Click(object sender, System.EventArgs e)
{
string message;
message = this.tbMessage.Text;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
MD5 md5 = new MD5CryptoServiceProvider();
SHA1 sha1 = new SHA1CryptoServiceProvider();
SHA256Managed sha256 = new SHA256Managed();
SHA384Managed sha384 = new SHA384Managed();
SHA512Managed sha512 = new SHA512Managed();
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = md5.ComputeHash(messageBytes);
this.tbMD5.Text= ByteToString(hashmessage);
hashmessage = sha1.ComputeHash(messageBytes);
this.tbSHA1.Text = ByteToString(hashmessage);
hashmessage = sha256.ComputeHash(messageBytes);
this.tbSHA256.Text = ByteToString(hashmessage);
hashmessage = sha384.ComputeHash(messageBytes);
this.tbSHA384.Text = ByteToString(hashmessage);
hashmessage = sha512.ComputeHash(messageBytes);
this.tbSHA512.Text = ByteToString(hashmessage);
}
and then below this add the ByteToString static method:
public static string ByteToString(byte [] buff)
{
string sbinary="";
for (int i=0;i<buff.Length;i++)
{
sbinary+=buff[i].ToString("X2"); // hex format
}
return(sbinary);
}
2. Testing the program. Next test the program with
a number of test values:
"Hello" should give: 8b1a9953c4611296a827abf8c47804d7 (MD5)
"hello" should give: 5d41402abc4b2a76b9719d911017c592 (MD5)
Test some other values and confirm against: [Click
here]
3. Encrypting with RC2. Next do to the RC2 tab,
and add the controls shown below (named tbMessage2, tbKey, tbEncrypt
and tbDecrypt).

Next add the following code to the button event:
private void button2_Click(object sender, System.EventArgs e)
{
RC2CryptoServiceProvider rc2 = new System.Security.Cryptography.RC2CryptoServiceProvider();
rc2.Key = StringToByte(this.tbKey.Text, 12); // convert to 12 characters
rc2.IV = StringToByte("",8);
byte[] key = rc2.Key;
byte[] IV = rc2.IV;
ICryptoTransform encryptor = rc2.CreateEncryptor(key, IV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
// Write all data to the crypto stream and flush it.
csEncrypt.Write(StringToByte(this.tbMessage2.Text), 0, StringToByte(this.tbMessage2.Text).Length);
csEncrypt.FlushFinalBlock();
// Get the encrypted array of bytes.
byte[] encrypted = msEncrypt.ToArray();
this.tbEncrypt.Text = ByteToString(encrypted);
ICryptoTransform decryptor = rc2.CreateDecryptor(key, IV);
// Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
this.tbDecrypt.Text = ByteToString(csDecrypt);
}
and this static method:
public static byte[] StringToByte(string StringToConvert, int length)
{
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[length];
for (int i = 0; i < CharArray.Length; i++)
{
if (i==length) break;
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
4. Testing RC2. Next test RC2 encryption:
Text: "hello", key: "hello" should give "EBE98BE9D2D3A4F4"
for the ciper stream.
Test some other values and confirm against: [Click
here]
5. RSA. Following the code from:
http://buchananweb.co.uk/security08.aspx [Click
here]
add RSA functionality.
6. 3DES. Following the code from:
http://buchananweb.co.uk/security07.aspx [Click
here]
add 3DES functionality.
7. HMAC. Following the code from:
http://buchananweb.co.uk/security01.aspx [Click
here]
add HMAC functionality.
|