Advertisement
Guest User

ssc3

a guest
Oct 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Security.Cryptography;
  11. using System.IO;
  12.  
  13. namespace ssclab3
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
  18.         RandomNumberGenerator rnd = RandomNumberGenerator.Create();
  19.         byte[] input = new byte[20];
  20.         byte[] hashValue;
  21.         //generates some random input rnd.GetBytes(input);
  22.         //computes the hash hashValue = myMD5.ComputeHash(input);
  23.         private HashAlgorithm hashAlgorithm;
  24.         private ConversionHandler convert = new ConversionHandler();
  25.         private HMAC myMAC;
  26.         private string comboBoxSelection;
  27.         private string inputTextString;
  28.  
  29.         public void MACHandler(string name)
  30.         {
  31.             if (name.CompareTo("SHA1") == 0)
  32.             {
  33.                 myMAC = new System.Security.Cryptography.HMACSHA1();
  34.             }
  35.             if (name.CompareTo("MD5") == 0)
  36.             {
  37.                 myMAC = new System.Security.Cryptography.HMACMD5();
  38.             }
  39.             if (name.CompareTo("RIPEMD") == 0)
  40.             {
  41.                 myMAC = new System.Security.Cryptography.HMACRIPEMD160();
  42.             }
  43.             if (name.CompareTo("SHA256") == 0)
  44.             {
  45.                 myMAC = new System.Security.Cryptography.HMACSHA256();
  46.             }
  47.             if (name.CompareTo("SHA384") == 0)
  48.             {
  49.                 myMAC = new System.Security.Cryptography.HMACSHA384();
  50.             }
  51.             if (name.CompareTo("SHA512") == 0)
  52.             {
  53.                 myMAC = new System.Security.Cryptography.HMACSHA512();
  54.             }
  55.         }
  56.  
  57.         public HashAlgorithm selectAlgorithm(string name)
  58.         {
  59.             switch (name)
  60.             {
  61.                 case "SHA1":
  62.                     hashAlgorithm = SHA1.Create();
  63.                     break;
  64.                 case "MD5":
  65.                     hashAlgorithm = MD5.Create();
  66.                     break;
  67.                 case "SHA256":
  68.                     hashAlgorithm = SHA256.Create();
  69.                     break;
  70.             }
  71.             return hashAlgorithm;
  72.         }
  73.  
  74.         public bool CheckAuthenticity(byte[] mes, byte[] mac, byte[] key)
  75.         {
  76.             myMAC.Key = key;
  77.             if (CompareByteArrays(myMAC.ComputeHash(mes), mac, myMAC.HashSize / 8) == true)
  78.             {
  79.                 return true;
  80.             }
  81.             else
  82.             {
  83.                 return false;
  84.             }
  85.         }
  86.  
  87.         public byte[] ComputeMAC(byte[] mes, byte[] key)
  88.         {
  89.             myMAC.Key = key;
  90.             return myMAC.ComputeHash(mes);
  91.         }
  92.  
  93.         public int MACByteLength()
  94.         {
  95.             return myMAC.HashSize / 8;
  96.         }
  97.  
  98.         private bool CompareByteArrays(byte[] a, byte[] b, int len)
  99.         {
  100.             for (int i = 0; i < len; i++)
  101.                 if (a[i] != b[i])
  102.                     return false;
  103.             return true;
  104.         }
  105.  
  106.         public Form1()
  107.         {
  108.             InitializeComponent();
  109.         }
  110.  
  111.         private void button1_MouseClick(object sender, MouseEventArgs e)
  112.         {
  113.             MACHandler(comboBoxSelection);
  114.             selectAlgorithm(comboBoxSelection);
  115.             input = convert.StringToByteArray(inputTextString);
  116.             hashValue = hashAlgorithm.ComputeHash(input);
  117.             label2.Text = convert.ByteArrayToHexString(hashValue);
  118.         }
  119.  
  120.         private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
  121.         {
  122.             ComboBox combobox = new ComboBox();
  123.             comboBoxSelection = combobox.GetItemText(comboBox1.SelectedItem);
  124.         }
  125.        
  126.         class ConversionHandler
  127.         {
  128.  
  129.             public byte[] StringToByteArray(string s) { return CharArrayToByteArray(s.ToCharArray()); }
  130.  
  131.             public byte[] CharArrayToByteArray(char[] array) { return Encoding.ASCII.GetBytes(array, 0, array.Length); }
  132.  
  133.             public string ByteArrayToString(byte[] array)
  134.             {
  135.                 return Encoding.ASCII.GetString(array);
  136.             }
  137.  
  138.  
  139.             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; }
  140.  
  141.             public byte[] HexStringToByteArray(string s)
  142.             {
  143.                 byte[] array = new byte[s.Length / 2]; char[] chararray = s.ToCharArray(); int i; for (i = 0; i < s.Length / 2; i++)
  144.                 {
  145.                     array[i] = (byte)(((HexCharToNibble(chararray[2 * i]) << 4) & 0xF0) | ((HexCharToNibble(chararray[2 * i + 1]) & 0x0F)));
  146.  
  147.                 }
  148.                 return array;
  149.             }
  150.  
  151.             public string NibbleToHexString(byte nib) { string s; if (nib < 10) { s = nib.ToString(); } else { char c = (char)(nib + 55); s = c.ToString(); } return s; }
  152.  
  153.             public byte HexCharToNibble(char c)
  154.             {
  155.                 byte value = (byte)c; if (value < 65)
  156.                 {
  157.                     value = (byte)(value - 48);
  158.                 }
  159.                 else { value = (byte)(value - 55); }
  160.                 return value;
  161.             }
  162.         }
  163.  
  164.         private void textBox1_TextChanged(object sender, EventArgs e)
  165.         {
  166.             inputTextString = textBox1.Text;
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement