Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 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 WindowsFormsApp8
  13. {
  14. public partial class Form1 : Form
  15. {
  16.  
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. ConversionHandler c = new ConversionHandler();
  22. class ConversionHandler
  23. {
  24.  
  25. public byte[] StringToByteArray(string s)
  26. {
  27. return CharArrayToByteArray(s.ToCharArray());
  28. }
  29.  
  30. public byte[] CharArrayToByteArray(char[] array)
  31. {
  32. return Encoding.ASCII.GetBytes(array, 0, array.Length);
  33. }
  34.  
  35. public string ByteArrayToString(byte[] array)
  36. {
  37. return Encoding.ASCII.GetString(array);
  38. }
  39.  
  40. public string ByteArrayToHexString(byte[] array)
  41. {
  42. string s = ""; int i; for (i = 0; i < array.Length; i++)
  43. {
  44. s = s + NibbleToHexString((byte)((array[i] >> 4) & 0x0F)) + NibbleToHexString((byte)(array[i] & 0x0F));
  45. }
  46. return s;
  47. }
  48.  
  49. public byte[] HexStringToByteArray(string s)
  50. {
  51. byte[] array = new byte[s.Length / 2];
  52. char[] chararray = s.ToCharArray(); int i;
  53. for (i = 0; i < s.Length / 2; i++)
  54. {
  55. array[i] = (byte)(((HexCharToNibble(chararray[2 * i]) << 4) & 0xF0) | ((HexCharToNibble(chararray[2 * i + 1]) & 0x0F)));
  56. }
  57. return array;
  58. }
  59.  
  60. public string NibbleToHexString(byte nib)
  61. {
  62. string s; if (nib < 10)
  63. {
  64. s = nib.ToString();
  65. }
  66. else
  67. {
  68. char c = (char)(nib + 55);
  69. s = c.ToString();
  70. }
  71. return s;
  72. }
  73.  
  74. public byte HexCharToNibble(char c)
  75. {
  76. byte value = (byte)c; if (value < 65)
  77. {
  78. value = (byte)(value - 48);
  79. }
  80. else { value = (byte)(value - 55);
  81. }
  82. return value;
  83. }
  84. }
  85. /* MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
  86. RandomNumberGenerator rnd = RandomNumberGenerator.Create();
  87. byte[] input = new byte[20];
  88. byte[] hashValue;
  89. //generates some random input
  90. rnd.GetBytes(input);
  91. //computes the hash
  92. hashValue = myMD5.ComputeHash(input);
  93. */
  94.  
  95. public bool CheckAutenticity(byte[] mes, byte[] mac, byte[] key)
  96. {
  97. myMAC.Key = key;
  98. if (CompareByteArrays(myMAC.ComputeHash(mes), mac, myMAC.HashSize / 8) == true)
  99. {
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107.  
  108.  
  109. public int MACByteLength()
  110. {
  111. return myMAC.HashSize / 8;
  112. }
  113.  
  114. private bool CompareByteArrays(byte[] a, byte[] b, int len)
  115. {
  116. for (int i = 0; i < len; i++)
  117. if (a[i] != b[i])
  118. return false;
  119. return true;
  120. }
  121.  
  122.  
  123. RandomNumberGenerator rnd = RandomNumberGenerator.Create();
  124. byte[] key = new byte[16];
  125.  
  126.  
  127.  
  128.  
  129.  
  130. private void label7_Click(object sender, EventArgs e)
  131. {
  132.  
  133. }
  134.  
  135. private void label2_Click(object sender, EventArgs e)
  136. {
  137.  
  138. }
  139.  
  140. private void label4_Click(object sender, EventArgs e)
  141. {
  142.  
  143. }
  144. private HMAC myMAC;
  145. public void MACHandler()
  146. {
  147.  
  148.  
  149. myMAC = new System.Security.Cryptography.HMACMD5();
  150.  
  151.  
  152. }
  153. public byte[] ComputeMAC(byte[] mes, byte[] key)
  154. {
  155. myMAC.Key = key;
  156. return myMAC.ComputeHash(mes);
  157.  
  158. }
  159. private void button2_Click(object sender, EventArgs e)
  160. {
  161. MACHandler();
  162. byte[] a=ComputeMAC(c.StringToByteArray(textBox4.Text),c.StringToByteArray(textBox1.Text));
  163. textBox3.Text = c.ByteArrayToString(a);
  164. }
  165.  
  166. private void Form1_Load(object sender, EventArgs e)
  167. {
  168.  
  169. }
  170.  
  171. private void button1_Click(object sender, EventArgs e)
  172. {
  173. MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
  174. RandomNumberGenerator rnd = RandomNumberGenerator.Create();
  175. byte[] input = c.StringToByteArray(textBox1.Text);
  176. byte[] hashValue;
  177.  
  178. hashValue = myMD5.ComputeHash(input);
  179. textBox2.Text = c.ByteArrayToString(hashValue);
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement