Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 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.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApp6
  13. {
  14. public partial class Form1 : Form
  15. {
  16. ConversionHandler c = new ConversionHandler();
  17. private HMAC myMAC;
  18.  
  19. public void MACHandler()
  20. {
  21. myMAC = new System.Security.Cryptography.HMACMD5();
  22. }
  23.  
  24. public bool CheckAuthenticity(byte[] mes, byte[] mac, byte[] key)
  25. {
  26. myMAC.Key = key;
  27. if (CompareByteArrays(myMAC.ComputeHash(mes), mac, myMAC.HashSize / 8) == true)
  28. { return true; }
  29. else
  30. return false;
  31. }
  32. public byte[] ComputeMAC(byte[] mes, byte[] key)
  33. {
  34. myMAC.Key = key;
  35. return myMAC.ComputeHash(mes);
  36. }
  37. public int MACByteLength()
  38. {
  39. return myMAC.HashSize / 8;
  40. }
  41.  
  42. private bool CompareByteArrays(byte[] a, byte[] b, int len)
  43. {
  44. for (int i = 0; i < len; i++)
  45. if (a[i] != b[i])
  46. return false;
  47. return true;
  48. }
  49.  
  50. public Form1()
  51. {
  52. InitializeComponent();
  53. }
  54.  
  55. private void Form1_Load(object sender, EventArgs e)
  56. {
  57.  
  58. }
  59.  
  60.  
  61. private void button1_Click(object sender, EventArgs e)
  62. {
  63. MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
  64. byte[] hashValue = myMD5.ComputeHash(c.StringToByteArray(textBox1.Text));
  65. textBox3.Text = c.ByteArrayToHexString(hashValue);
  66.  
  67. }
  68.  
  69. private void textBox3_TextChanged(object sender, EventArgs e)
  70. {
  71.  
  72. }
  73.  
  74. private void textBox4_TextChanged(object sender, EventArgs e)
  75. {
  76.  
  77. }
  78.  
  79. private void button2_Click(object sender, EventArgs e)
  80. {
  81. MACHandler();
  82. byte[] rezultat = ComputeMAC(c.StringToByteArray(textBox1.Text), c.StringToByteArray(textBox4.Text));
  83.  
  84.  
  85. textBox2.Text = c.ByteArrayToHexString(rezultat);
  86. }
  87.  
  88. private void textBox2_TextChanged(object sender, EventArgs e)
  89. {
  90.  
  91. }
  92.  
  93. private void textBox1_TextChanged_1(object sender, EventArgs e)
  94. {
  95.  
  96. }
  97.  
  98. private void button3_Click(object sender, EventArgs e)
  99. {
  100. MACHandler();
  101. byte[] rezultat = ComputeMAC(c.StringToByteArray(textBox1.Text), c.StringToByteArray(textBox4.Text));
  102. if (CheckAuthenticity(c.StringToByteArray(textBox1.Text), rezultat, c.StringToByteArray(textBox4.Text)) == true)
  103. { System.Windows.Forms.MessageBox.Show("MAC OK !!!"); }
  104. else { System.Windows.Forms.MessageBox.Show("MAC NOT OK !!!"); }
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement