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.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApp8
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- ConversionHandler c = new ConversionHandler();
- 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;
- }
- }
- /* 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);
- */
- public bool CheckAutenticity(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 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;
- }
- RandomNumberGenerator rnd = RandomNumberGenerator.Create();
- byte[] key = new byte[16];
- private void label7_Click(object sender, EventArgs e)
- {
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void label4_Click(object sender, EventArgs e)
- {
- }
- private HMAC myMAC;
- public void MACHandler()
- {
- myMAC = new System.Security.Cryptography.HMACMD5();
- }
- public byte[] ComputeMAC(byte[] mes, byte[] key)
- {
- myMAC.Key = key;
- return myMAC.ComputeHash(mes);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- MACHandler();
- byte[] a=ComputeMAC(c.StringToByteArray(textBox4.Text),c.StringToByteArray(textBox1.Text));
- textBox3.Text = c.ByteArrayToString(a);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
- RandomNumberGenerator rnd = RandomNumberGenerator.Create();
- byte[] input = c.StringToByteArray(textBox1.Text);
- byte[] hashValue;
- hashValue = myMD5.ComputeHash(input);
- textBox2.Text = c.ByteArrayToString(hashValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement