/CRCBreaker/MainWindow.xaml.cs

https://github.com/Comfy-Mittens/Digital-World · C# · 230 lines · 198 code · 29 blank · 3 comment · 41 complexity · 40187218010b1735d723b4043445e2fa MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Threading;
  15. namespace CRCBreaker
  16. {
  17. /// <summary>
  18. /// Interaction logic for MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. CRC16 c16;
  23. CRC16CCITT c162;
  24. Thread thdC1;
  25. Thread thdC2;
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. c16 = new CRC16();
  30. c162 = new CRC16CCITT( CRC16CCITT.InitialCrcValue.Zeros);
  31. tCRC16Poly.Text = "0x" + c16.poly.ToString("X");
  32. tCRC162Poly.Text = "0x"+c162.poly.ToString("X");
  33. tCRC162IV.Text = "0x"+c162.initialValue.ToString("X");
  34. }
  35. private void button1_Click(object sender, RoutedEventArgs e)
  36. {
  37. c16.poly = Parse(tCRC16Poly.Text);
  38. c162.poly = Parse(tCRC162Poly.Text);
  39. c162.initialValue = Parse(tCRC162IV.Text);
  40. tCRC16.Text = Visualize(c16.ComputeChecksumBytes(buffer()));
  41. tCRC162.Text = Visualize(c162.ComputeChecksumBytes(buffer()));
  42. }
  43. private ushort Parse(string s)
  44. {
  45. if (s.StartsWith("0x"))
  46. {
  47. return ushort.Parse(s.Substring(2), System.Globalization.NumberStyles.HexNumber);
  48. }
  49. else
  50. return ushort.Parse(s);
  51. }
  52. private byte[] buffer()
  53. {
  54. string[] rawData = tData.Text.Trim().Split(' ');
  55. byte[] buffer = new byte[rawData.Length];
  56. for (int i = 0; i < rawData.Length; i++)
  57. {
  58. buffer[i] = byte.Parse(rawData[i], System.Globalization.NumberStyles.HexNumber);
  59. }
  60. return buffer;
  61. }
  62. private string Visualize(byte[] buffer)
  63. {
  64. StringBuilder sb = new StringBuilder();
  65. for (int i = 0; i < buffer.Length; i++)
  66. {
  67. sb.Append(buffer[i].ToString("X2"));
  68. if (i < buffer.Length - 1)
  69. sb.Append(" ");
  70. }
  71. return sb.ToString();
  72. }
  73. private void btnGuess1_Click(object sender, RoutedEventArgs e)
  74. {
  75. tCRC16Poly.Text = ushort.MinValue.ToString("X");
  76. thdC1 = new Thread(new ParameterizedThreadStart(Guess1));
  77. thdC1.IsBackground = true;
  78. thdC1.Start(buffer());
  79. }
  80. private void btnGuess2_Click(object sender, RoutedEventArgs e)
  81. {
  82. thdC2 = new Thread(new ParameterizedThreadStart(Guess2));
  83. thdC2.IsBackground = true;
  84. thdC2.Start(buffer());
  85. }
  86. private void Guess1(object o)
  87. {
  88. byte[] ok = new byte[] { 0xf7, 0x1a };
  89. byte[] buffer = (byte[]) o;
  90. try
  91. {
  92. for (ushort crc_poly = ushort.MinValue; crc_poly < ushort.MaxValue; crc_poly++)
  93. {
  94. c16.poly = crc_poly++;
  95. c16.GenTable(c16.poly);
  96. byte[] hash = c16.ComputeChecksumBytes(buffer);
  97. tCRC16Poly.Dispatcher.BeginInvoke(new Action(() =>
  98. {
  99. tCRC16Poly.Text = "0x" + (crc_poly).ToString("X");
  100. tCRC16.Text = Visualize(hash);
  101. }));
  102. if (hash.Equals(ok)) break;
  103. if (hash[0] == ok[0] && hash[1] == ok[1]) break;
  104. if (hash[0] == ok[1] && hash[1] == ok[0]) break;
  105. Thread.Sleep(5);
  106. if (crc_poly == ushort.MaxValue) break;
  107. }
  108. }
  109. catch
  110. {
  111. }
  112. }
  113. private void Guess2(object o)
  114. {
  115. byte[] ok = new byte[] { 0xf7, 0x1a };
  116. byte[] buffer = (byte[])o;
  117. try
  118. {
  119. for(ushort ccitt_poly = 1; ccitt_poly <= ushort.MaxValue; ccitt_poly++)
  120. {
  121. for (ushort ccitt_iv = ushort.MinValue; ccitt_iv <= ushort.MaxValue; ccitt_iv++)
  122. {
  123. c162.initialValue = ccitt_iv;
  124. c162.poly = ccitt_poly;
  125. c162.GenTable(ccitt_iv, ccitt_poly);
  126. byte[] hash = c162.ComputeChecksumBytes(buffer);
  127. tCRC162Poly.Dispatcher.BeginInvoke(new Action(() =>
  128. {
  129. tCRC162Poly.Text = "0x" + ccitt_poly.ToString("X");
  130. tCRC162IV.Text = "0x" + ccitt_iv.ToString("X");
  131. tCRC162.Text = Visualize(hash);
  132. }));
  133. if (hash.Equals(ok)) break;
  134. if (hash[0] == ok[0] && hash[1] == ok[1]) break;
  135. if (hash[0] == ok[1] && hash[1] == ok[0]) break;
  136. Thread.Sleep(5);
  137. if (ccitt_iv == ushort.MaxValue) break;
  138. }
  139. if (ccitt_poly == ushort.MaxValue) break;
  140. }
  141. }
  142. catch
  143. {
  144. }
  145. }
  146. private void btnCalc_Click(object sender, RoutedEventArgs e)
  147. {
  148. DateTime d = DateTime.Now; ;
  149. if (tCRC16.Text != "")
  150. {
  151. d = new DateTime(1970, 1, 1).AddSeconds(double.Parse(tCRC16.Text));
  152. }
  153. else if (tCRC162.Text != "")
  154. {
  155. d = new DateTime(1970, 1, 1).AddSeconds(double.Parse(tCRC162.Text));
  156. Random rr = new Random((int)d.Ticks);
  157. byte[] zbuffer = new byte[334];
  158. rr.NextBytes(zbuffer);
  159. tData.Text = See(zbuffer);
  160. return;
  161. }
  162. else
  163. d = dpTime.SelectedDate.Value.AddHours(double.Parse(tH.Text)).AddMinutes(double.Parse(tM.Text)).AddSeconds(double.Parse(tS.Text));
  164. DateTime k = TimeZoneInfo.ConvertTimeFromUtc(d.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("Korea Standard Time"));
  165. Random r = new Random((int)k.Ticks);
  166. byte[] buffer = new byte[334];
  167. r.NextBytes(buffer);
  168. tData.Text = See(buffer);
  169. }
  170. public static string See(byte[] buffer)
  171. {
  172. StringBuilder sb = new StringBuilder();
  173. int rows = (int)Math.Ceiling(buffer.Length / 16.0);
  174. for (int i = 0; i < rows; i++)
  175. {
  176. StringBuilder text = new StringBuilder();
  177. for (int col = 0; col < 16; col++)
  178. {
  179. int pos = col + (i * 16);
  180. if (pos < buffer.Length)
  181. {
  182. sb.Append(buffer[pos].ToString("X2"));
  183. if (buffer[pos] > 32 && buffer[pos] < 126)
  184. text.Append((char)buffer[pos]);
  185. else
  186. text.Append(".");
  187. }
  188. else
  189. {
  190. sb.Append(" ");
  191. text.Append(" ");
  192. }
  193. sb.Append(" ");
  194. }
  195. sb.Append(" ");
  196. sb.AppendLine(text.ToString());
  197. }
  198. return sb.ToString();
  199. }
  200. }
  201. }