Win32Api.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Security.Cryptography;
  6. using System.ComponentModel;
  7. using Y.FileQueryEngine.Win32.Constants;
  8. using Y.FileQueryEngine.Win32.Structures;
  9. namespace Y.FileQueryEngine.Win32
  10. {
  11. internal class Win32Api
  12. {
  13. private const string KERNEL32DLL = "kernel32.dll";
  14. private const string USER32DLL = "user32.dll";
  15. #region dll imports
  16. /// <summary>
  17. /// Creates the file specified by 'lpFileName' with desired access, share mode, security attributes,
  18. /// creation disposition, flags and attributes.
  19. /// </summary>
  20. /// <param name="lpFileName">Fully qualified path to a file</param>
  21. /// <param name="dwDesiredAccess">Requested access (write, read, read/write, none)</param>
  22. /// <param name="dwShareMode">Share mode (read, write, read/write, delete, all, none)</param>
  23. /// <param name="lpSecurityAttributes">IntPtr to a 'SECURITY_ATTRIBUTES' structure</param>
  24. /// <param name="dwCreationDisposition">Action to take on file or device specified by 'lpFileName' (CREATE_NEW,
  25. /// CREATE_ALWAYS, OPEN_ALWAYS, OPEN_EXISTING, TRUNCATE_EXISTING)</param>
  26. /// <param name="dwFlagsAndAttributes">File or device attributes and flags (typically FILE_ATTRIBUTE_NORMAL)</param>
  27. /// <param name="hTemplateFile">IntPtr to a valid handle to a template file with 'GENERIC_READ' access right</param>
  28. /// <returns>IntPtr handle to the 'lpFileName' file or device or 'INVALID_HANDLE_VALUE'</returns>
  29. [DllImport(KERNEL32DLL, SetLastError = true)]
  30. public static extern IntPtr CreateFile(string lpFileName,
  31. uint dwDesiredAccess,
  32. uint dwShareMode,
  33. IntPtr lpSecurityAttributes,
  34. uint dwCreationDisposition,
  35. uint dwFlagsAndAttributes,
  36. IntPtr hTemplateFile);
  37. /// <summary>
  38. /// Closes the file specified by the IntPtr 'hObject'.
  39. /// </summary>
  40. /// <param name="hObject">IntPtr handle to a file</param>
  41. /// <returns>'true' if successful, otherwise 'false'</returns>
  42. [DllImport(KERNEL32DLL, SetLastError = true)]
  43. [return: MarshalAs(UnmanagedType.Bool)]
  44. public static extern bool CloseHandle(
  45. IntPtr hObject);
  46. /// <summary>
  47. /// Fills the 'BY_HANDLE_FILE_INFORMATION' structure for the file specified by 'hFile'.
  48. /// </summary>
  49. /// <param name="hFile">Fully qualified name of a file</param>
  50. /// <param name="lpFileInformation">Out BY_HANDLE_FILE_INFORMATION argument</param>
  51. /// <returns>'true' if successful, otherwise 'false'</returns>
  52. [DllImport(KERNEL32DLL, SetLastError = true)]
  53. [return: MarshalAs(UnmanagedType.Bool)]
  54. public static extern bool GetFileInformationByHandle(
  55. IntPtr hFile,
  56. out BY_HANDLE_FILE_INFORMATION lpFileInformation);
  57. /// <summary>
  58. /// Deletes the file specified by 'fileName'.
  59. /// </summary>
  60. /// <param name="fileName">Fully qualified path to the file to delete</param>
  61. /// <returns>'true' if successful, otherwise 'false'</returns>
  62. [DllImport(KERNEL32DLL, SetLastError = true)]
  63. [return: MarshalAs(UnmanagedType.Bool)]
  64. public static extern bool DeleteFile(
  65. string fileName);
  66. /// <summary>
  67. /// Read data from the file specified by 'hFile'.
  68. /// </summary>
  69. /// <param name="hFile">IntPtr handle to the file to read</param>
  70. /// <param name="lpBuffer">IntPtr to a buffer of bytes to receive the bytes read from 'hFile'</param>
  71. /// <param name="nNumberOfBytesToRead">Number of bytes to read from 'hFile'</param>
  72. /// <param name="lpNumberOfBytesRead">Number of bytes read from 'hFile'</param>
  73. /// <param name="lpOverlapped">IntPtr to an 'OVERLAPPED' structure</param>
  74. /// <returns>'true' if successful, otherwise 'false'</returns>
  75. [DllImport(KERNEL32DLL)]
  76. [return: MarshalAs(UnmanagedType.Bool)]
  77. public static extern bool ReadFile(
  78. IntPtr hFile,
  79. IntPtr lpBuffer,
  80. uint nNumberOfBytesToRead,
  81. out uint lpNumberOfBytesRead,
  82. IntPtr lpOverlapped);
  83. /// <summary>
  84. /// Writes the
  85. /// </summary>
  86. /// <param name="hFile">IntPtr handle to the file to write</param>
  87. /// <param name="bytes">IntPtr to a buffer of bytes to write to 'hFile'</param>
  88. /// <param name="nNumberOfBytesToWrite">Number of bytes in 'lpBuffer' to write to 'hFile'</param>
  89. /// <param name="lpNumberOfBytesWritten">Number of bytes written to 'hFile'</param>
  90. /// <param name="overlapped">IntPtr to an 'OVERLAPPED' structure</param>
  91. /// <returns>'true' if successful, otherwise 'false'</returns>
  92. [DllImport(KERNEL32DLL, SetLastError = true, CharSet = CharSet.Unicode)]
  93. [return: MarshalAs(UnmanagedType.Bool)]
  94. public static extern bool WriteFile(
  95. IntPtr hFile,
  96. IntPtr bytes,
  97. uint nNumberOfBytesToWrite,
  98. out uint lpNumberOfBytesWritten,
  99. int overlapped);
  100. /// <summary>
  101. /// Writes the data in 'lpBuffer' to the file specified by 'hFile'.
  102. /// </summary>
  103. /// <param name="hFile">IntPtr handle to file to write</param>
  104. /// <param name="lpBuffer">Buffer of bytes to write to file 'hFile'</param>
  105. /// <param name="nNumberOfBytesToWrite">Number of bytes in 'lpBuffer' to write to 'hFile'</param>
  106. /// <param name="lpNumberOfBytesWritten">Number of bytes written to 'hFile'</param>
  107. /// <param name="overlapped">IntPtr to an 'OVERLAPPED' structure</param>
  108. /// <returns>'true' if successful, otherwise 'false'</returns>
  109. [DllImport(KERNEL32DLL, SetLastError = true, CharSet = CharSet.Unicode)]
  110. [return: MarshalAs(UnmanagedType.Bool)]
  111. public static extern bool WriteFile(
  112. IntPtr hFile,
  113. byte[] lpBuffer,
  114. uint nNumberOfBytesToWrite,
  115. out uint lpNumberOfBytesWritten,
  116. int overlapped);
  117. /// <summary>
  118. /// Sends the 'dwIoControlCode' to the device specified by 'hDevice'.
  119. /// </summary>
  120. /// <param name="hDevice">IntPtr handle to the device to receive 'dwIoControlCode'</param>
  121. /// <param name="dwIoControlCode">Device IO Control Code to send</param>
  122. /// <param name="lpInBuffer">Input buffer if required</param>
  123. /// <param name="nInBufferSize">Size of input buffer</param>
  124. /// <param name="lpOutBuffer">Output buffer if required</param>
  125. /// <param name="nOutBufferSize">Size of output buffer</param>
  126. /// <param name="lpBytesReturned">Number of bytes returned in output buffer</param>
  127. /// <param name="lpOverlapped">IntPtr to an 'OVERLAPPED' structure</param>
  128. /// <returns>'true' if successful, otherwise 'false'</returns>
  129. [DllImport(KERNEL32DLL, ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
  130. [return: MarshalAs(UnmanagedType.Bool)]
  131. public static extern bool DeviceIoControl(
  132. IntPtr hDevice,
  133. UInt32 dwIoControlCode,
  134. IntPtr lpInBuffer,
  135. Int32 nInBufferSize,
  136. out USN_JOURNAL_DATA lpOutBuffer,
  137. Int32 nOutBufferSize,
  138. out uint lpBytesReturned,
  139. IntPtr lpOverlapped);
  140. /// <summary>
  141. /// Sends the control code 'dwIoControlCode' to the device driver specified by 'hDevice'.
  142. /// </summary>
  143. /// <param name="hDevice">IntPtr handle to the device to receive 'dwIoControlCode</param>
  144. /// <param name="dwIoControlCode">Device IO Control Code to send</param>
  145. /// <param name="lpInBuffer">Input buffer if required</param>
  146. /// <param name="nInBufferSize">Size of input buffer </param>
  147. /// <param name="lpOutBuffer">Output buffer if required</param>
  148. /// <param name="nOutBufferSize">Size of output buffer</param>
  149. /// <param name="lpBytesReturned">Number of bytes returned</param>
  150. /// <param name="lpOverlapped">Pointer to an 'OVERLAPPED' struture</param>
  151. /// <returns></returns>
  152. [DllImport(KERNEL32DLL, ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
  153. [return: MarshalAs(UnmanagedType.Bool)]
  154. public static extern bool DeviceIoControl(
  155. IntPtr hDevice,
  156. UInt32 dwIoControlCode,
  157. IntPtr lpInBuffer,
  158. Int32 nInBufferSize,
  159. IntPtr lpOutBuffer,
  160. Int32 nOutBufferSize,
  161. out uint lpBytesReturned,
  162. IntPtr lpOverlapped);
  163. /// <summary>
  164. /// Sets the number of bytes specified by 'size' of the memory associated with the argument 'ptr'
  165. /// to zero.
  166. /// </summary>
  167. /// <param name="ptr"></param>
  168. /// <param name="size"></param>
  169. [DllImport(KERNEL32DLL)]
  170. public static extern void ZeroMemory(IntPtr ptr, int size);
  171. [DllImport(USER32DLL, CharSet = CharSet.Auto)]
  172. [return: MarshalAs(UnmanagedType.Bool)]
  173. public static extern bool GetCursorPos(out POINT pt);
  174. [DllImport(USER32DLL, CharSet = CharSet.Auto)]
  175. public static extern Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex);
  176. [DllImport(USER32DLL, CharSet = CharSet.Auto)]
  177. public static extern Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 newVal);
  178. #endregion
  179. #region functions
  180. /// <summary>
  181. /// Writes the data in 'text' to the alternate stream ':Description' of the file 'currentFile.
  182. /// </summary>
  183. /// <param name="currentfile">Fully qualified path to a file</param>
  184. /// <param name="text">Data to write to the ':Description' stream</param>
  185. public static void WriteAlternateStream(string currentfile, string text)
  186. {
  187. string AltStreamDesc = currentfile + ":Description";
  188. IntPtr txtBuffer = IntPtr.Zero;
  189. IntPtr hFile = IntPtr.Zero;
  190. DeleteFile(AltStreamDesc);
  191. string descText = text.TrimEnd(' ');
  192. try
  193. {
  194. hFile = CreateFile(AltStreamDesc, Win32ApiConstant.GENERIC_WRITE, 0, IntPtr.Zero,
  195. Win32ApiConstant.CREATE_ALWAYS, 0, IntPtr.Zero);
  196. if (-1 != hFile.ToInt32())
  197. {
  198. txtBuffer = Marshal.StringToHGlobalUni(descText);
  199. uint nBytes, count;
  200. nBytes = (uint)descText.Length;
  201. bool bRtn = WriteFile(hFile, txtBuffer, sizeof(char) * nBytes, out count, 0);
  202. if (!bRtn)
  203. {
  204. if ((sizeof(char) * nBytes) != count)
  205. {
  206. throw new Exception(string.Format("Bytes written {0} should be {1} for file {2}.",
  207. count, sizeof(char) * nBytes, AltStreamDesc));
  208. }
  209. else
  210. {
  211. throw new Exception("WriteFile() returned false");
  212. }
  213. }
  214. }
  215. else
  216. {
  217. throw new Win32Exception(Marshal.GetLastWin32Error());
  218. }
  219. }
  220. catch (Exception exception)
  221. {
  222. //string msg = string.Format("Exception caught in WriteAlternateStream()\n '{0}'\n for file '{1}'.",
  223. // exception.Message, AltStreamDesc);
  224. //Console.WriiteLine(msg);
  225. throw;
  226. }
  227. finally
  228. {
  229. CloseHandle(hFile);
  230. hFile = IntPtr.Zero;
  231. Marshal.FreeHGlobal(txtBuffer);
  232. GC.Collect();
  233. }
  234. }
  235. /// <summary>
  236. /// Adds the ':Description' alternate stream name to the argument 'currentFile'.
  237. /// </summary>
  238. /// <param name="currentfile">The file whose alternate stream is to be read</param>
  239. /// <returns>A string value representing the value of the alternate stream</returns>
  240. public static string ReadAlternateStream(string currentfile)
  241. {
  242. string AltStreamDesc = currentfile + ":Description";
  243. string returnstring = ReadAlternateStreamEx(AltStreamDesc);
  244. return returnstring;
  245. }
  246. /// <summary>
  247. /// Reads the stream represented by 'currentFile'.
  248. /// </summary>
  249. /// <param name="currentfile">Fully qualified path including stream</param>
  250. /// <returns>Value of the alternate stream as a string</returns>
  251. public static string ReadAlternateStreamEx(string currentfile)
  252. {
  253. string returnstring = string.Empty;
  254. IntPtr hFile = IntPtr.Zero;
  255. IntPtr buffer = IntPtr.Zero;
  256. try
  257. {
  258. hFile = CreateFile(currentfile, Win32ApiConstant.GENERIC_READ, 0, IntPtr.Zero, Win32ApiConstant.OPEN_EXISTING, 0, IntPtr.Zero);
  259. if (-1 != hFile.ToInt32())
  260. {
  261. buffer = Marshal.AllocHGlobal(1000 * sizeof(char));
  262. ZeroMemory(buffer, 1000 * sizeof(char));
  263. uint nBytes;
  264. bool bRtn = ReadFile(hFile, buffer, 1000 * sizeof(char), out nBytes, IntPtr.Zero);
  265. if (bRtn)
  266. {
  267. if (nBytes > 0)
  268. {
  269. returnstring = Marshal.PtrToStringAuto(buffer);
  270. //byte[] byteBuffer = new byte[nBytes];
  271. //for (int i = 0; i < nBytes; i++)
  272. //{
  273. // byteBuffer[i] = Marshal.ReadByte(buffer, i);
  274. //}
  275. //returnstring = Encoding.Unicode.GetString(byteBuffer, 0, (int)nBytes);
  276. }
  277. else
  278. {
  279. throw new Exception("ReadFile() returned true but read zero bytes");
  280. }
  281. }
  282. else
  283. {
  284. if (nBytes <= 0)
  285. {
  286. throw new Exception("ReadFile() read zero bytes.");
  287. }
  288. else
  289. {
  290. throw new Exception("ReadFile() returned false");
  291. }
  292. }
  293. }
  294. else
  295. {
  296. Exception excptn = new Win32Exception(Marshal.GetLastWin32Error());
  297. if (!excptn.Message.Contains("cannot find the file"))
  298. {
  299. throw excptn;
  300. }
  301. }
  302. }
  303. catch (Exception exception)
  304. {
  305. //string msg = string.Format("Exception caught in ReadAlternateStream(), '{0}'\n for file '{1}'.",
  306. // exception.Message, currentfile);
  307. //Console.WriteLine(msg);
  308. //Console.WriteLine(exception.Message);
  309. throw;
  310. }
  311. finally
  312. {
  313. CloseHandle(hFile);
  314. hFile = IntPtr.Zero;
  315. if (buffer != IntPtr.Zero)
  316. {
  317. Marshal.FreeHGlobal(buffer);
  318. }
  319. GC.Collect();
  320. }
  321. return returnstring;
  322. }
  323. /// <summary>
  324. /// Read the encrypted alternate stream specified by 'currentFile'.
  325. /// </summary>
  326. /// <param name="currentfile">Fully qualified path to encrypted alternate stream</param>
  327. /// <returns>The un-encrypted value of the alternate stream as a string</returns>
  328. public static string ReadAlternateStreamEncrypted(string currentfile)
  329. {
  330. string returnstring = string.Empty;
  331. IntPtr buffer = IntPtr.Zero;
  332. IntPtr hFile = IntPtr.Zero;
  333. try
  334. {
  335. hFile = CreateFile(currentfile, Win32ApiConstant.GENERIC_READ, 0, IntPtr.Zero, Win32ApiConstant.OPEN_EXISTING, 0, IntPtr.Zero);
  336. if (-1 != hFile.ToInt32())
  337. {
  338. buffer = Marshal.AllocHGlobal(1000 * sizeof(char));
  339. ZeroMemory(buffer, 1000 * sizeof(char));
  340. uint nBytes;
  341. bool bRtn = ReadFile(hFile, buffer, 1000 * sizeof(char), out nBytes, IntPtr.Zero);
  342. if (0 != nBytes)
  343. {
  344. returnstring = DecryptLicenseString(buffer, nBytes);
  345. }
  346. }
  347. else
  348. {
  349. Exception excptn = new Win32Exception(Marshal.GetLastWin32Error());
  350. if (!excptn.Message.Contains("cannot find the file"))
  351. {
  352. throw excptn;
  353. }
  354. }
  355. }
  356. catch (Exception exception)
  357. {
  358. Console.WriteLine("Exception caught in ReadAlternateStreamEncrypted()");
  359. Console.WriteLine(exception.Message);
  360. }
  361. finally
  362. {
  363. CloseHandle(hFile);
  364. hFile = IntPtr.Zero;
  365. if (buffer != IntPtr.Zero)
  366. {
  367. Marshal.FreeHGlobal(buffer);
  368. }
  369. GC.Collect();
  370. }
  371. return returnstring;
  372. }
  373. /// <summary>
  374. /// Writes the value of 'LicenseString' as an encrypted stream to the file:stream specified
  375. /// by 'currentFile'.
  376. /// </summary>
  377. /// <param name="currentFile">Fully qualified path to the alternate stream</param>
  378. /// <param name="LicenseString">The string value to encrypt and write to the alternate stream</param>
  379. public static void WriteAlternateStreamEncrypted(string currentFile, string LicenseString)
  380. {
  381. RC2CryptoServiceProvider rc2 = null;
  382. CryptoStream cs = null;
  383. MemoryStream ms = null;
  384. uint count = 0;
  385. IntPtr buffer = IntPtr.Zero;
  386. IntPtr hFile = IntPtr.Zero;
  387. try
  388. {
  389. Encoding enc = Encoding.Unicode;
  390. byte[] ba = enc.GetBytes(LicenseString);
  391. ms = new MemoryStream();
  392. rc2 = new RC2CryptoServiceProvider();
  393. rc2.Key = GetBytesFromHexString("7a6823a42a3a3ae27057c647db812d0");
  394. rc2.IV = GetBytesFromHexString("827d961224d99b2d");
  395. cs = new CryptoStream(ms, rc2.CreateEncryptor(), CryptoStreamMode.Write);
  396. cs.Write(ba, 0, ba.Length);
  397. cs.FlushFinalBlock();
  398. buffer = Marshal.AllocHGlobal(1000 * sizeof(char));
  399. ZeroMemory(buffer, 1000 * sizeof(char));
  400. uint nBytes = (uint)ms.Length;
  401. Marshal.Copy(ms.GetBuffer(), 0, buffer, (int)nBytes);
  402. DeleteFile(currentFile);
  403. hFile = CreateFile(currentFile, Win32ApiConstant.GENERIC_WRITE, 0, IntPtr.Zero,
  404. Win32ApiConstant.CREATE_ALWAYS, 0, IntPtr.Zero);
  405. if (-1 != hFile.ToInt32())
  406. {
  407. bool bRtn = WriteFile(hFile, buffer, nBytes, out count, 0);
  408. }
  409. else
  410. {
  411. Exception excptn = new Win32Exception(Marshal.GetLastWin32Error());
  412. if (!excptn.Message.Contains("cannot find the file"))
  413. {
  414. throw excptn;
  415. }
  416. }
  417. }
  418. catch (Exception exception)
  419. {
  420. Console.WriteLine("WriteAlternateStreamEncrypted()");
  421. Console.WriteLine(exception.Message);
  422. }
  423. finally
  424. {
  425. CloseHandle(hFile);
  426. hFile = IntPtr.Zero;
  427. if (cs != null)
  428. {
  429. cs.Close();
  430. cs.Dispose();
  431. }
  432. rc2 = null;
  433. if (ms != null)
  434. {
  435. ms.Close();
  436. ms.Dispose();
  437. }
  438. if (buffer != IntPtr.Zero)
  439. {
  440. Marshal.FreeHGlobal(buffer);
  441. }
  442. }
  443. }
  444. /// <summary>
  445. /// Encrypt the string 'LicenseString' argument and return as a MemoryStream.
  446. /// </summary>
  447. /// <param name="LicenseString">The string value to encrypt</param>
  448. /// <returns>A MemoryStream which contains the encrypted value of 'LicenseString'</returns>
  449. private static MemoryStream EncryptLicenseString(string LicenseString)
  450. {
  451. Encoding enc = Encoding.Unicode;
  452. byte[] ba = enc.GetBytes(LicenseString);
  453. MemoryStream ms = new MemoryStream();
  454. RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
  455. rc2.Key = GetBytesFromHexString("7a6823a42a3a3ae27057c647db812d0");
  456. rc2.IV = GetBytesFromHexString("827d961224d99b2d");
  457. CryptoStream cs = new CryptoStream(ms, rc2.CreateEncryptor(), CryptoStreamMode.Write);
  458. cs.Write(ba, 0, ba.Length);
  459. cs.Close();
  460. cs.Dispose();
  461. rc2 = null;
  462. return ms;
  463. }
  464. /// <summary>
  465. /// Given an IntPtr to a bufer and the number of bytes, decrypt the buffer and return an
  466. /// unencrypted text string.
  467. /// </summary>
  468. /// <param name="buffer">An IntPtr to the 'buffer' containing the encrypted string</param>
  469. /// <param name="nBytes">The number of bytes in 'buffer' to decrypt</param>
  470. /// <returns></returns>
  471. private static string DecryptLicenseString(IntPtr buffer, uint nBytes)
  472. {
  473. byte[] ba = new byte[nBytes];
  474. for (int i = 0; i < nBytes; i++)
  475. {
  476. ba[i] = Marshal.ReadByte(buffer, i);
  477. }
  478. MemoryStream ms = new MemoryStream(ba);
  479. RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
  480. rc2.Key = GetBytesFromHexString("7a6823a42a3a3ae27057c647db812d0");
  481. rc2.IV = GetBytesFromHexString("827d961224d99b2d");
  482. CryptoStream cs = new CryptoStream(ms, rc2.CreateDecryptor(), CryptoStreamMode.Read);
  483. string licenseString = string.Empty;
  484. byte[] ba1 = new byte[4096];
  485. int irtn = cs.Read(ba1, 0, 4096);
  486. Encoding enc = Encoding.Unicode;
  487. licenseString = enc.GetString(ba1, 0, irtn);
  488. cs.Close();
  489. cs.Dispose();
  490. ms.Close();
  491. ms.Dispose();
  492. rc2 = null;
  493. return licenseString;
  494. }
  495. /// <summary>
  496. /// Gets the byte array generated from the value of 'hexString'.
  497. /// </summary>
  498. /// <param name="hexString">Hexadecimal string</param>
  499. /// <returns>Array of bytes generated from 'hexString'.</returns>
  500. public static byte[] GetBytesFromHexString(string hexString)
  501. {
  502. int numHexChars = hexString.Length / 2;
  503. byte[] ba = new byte[numHexChars];
  504. int j = 0;
  505. for (int i = 0; i < ba.Length; i++)
  506. {
  507. string hex = new string(new char[] { hexString[j], hexString[j + 1] });
  508. ba[i] = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
  509. j = j + 2;
  510. }
  511. return ba;
  512. }
  513. #endregion
  514. }
  515. }