PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/FaceCapturer/RemoteImagingMC/Util.cs

http://facecomparerdis.codeplex.com
C# | 196 lines | 155 code | 35 blank | 6 comment | 18 complexity | 0daff00743a5d2ba7643a0fc15c31c8d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Management;
  8. using System.Security.Cryptography;
  9. using Encryptor;
  10. using Microsoft.Win32;
  11. namespace RemoteImaging
  12. {
  13. public static class Util
  14. {
  15. private static string ProductRegistryPath = @"Software\Yufei\RemoteImaging";
  16. private static string IDRegistryName = "UUID";
  17. private static string KeyRegistryName = "Key";
  18. private static string GetKeyFile()
  19. {
  20. string keyFile = Path.Combine(Application.StartupPath, "key");
  21. return keyFile;
  22. }
  23. public static void WriteKey(string ID, string key)
  24. {
  25. string file = GetKeyFile();
  26. FileStream fs = File.OpenWrite(file);
  27. StreamWriter sw = new StreamWriter(fs);
  28. sw.Write(key);
  29. sw.Close();
  30. }
  31. public static bool VerifyKey()
  32. {
  33. string uuid = "";
  34. string key = "";
  35. ReadAuthentication(out uuid, out key);
  36. if (string.IsNullOrEmpty(uuid) || string.IsNullOrEmpty(key)) return false;
  37. string encodedSN = EncryptService.Encode(uuid);
  38. string decoded = EncryptService.Decode(key);
  39. return string.Compare(encodedSN, key, StringComparison.Ordinal) == 0;
  40. }
  41. //Return a hardware identifier
  42. private static string identifier(string wmiClass, string wmiProperty)
  43. {
  44. string result = "";
  45. System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
  46. System.Management.ManagementObjectCollection moc = mc.GetInstances();
  47. foreach (System.Management.ManagementObject mo in moc)
  48. {
  49. //Only get the first one
  50. if (result == "")
  51. {
  52. try
  53. {
  54. result = mo[wmiProperty].ToString();
  55. break;
  56. }
  57. catch
  58. {
  59. }
  60. }
  61. }
  62. return result;
  63. }
  64. private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
  65. {
  66. string result = "";
  67. System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
  68. System.Management.ManagementObjectCollection moc = mc.GetInstances();
  69. foreach (System.Management.ManagementObject mo in moc)
  70. {
  71. if (mo[wmiMustBeTrue].ToString() == "True")
  72. {
  73. //Only get the first one
  74. if (result == "")
  75. {
  76. try
  77. {
  78. result = mo[wmiProperty].ToString();
  79. break;
  80. }
  81. catch
  82. {
  83. }
  84. }
  85. }
  86. }
  87. return result;
  88. }
  89. private static string cpuId()
  90. {
  91. //Uses first CPU identifier available in order of preference
  92. //Don't get all identifiers, as it is very time consuming
  93. string retVal = identifier("Win32_Processor", "UniqueId");
  94. if (retVal == "") //If no UniqueID, use ProcessorID
  95. {
  96. retVal = identifier("Win32_Processor", "ProcessorId");
  97. if (retVal == "") //If no ProcessorId, use Name
  98. {
  99. retVal = identifier("Win32_Processor", "Name");
  100. if (retVal == "") //If no Name, use Manufacturer
  101. {
  102. retVal = identifier("Win32_Processor", "Manufacturer");
  103. }
  104. //Add clock speed for extra security
  105. retVal += identifier("Win32_Processor", "MaxClockSpeed");
  106. }
  107. }
  108. return retVal;
  109. }
  110. private static string MAC()
  111. {
  112. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  113. ManagementObjectCollection moc = mc.GetInstances();
  114. string MACAddress = String.Empty;
  115. foreach (ManagementObject mo in moc)
  116. {
  117. if (MACAddress == String.Empty) // only return MAC Address from first card
  118. {
  119. bool enabled = (bool)mo["IPEnabled"];
  120. MACAddress = mo["MacAddress"].ToString();
  121. }
  122. mo.Dispose();
  123. }
  124. MACAddress = MACAddress.Replace(":", "");
  125. return EncryptService.Encode(MACAddress);
  126. }
  127. public static void WriteAuthentication(string UUID, string key)
  128. {
  129. RegistryKey productKey = Registry.LocalMachine.CreateSubKey(ProductRegistryPath);
  130. productKey.SetValue(IDRegistryName, UUID);
  131. productKey.SetValue(KeyRegistryName, key);
  132. }
  133. public static void ReadAuthentication(out string UUID, out string key)
  134. {
  135. try
  136. {
  137. RegistryKey productKey = Registry.LocalMachine.OpenSubKey(ProductRegistryPath);
  138. UUID = (string)productKey.GetValue(IDRegistryName, "");
  139. key = (string)productKey.GetValue(KeyRegistryName, "");
  140. }
  141. catch
  142. {
  143. UUID = null;
  144. key = null;
  145. }
  146. }
  147. public static string GetOrGenerateUniqID()
  148. {
  149. string uuid;
  150. string key;
  151. ReadAuthentication(out uuid, out key);
  152. if (string.IsNullOrEmpty(uuid))
  153. uuid = System.Guid.NewGuid().ToString();
  154. return uuid.ToUpper();
  155. }
  156. public static void ShowErrorMessage(string msg)
  157. {
  158. MessageBox.Show(msg, RemoteImaging.Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
  159. }
  160. }
  161. }