123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Management;//需要在项目中添加System.Management引用
- using System.Security.Cryptography;
- namespace felixyin
- {
- public class SoftReg
- {
- /// <summary>
- /// 取得设备硬盘的卷标号
- /// </summary>
- /// <returns></returns>
- public string GetDiskVolumeSerialNumber()
- {
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
- disk.Get();
- return disk.GetPropertyValue("VolumeSerialNumber").ToString();
- }
- /// <summary>
- /// 获得CPU的序列号
- /// </summary>
- /// <returns></returns>
- public string getCpu()
- {
- string strCpu = null;
- ManagementClass myCpu = new ManagementClass("win32_Processor");
- ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
- foreach (ManagementObject myObject in myCpuConnection)
- {
- strCpu = myObject.Properties["Processorid"].Value.ToString();
- break;
- }
- return strCpu;
- }
- /// <summary>
- /// 生成机器码
- /// </summary>
- /// <returns></returns>
- public string getMNum()
- {
- string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
- string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码
- return strMNum;
- }
- public int[] intCode = new int[127];//存储密钥
- public int[] intNumber = new int[25];//存机器码的Ascii值
- public char[] Charcode = new char[25];//存储机器码字
- public void setIntCode()//给数组赋值小于10的数
- {
- for (int i = 1; i < intCode.Length; i++)
- {
- intCode[i] = i % 9;
- }
- }
- /// <summary>
- /// 生成注册码
- /// </summary>
- /// <returns></returns>
- public string getRNum()
- {
- setIntCode();//初始化127位数组
- string MNum = this.getMNum();//获取注册码
- for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
- {
- Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1));
- }
- for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
- {
- intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
- }
- string strAsciiName = "";//用于存储注册码
- for (int j = 1; j < intNumber.Length; j++)
- {
- if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
- {
- strAsciiName += Convert.ToChar(intNumber[j]).ToString();
- }
- else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
- {
- strAsciiName += Convert.ToChar(intNumber[j]).ToString();
- }
- else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
- {
- strAsciiName += Convert.ToChar(intNumber[j]).ToString();
- }
- else//判断字符ASCII值不在以上范围内
- {
- if (intNumber[j] > 122)//判断字符ASCII值是否大于z
- {
- strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
- }
- else
- {
- strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
- }
- }
- }
- return strAsciiName;//返回注册码
- }
- /// <summary>
- /// 用MD5加密字符串
- /// </summary>
- /// <param name="password">待加密的字符串</param>
- /// <returns></returns>
- public string MD5Encrypt(string password)
- {
- MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
- byte[] hashedDataBytes;
- hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
- StringBuilder tmp = new StringBuilder();
- foreach (byte i in hashedDataBytes)
- {
- tmp.Append(i.ToString("x2"));
- }
- return tmp.ToString();
- }
- }
- }
|