Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Security.Cryptography;
- using System.IO;
- namespace ssclab3
- {
- public partial class Form1 : Form
- {
- MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
- RandomNumberGenerator rnd = RandomNumberGenerator.Create();
- byte[] input = new byte[20];
- byte[] hashValue;
- //generates some random input rnd.GetBytes(input);
- //computes the hash hashValue = myMD5.ComputeHash(input);
- private HashAlgorithm hashAlgorithm;
- private ConversionHandler convert = new ConversionHandler();
- private HMAC myMAC;
- private string comboBoxSelection;
- private string inputTextString;
- public void MACHandler(string name)
- {
- if (name.CompareTo("SHA1") == 0)
- {
- myMAC = new System.Security.Cryptography.HMACSHA1();
- }
- if (name.CompareTo("MD5") == 0)
- {
- myMAC = new System.Security.Cryptography.HMACMD5();
- }
- if (name.CompareTo("RIPEMD") == 0)
- {
- myMAC = new System.Security.Cryptography.HMACRIPEMD160();
- }
- if (name.CompareTo("SHA256") == 0)
- {
- myMAC = new System.Security.Cryptography.HMACSHA256();
- }
- if (name.CompareTo("SHA384") == 0)
- {
- myMAC = new System.Security.Cryptography.HMACSHA384();
- }
- if (name.CompareTo("SHA512") == 0)
- {
- myMAC = new System.Security.Cryptography.HMACSHA512();
- }
- }
- public HashAlgorithm selectAlgorithm(string name)
- {
- switch (name)
- {
- case "SHA1":
- hashAlgorithm = SHA1.Create();
- break;
- case "MD5":
- hashAlgorithm = MD5.Create();
- break;
- case "SHA256":
- hashAlgorithm = SHA256.Create();
- break;
- }
- return hashAlgorithm;
- }
- public bool CheckAuthenticity(byte[] mes, byte[] mac, byte[] key)
- {
- myMAC.Key = key;
- if (CompareByteArrays(myMAC.ComputeHash(mes), mac, myMAC.HashSize / 8) == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public byte[] ComputeMAC(byte[] mes, byte[] key)
- {
- myMAC.Key = key;
- return myMAC.ComputeHash(mes);
- }
- public int MACByteLength()
- {
- return myMAC.HashSize / 8;
- }
- private bool CompareByteArrays(byte[] a, byte[] b, int len)
- {
- for (int i = 0; i < len; i++)
- if (a[i] != b[i])
- return false;
- return true;
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_MouseClick(object sender, MouseEventArgs e)
- {
- MACHandler(comboBoxSelection);
- selectAlgorithm(comboBoxSelection);
- input = convert.StringToByteArray(inputTextString);
- hashValue = hashAlgorithm.ComputeHash(input);
- label2.Text = convert.ByteArrayToHexString(hashValue);
- }
- private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
- {
- ComboBox combobox = new ComboBox();
- comboBoxSelection = combobox.GetItemText(comboBox1.SelectedItem);
- }
- class ConversionHandler
- {
- public byte[] StringToByteArray(string s) { return CharArrayToByteArray(s.ToCharArray()); }
- public byte[] CharArrayToByteArray(char[] array) { return Encoding.ASCII.GetBytes(array, 0, array.Length); }
- public string ByteArrayToString(byte[] array)
- {
- return Encoding.ASCII.GetString(array);
- }
- public string ByteArrayToHexString(byte[] array) { string s = ""; int i; for (i = 0; i < array.Length; i++) { s = s + NibbleToHexString((byte)((array[i] >> 4) & 0x0F)) + NibbleToHexString((byte)(array[i] & 0x0F)); } return s; }
- public byte[] HexStringToByteArray(string s)
- {
- byte[] array = new byte[s.Length / 2]; char[] chararray = s.ToCharArray(); int i; for (i = 0; i < s.Length / 2; i++)
- {
- array[i] = (byte)(((HexCharToNibble(chararray[2 * i]) << 4) & 0xF0) | ((HexCharToNibble(chararray[2 * i + 1]) & 0x0F)));
- }
- return array;
- }
- public string NibbleToHexString(byte nib) { string s; if (nib < 10) { s = nib.ToString(); } else { char c = (char)(nib + 55); s = c.ToString(); } return s; }
- public byte HexCharToNibble(char c)
- {
- byte value = (byte)c; if (value < 65)
- {
- value = (byte)(value - 48);
- }
- else { value = (byte)(value - 55); }
- return value;
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- inputTextString = textBox1.Text;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement