using GrxCAD.ApplicationServices; using GrxCAD.EditorInput; using GrxCAD.Internal; using GrxCAD.Runtime; using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Management; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; [assembly: ExtensionApplication(typeof(Thumbnail.MainClass))] namespace Thumbnail { public class MainClass : IExtensionApplication { public void Initialize() { } public void Terminate() { } public static string Flag = ""; //标记不同命令 public int num = 1; //0超过期限,-1空 public int slf_flag = 0; //加密 private static string Encrypt(string str, string key) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(str); des.Key = ASCIIEncoding.ASCII.GetBytes(key);// 密钥 des.IV = ASCIIEncoding.ASCII.GetBytes(key);// 初始化向量 MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); var retB = Convert.ToBase64String(ms.ToArray()); return retB; } //解密 private static string Decrypt(string pToDecrypt, string key) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(key); des.IV = ASCIIEncoding.ASCII.GetBytes(key); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); // 如果两次密钥不一样,这一步可能会引发异常 cs.FlushFinalBlock(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } //日期加密并放到注册表中 private void AddRecord() { string byte2string = null; ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); string strCpuID = null; foreach (ManagementObject mo in moc) { strCpuID = mo.Properties["ProcessorId"].Value.ToString(); break; } byte[] from = Encoding.Default.GetBytes(strCpuID); MD5CryptoServiceProvider makemd5 = new MD5CryptoServiceProvider(); byte[] target = makemd5.ComputeHash(from); for (int i = 0; i < target.Length; i++) { byte2string += target[i].ToString("x"); } Document acDoc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = acDoc.Editor; PromptStringOptions options = new PromptStringOptions("机器码:" + byte2string + "\n输入注册码:"); options.AllowSpaces = true; options.UseDefaultValue = true; options.DefaultValue = ""; string encrypted = Encrypt(byte2string, "vnf4s3ru"); string userInput = ed.GetString(options).StringResult; if (userInput == "") { num = -1; return; } string jm = Decrypt(userInput, "vnf4s3ru"); string zcm = jm.Split(',')[0]; string zcm_encrypted = Encrypt(zcm, "vnf4s3ru"); string work_days = jm.Split(',')[1]; if (zcm_encrypted == encrypted) { string dt = DateTime.Now.ToString().Trim(); dt = dt + ',' + work_days; string dt_ecpt = Encrypt(dt, "vnf4s3ru"); Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("CADplugins").CreateSubKey("Register.INI").CreateSubKey(dt_ecpt); MessageBox.Show("注册成功!"); } else { MessageBox.Show("注册失败!\n zcm_encrypted =" + zcm_encrypted.ToString() + "\nencrypted=" + encrypted.ToString() + "\nzcm=" + zcm.ToString() + "\njm=" + jm.ToString()); MessageBox.Show("注册失败!"); num = -1; } } public void Verify() { Microsoft.Win32.RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("CADplugins").CreateSubKey("Register.INI"); //判断注册表中相应位置的subkey数量,如果为零的话则需要注册 if (retkey.SubKeyCount == 0) { AddRecord(); } //如果不为零,判断日期是否在期限内 else { //转换为日期格式后检查当前日期之间和输入注册码的日期之间的间隔 string encrypted = retkey.GetSubKeyNames()[0]; string mw = Decrypt(encrypted, "vnf4s3ru"); string dt = mw.Split(',')[0]; int wkdays = Convert.ToInt32(mw.Split(',')[1]); TimeSpan ts = DateTime.Now - DateTime.Parse(dt); //如果超过期限,则删除所有subkey,显示注册码窗口 if (ts.Days > wkdays || ts.Days < 0) { MessageBox.Show("超出有效期,请重新注册!"); num = 0; foreach (string strRNum in retkey.GetSubKeyNames()) { retkey.DeleteSubKey(strRNum); } AddRecord(); } else { num = 1; } } } [CommandMethod("JTB")] public void thum() { Verify(); if (num == 1) { Form1 Frm = new Form1(); Frm.Show(); } } } }