1. 程式人生 > >C# 生成軟體註冊碼

C# 生成軟體註冊碼

C# 生成軟體註冊碼

今天早上,花了一個早上弄了個生成機器碼和註冊碼的Demo,通過生成的註冊碼裡面包含時間資訊,保證了註冊碼在使用後的指定時間後失效

由於數學不行所以寫的比較簡單,ok 上程式碼吧

  1. 產生機器碼的原理很簡單,基本上都是取裝置資訊之後加密
        /// <summary>
        /// 取本機機器碼
        /// </summary>
        public static string GetMachineCode()
        {
           //CPU資訊
           string
cpuId = DeviceHelper.GetCpuID(); //磁碟資訊 string diskId = DeviceHelper.GetDiskID(); //網絡卡資訊 string MacAddress = DeviceHelper.GetMacAddress(); string m1 = GetMD5(cpuId + typeof(string).ToString()); string m2 = GetMD5(diskId + typeof(int).ToString()); string
m3 = GetMD5(MacAddress+typeof(double).ToString()); string code1 = GetNum(m1, 8); string code2 = GetNum(m2, 8); string code3 = GetNum(m3, 8); return code1 + code2 + code3; }
  1. 產生註冊碼
        /// <summary>
        /// 根據機器碼產生註冊碼
        /// </summary>
/// <param name="machineCode">機器碼</param> /// <param name="overTime">到期時間</param> /// <returns></returns> public static string CreateRegisterCode(string machineCode,DateTime overTime) { int year = int.Parse(overTime.Year.ToString().Substring(2))+33; int month = overTime.Month+21; int day = overTime.Day+54; int section = machineCode.Length / 4; string reg = ""; int n = 1597; for (int i = 0; i < section; i++) { int sec = int.Parse(machineCode.Substring(i*4,4)); int resu = sec + n; if (resu >= 10000) { resu = sec - 1597; } reg += resu ; n = n + 1597; } //插入年月日資訊 reg = InsertNum(reg, year, 0, 8, 4, 6, 7, 1, 3, 2, 5, 9); reg = InsertNum(reg, month, 0, 6, 9, 7, 3, 8, 4, 1, 2, 5); reg = InsertNum(reg, day, 0,1, 2, 5, 6,7, 3, 8, 9, 4); return reg.ToString(); } /// <summary> /// 在指定數字後面插入內容 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="index"></param> /// <param name="pmc"></param> /// <returns></returns> static string InsertNum(string str,int num,int index,params int[] pmc) { int posi = str.IndexOf(pmc[index].ToString()); if (posi <= -1) return InsertNum(str, num, index + 1, pmc); return str.Insert(posi, num.ToString()); }
  1. 驗證註冊碼
        /// <summary>
        /// 檢查註冊碼
        /// </summary>
        /// <param name="registerCode"></param>
        /// <param name="overTime"></param>
        /// <returns></returns>
        public static bool CheckRegister( string registerCode,ref DateTime overTime)
        {
            try
            {
                string machineCode = GetMachineCode();
                //提取年月日
                int day = int.Parse(ExtractNum(ref registerCode, 0, 1, 2, 5, 6, 7, 3, 8, 9, 4));
                int month = int.Parse(ExtractNum(ref registerCode, 0, 6, 9, 7, 3, 8, 4, 1, 2, 5));
                int year = int.Parse(ExtractNum(ref registerCode, 0, 8, 4, 6, 7, 1, 3, 2, 5, 9));
                day -= 54;
                month -= 21;
                year -= 33;
                overTime = new DateTime(year, month, day);
                //核對註冊碼
                int section = machineCode.Length / 4;
                int n = 1597;
                string reg = "";
                for (int i = 0; i < section; i++)
                {
                    int sec = int.Parse(machineCode.Substring(i * 4, 4));
                    int resu = sec + n;
                    if (resu >= 10000)
                    {
                        resu = sec - 1597;
                    }
                    reg += resu;
                    n = n + 1597;
                }
                return registerCode == reg;
            }
            catch {
                return false;
            }
        }

        /// <summary>
        /// 提取數字
        /// </summary>
        /// <param name="str"></param>
        /// <param name="index"></param>
        /// <param name="pmc"></param>
        /// <returns></returns>
        static string ExtractNum(ref string str, int index, params int[] pmc)
        {
            int posi = str.IndexOf(pmc[index].ToString());
            if (posi <= -1)
                return ExtractNum(ref str, index + 1, pmc);
            string resu = str.Substring(posi - 2, 2);
            str = str.Remove(posi - 2, 2);
            return resu;
        }
  1. 呼叫例項
           //取機器碼
           string mCode = RegInfo.GetMachineCode();

           //產生註冊碼
           string regCode = RegInfo.CreateRegisterCode(mCode, DateTime.Now);
           DateTime time = DateTime.Now;
           //驗證註冊碼
           bool resu = RegInfo.CheckRegister(regCode+"1", ref time);

程式碼下載

連結:http://pan.baidu.com/s/1bpfFu3d 密碼:lth3