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 WindowsFormsApp6
- {
- public partial class Form1 : Form
- {
- ConversionHandler c = new ConversionHandler();
- private HMAC myMAC;
- public void MACHandler()
- {
- myMAC = new System.Security.Cryptography.HMACMD5();
- }
- 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 Form1_Load(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
- byte[] hashValue = myMD5.ComputeHash(c.StringToByteArray(textBox1.Text));
- textBox3.Text = c.ByteArrayToHexString(hashValue);
- }
- private void textBox3_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox4_TextChanged(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- MACHandler();
- byte[] rezultat = ComputeMAC(c.StringToByteArray(textBox1.Text), c.StringToByteArray(textBox4.Text));
- textBox2.Text = c.ByteArrayToHexString(rezultat);
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox1_TextChanged_1(object sender, EventArgs e)
- {
- }
- private void button3_Click(object sender, EventArgs e)
- {
- MACHandler();
- byte[] rezultat = ComputeMAC(c.StringToByteArray(textBox1.Text), c.StringToByteArray(textBox4.Text));
- if (CheckAuthenticity(c.StringToByteArray(textBox1.Text), rezultat, c.StringToByteArray(textBox4.Text)) == true)
- { System.Windows.Forms.MessageBox.Show("MAC OK !!!"); }
- else { System.Windows.Forms.MessageBox.Show("MAC NOT OK !!!"); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement