工具箱相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainClass.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.EditorInput;
  3. using Autodesk.AutoCAD.Runtime;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Management;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. [assembly: ExtensionApplication(typeof(CutDGX.MainClass))]
  13. namespace CutDGX
  14. {
  15. public class MainClass : IExtensionApplication
  16. {
  17. public void Initialize()
  18. {
  19. Menu mn = new Menu();
  20. mn.AddMenu();
  21. }
  22. public void Terminate()
  23. {
  24. }
  25. public static string Flag = ""; //标记不同命令
  26. public int num = 1; //0超过期限,-1空
  27. public int slf_flag = 0;
  28. //加密
  29. private static string Encrypt(string str, string key)
  30. {
  31. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  32. byte[] inputByteArray = Encoding.Default.GetBytes(str);
  33. des.Key = ASCIIEncoding.ASCII.GetBytes(key);// 密钥
  34. des.IV = ASCIIEncoding.ASCII.GetBytes(key);// 初始化向量
  35. MemoryStream ms = new MemoryStream();
  36. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  37. cs.Write(inputByteArray, 0, inputByteArray.Length);
  38. cs.FlushFinalBlock();
  39. var retB = Convert.ToBase64String(ms.ToArray());
  40. return retB;
  41. }
  42. //解密
  43. private static string Decrypt(string pToDecrypt, string key)
  44. {
  45. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  46. byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
  47. des.Key = ASCIIEncoding.ASCII.GetBytes(key);
  48. des.IV = ASCIIEncoding.ASCII.GetBytes(key);
  49. MemoryStream ms = new MemoryStream();
  50. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  51. cs.Write(inputByteArray, 0, inputByteArray.Length);
  52. // 如果两次密钥不一样,这一步可能会引发异常
  53. cs.FlushFinalBlock();
  54. return System.Text.Encoding.Default.GetString(ms.ToArray());
  55. }
  56. //日期加密并放到注册表中
  57. private void AddRecord()
  58. {
  59. string byte2string = null;
  60. ManagementClass mc = new ManagementClass("Win32_Processor");
  61. ManagementObjectCollection moc = mc.GetInstances();
  62. string strCpuID = null;
  63. foreach (ManagementObject mo in moc)
  64. {
  65. strCpuID = mo.Properties["ProcessorId"].Value.ToString();
  66. break;
  67. }
  68. byte[] from = Encoding.Default.GetBytes(strCpuID);
  69. MD5CryptoServiceProvider makemd5 = new MD5CryptoServiceProvider();
  70. byte[] target = makemd5.ComputeHash(from);
  71. for (int i = 0; i < target.Length; i++)
  72. {
  73. byte2string += target[i].ToString("x");
  74. }
  75. Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  76. Editor ed = acDoc.Editor;
  77. PromptStringOptions options = new PromptStringOptions("机器码:" + byte2string + "\n输入注册码:");
  78. options.AllowSpaces = true;
  79. options.UseDefaultValue = true;
  80. options.DefaultValue = "";
  81. string encrypted = Encrypt(byte2string, "vnf4s3ru");
  82. string userInput = ed.GetString(options).StringResult;
  83. if (userInput == "")
  84. {
  85. num = -1;
  86. return;
  87. }
  88. string jm = Decrypt(userInput, "vnf4s3ru");
  89. string zcm = jm.Split(',')[0];
  90. string zcm_encrypted = Encrypt(zcm, "vnf4s3ru");
  91. string work_days = jm.Split(',')[1];
  92. if (zcm_encrypted == encrypted)
  93. {
  94. string dt = DateTime.Now.ToString().Trim();
  95. dt = dt + ',' + work_days;
  96. string dt_ecpt = Encrypt(dt, "vnf4s3ru");
  97. Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("CADplugins").CreateSubKey("Register.INI").CreateSubKey(dt_ecpt);
  98. System.Windows.Forms.MessageBox.Show("注册成功!");
  99. }
  100. else
  101. {
  102. System.Windows.Forms.MessageBox.Show("注册失败!\n zcm_encrypted =" + zcm_encrypted.ToString() + "\nencrypted=" + encrypted.ToString() + "\nzcm=" + zcm.ToString() + "\njm=" + jm.ToString());
  103. System.Windows.Forms.MessageBox.Show("注册失败!");
  104. num = -1;
  105. }
  106. }
  107. public void Verify()
  108. {
  109. RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("CADplugins").CreateSubKey("Register.INI");
  110. //判断注册表中相应位置的subkey数量,如果为零的话则需要注册
  111. if (retkey.SubKeyCount == 0)
  112. {
  113. AddRecord();
  114. }
  115. //如果不为零,判断日期是否在期限内
  116. else
  117. {
  118. //转换为日期格式后检查当前日期之间和输入注册码的日期之间的间隔
  119. string encrypted = retkey.GetSubKeyNames()[0];
  120. string mw = Decrypt(encrypted, "vnf4s3ru");
  121. string dt = mw.Split(',')[0];
  122. int wkdays = Convert.ToInt32(mw.Split(',')[1]);
  123. TimeSpan ts = DateTime.Now - DateTime.Parse(dt);
  124. //如果超过期限,则删除所有subkey,显示注册码窗口
  125. if (ts.Days > wkdays || ts.Days < 0)
  126. {
  127. System.Windows.Forms.MessageBox.Show("超出有效期,请重新注册!");
  128. num = 0;
  129. foreach (string strRNum in retkey.GetSubKeyNames())
  130. {
  131. retkey.DeleteSubKey(strRNum);
  132. }
  133. AddRecord();
  134. }
  135. else
  136. {
  137. num = 1;
  138. }
  139. }
  140. }
  141. //等高线裁剪
  142. [CommandMethod("DENSECUT")]
  143. public void DENSECUT()
  144. {
  145. Verify();
  146. if (num == 1)
  147. {
  148. Form1 Frm = new Form1();
  149. Frm.Show();
  150. }
  151. }
  152. //过河流道路等高线裁剪
  153. [CommandMethod("CROSSCUT")]
  154. public void CROSSCUT()
  155. {
  156. Verify();
  157. if (num == 1)
  158. {
  159. Dgxthrorvrd_Form Frm = new Dgxthrorvrd_Form();
  160. Frm.Show();
  161. }
  162. }
  163. //图幅接边
  164. [CommandMethod("Joinedge")]
  165. public void Joinedge()
  166. {
  167. Verify();
  168. if (num == 1)
  169. {
  170. joinedge Frm = new joinedge();
  171. Frm.Show();
  172. }
  173. }
  174. //按范围裁剪图框
  175. [CommandMethod("TKCut")]
  176. public void TKsCut()
  177. {
  178. Verify();
  179. if (num == 1)
  180. {
  181. TKCuts tc = new TKCuts();
  182. tc.Show();
  183. }
  184. }
  185. }
  186. }