C# 產生軟體註冊碼

來源:互聯網
上載者:User
C# 產生軟體註冊碼

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

由於數學不行所以寫的比較簡單,ok 上代碼吧 產生機器碼的原理很簡單,基本上都是取裝置資訊之後加密

        /// <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;        }
產生註冊碼
        /// <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());        } 
驗證註冊碼
        /// <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;        }
調用執行個體
           //取機器碼           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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.