C# 给自己写的软件,加注册码功能

来源:互联网 发布:苹果透视软件 编辑:程序博客网 时间:2024/05/17 03:33

 由于永和的项目里边用到了这个功能,因此在网上找到了,直接贴过来,复用一下。

       为自己写的程序加一个注册功能吧。生成的机器号是根据CPU和硬盘号来的,根据自己的需求改成是否是随机生成。

       代码直接粘贴到新建类覆盖原代码就能直接用了。

[csharp] view plaincopy
  1. using System;  
  2. using System.Management;  
  3. using System.Security.Cryptography;  
  4. using System.Text;  
  5.   
  6. namespace RegisterClass  
  7. {  
  8.     class RegisterClass  
  9.     {  
  10.         //步骤一: 获得CUP序列号和硬盘序列号的实现代码如下:  
  11.         //获得CPU的序列号  
  12.   
  13.         bool Stupids = true;  
  14.         bool Cat = false;  
  15.        public string getCpu()  
  16.         {  
  17.             string strCpu = null;  
  18.             ManagementClass myCpu = new ManagementClass("win32_Processor");  
  19.             ManagementObjectCollection myCpuConnection = myCpu.GetInstances();  
  20.             foreach( ManagementObject myObject in myCpuConnection)  
  21.             {  
  22.                 strCpu = myObject.Properties["Processorid"].Value.ToString();  
  23.                 break;  
  24.             }  
  25.             return strCpu;  
  26.         }  
  27.    
  28.         //取得设备硬盘的卷标号  
  29.   
  30.        public string GetDiskVolumeSerialNumber()  
  31.         {  
  32.             ManagementClass mc =   
  33.                  new ManagementClass("Win32_NetworkAdapterConfiguration");  
  34.             ManagementObject disk =   
  35.                  new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");  
  36.             disk.Get();  
  37.             return disk.GetPropertyValue("VolumeSerialNumber").ToString();  
  38.         }  
  39.    
  40.    
  41.         //步骤二: 收集硬件信息生成机器码, 代码如下:   
  42.         //生成机器码  
  43.   
  44.        public string CreateCode()  
  45.         {  
  46.             string temp = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号  
  47.             string[] strid = new string[24];//  
  48.             for (int i = 0; i < 24; i++)//把字符赋给数组  
  49.             {  
  50.                 strid[i] = temp.Substring(i, 1);  
  51.             }  
  52.             temp = "";  
  53.             //Random rdid = new Random();  
  54.             for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器三  
  55.             {  
  56.                 //temp += strid[rdid.Next(0, 24)];  
  57.                 temp += strid[i+3>=24?0:i+3];  
  58.             }  
  59.             return GetMd5(temp);  
  60.         }  
  61.   
  62.         //步骤三: 使用机器码生成软件注册码, 代码如下:  
  63.         //使用机器码生成注册码  
  64.         public int[] intCode = new int[127];//用于存密钥  
  65.   
  66.         public void setIntCode()//给数组赋值个小于10的随机数  
  67.         {  
  68.             //Random ra = new Random();  
  69.             //for (int i = 1; i < intCode.Length;i++ )  
  70.             //{  
  71.             //    intCode[i] = ra.Next(0, 9);  
  72.             //}  
  73.             for (int i = 1; i < intCode.Length; i++)  
  74.             {  
  75.                 intCode[i] = i + 3 > 9 ? 0 : i + 3;  
  76.             }  
  77.         }  
  78.         public int[] intNumber = new int[25];//用于存机器码的Ascii值  
  79.         public char[] Charcode = new char[25];//存储机器码字  
  80.       
  81.         //生成注册码  
  82.         public string GetCode(string code)  
  83.         {  
  84.             if (code != "")  
  85.             {  
  86.                 //把机器码存入数组中  
  87.                 setIntCode();//初始化127位数组  
  88.                 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中  
  89.                 {  
  90.                     Charcode[i] = Convert.ToChar(code.Substring(i - 1, 1));  
  91.                 }//  
  92.                 for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。  
  93.                   {  
  94.                     intNumber[j] =   
  95.                        intCode[Convert.ToInt32(Charcode[j])] +   
  96.                        Convert.ToInt32(Charcode[j]);  
  97.   
  98.                 }  
  99.                 string strAsciiName = null;//用于存储机器码  
  100.                 for (int j = 1; j < intNumber.Length; j++)  
  101.                 {  
  102.                     //MessageBox.Show((Convert.ToChar(intNumber[j])).ToString());  
  103.                     //判断字符ASCII值是否0-9之间  
  104.   
  105.                     if (intNumber[j] >= 48 && intNumber[j] <= 57)  
  106.                     {  
  107.                         strAsciiName += Convert.ToChar(intNumber[j]).ToString();  
  108.                     }  
  109.                     //判断字符ASCII值是否A-Z之间  
  110.   
  111.                     else if (intNumber[j] >= 65 && intNumber[j] <= 90)  
  112.                     {  
  113.                         strAsciiName += Convert.ToChar(intNumber[j]).ToString();  
  114.                     }  
  115.                     //判断字符ASCII值是否a-z之间  
  116.   
  117.   
  118.                     else if (intNumber[j] >= 97 && intNumber[j] <= 122)                      
  119.                   {  
  120.                         strAsciiName += Convert.ToChar(intNumber[j]).ToString();  
  121.                     }  
  122.                     else//判断字符ASCII值不在以上范围内  
  123.                     {  
  124.                         if (intNumber[j] > 122)//判断字符ASCII值是否大于z  
  125.                         {   
  126.                            strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();   
  127.                         }  
  128.                         else  
  129.                         {  
  130.                             strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();  
  131.                         }  
  132.   
  133.                     }  
  134.                     //label3.Text = strAsciiName;//得到注册码  
  135.                 }  
  136.                 return strAsciiName;  
  137.             }  
  138.             else  
  139.             {  
  140.                 return "";  
  141.             }  
  142.         }  
  143.    
  144.    
  145.         //步骤四: 用户输入注册码注册软件, 演示代码如下:  
  146.   
  147.         //注册  
  148.         public bool RegistIt(string currentCode,string realCode)  
  149.         {  
  150.             if (realCode != "")  
  151.             {  
  152.                 if (currentCode.TrimEnd().Equals(realCode.TrimEnd()))  
  153.                 {  
  154.                     Microsoft.Win32.RegistryKey retkey =   
  155.                          Microsoft.Win32.Registry.CurrentUser.  
  156.                          OpenSubKey("software"true).CreateSubKey("StupidsCat").  
  157.                          CreateSubKey("StupidsCat.ini").  
  158.                          CreateSubKey(currentCode.TrimEnd());  
  159.                     retkey.SetValue("StupidsCat""BBC6D58D0953F027760A046D58D52786");  
  160.   
  161.                     retkey = Microsoft.Win32.Registry.LocalMachine.  
  162.                         OpenSubKey("software"true).CreateSubKey("StupidsCat").  
  163.                          CreateSubKey("StupidsCat.ini").  
  164.                          CreateSubKey(currentCode.TrimEnd());  
  165.                     retkey.SetValue("StupidsCat""BBC6D58D0953F027760A046D58D52786");  
  166.   
  167.                     return Stupids;  
  168.                 }  
  169.                 else  
  170.                 {  
  171.                     return Cat;  
  172.                 }  
  173.             }  
  174.             else { return Cat; }  
  175.         }  
  176.   
  177.         public bool BoolRegist(string sn)  
  178.         {  
  179.             string[] keynames; bool flag = false;  
  180.             Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine;  
  181.             Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser;  
  182.             try  
  183.             {  
  184.                 keynames = localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();  
  185.                 foreach (string name in keynames)  
  186.                 {  
  187.                     if (name == "StupidsCat")  
  188.                     {  
  189.                         if (localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")  
  190.                             flag = true;   
  191.                     }  
  192.                 }  
  193.                 keynames = userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();  
  194.                 foreach (string name in keynames)  
  195.                 {  
  196.                     if (name == "StupidsCat")  
  197.                     {  
  198.                         if (flag && userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")  
  199.                             return true;  
  200.                     }  
  201.                 }  
  202.                 return false;  
  203.             }  
  204.             catch  
  205.             {  
  206.                 return false;  
  207.             }  
  208.             finally   
  209.             {   
  210.                 localRegKey.Close();   
  211.                 userRegKey.Close();   
  212.             }  
  213.         }  
  214.   
  215.         public string GetMd5(object text)  
  216.         {  
  217.             string path = text.ToString();  
  218.   
  219.             MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider();  
  220.             Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(text.ToString());  
  221.             Byte[] byteResult = MD5Pro.ComputeHash(buffer);  
  222.   
  223.             string md5result = BitConverter.ToString(byteResult).Replace("-""");  
  224.             return md5result;  
  225.         }  
  226.     }  
  227. }  
0 0
原创粉丝点击