工具箱相关
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MainClass.cs 6.2KB

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