SoftReg.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Management;//需要在项目中添加System.Management引用
  7. using System.Security.Cryptography;
  8. namespace felixyin
  9. {
  10. public class SoftReg
  11. {
  12. /// <summary>
  13. /// 取得设备硬盘的卷标号
  14. /// </summary>
  15. /// <returns></returns>
  16. public string GetDiskVolumeSerialNumber()
  17. {
  18. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  19. ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  20. disk.Get();
  21. return disk.GetPropertyValue("VolumeSerialNumber").ToString();
  22. }
  23. /// <summary>
  24. /// 获得CPU的序列号
  25. /// </summary>
  26. /// <returns></returns>
  27. public string getCpu()
  28. {
  29. string strCpu = null;
  30. ManagementClass myCpu = new ManagementClass("win32_Processor");
  31. ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
  32. foreach (ManagementObject myObject in myCpuConnection)
  33. {
  34. strCpu = myObject.Properties["Processorid"].Value.ToString();
  35. break;
  36. }
  37. return strCpu;
  38. }
  39. /// <summary>
  40. /// 生成机器码
  41. /// </summary>
  42. /// <returns></returns>
  43. public string getMNum()
  44. {
  45. string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
  46. string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码
  47. return strMNum;
  48. }
  49. public int[] intCode = new int[127];//存储密钥
  50. public int[] intNumber = new int[25];//存机器码的Ascii值
  51. public char[] Charcode = new char[25];//存储机器码字
  52. public void setIntCode()//给数组赋值小于10的数
  53. {
  54. for (int i = 1; i < intCode.Length; i++)
  55. {
  56. intCode[i] = i % 9;
  57. }
  58. }
  59. /// <summary>
  60. /// 生成注册码
  61. /// </summary>
  62. /// <returns></returns>
  63. public string getRNum()
  64. {
  65. setIntCode();//初始化127位数组
  66. string MNum = this.getMNum();//获取注册码
  67. for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
  68. {
  69. Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1));
  70. }
  71. for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
  72. {
  73. intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
  74. }
  75. string strAsciiName = "";//用于存储注册码
  76. for (int j = 1; j < intNumber.Length; j++)
  77. {
  78. if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
  79. {
  80. strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  81. }
  82. else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
  83. {
  84. strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  85. }
  86. else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
  87. {
  88. strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  89. }
  90. else//判断字符ASCII值不在以上范围内
  91. {
  92. if (intNumber[j] > 122)//判断字符ASCII值是否大于z
  93. {
  94. strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
  95. }
  96. else
  97. {
  98. strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
  99. }
  100. }
  101. }
  102. return strAsciiName;//返回注册码
  103. }
  104. /// <summary>
  105. /// 用MD5加密字符串
  106. /// </summary>
  107. /// <param name="password">待加密的字符串</param>
  108. /// <returns></returns>
  109. public string MD5Encrypt(string password)
  110. {
  111. MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
  112. byte[] hashedDataBytes;
  113. hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
  114. StringBuilder tmp = new StringBuilder();
  115. foreach (byte i in hashedDataBytes)
  116. {
  117. tmp.Append(i.ToString("x2"));
  118. }
  119. return tmp.ToString();
  120. }
  121. }
  122. }