失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > C#—软件注册与注册机

C#—软件注册与注册机

时间:2019-04-13 23:26:09

相关推荐

C#—软件注册与注册机



(一)软件的实现:

SoftReg类:

1: using System;

2: using System.Collections.Generic;

3: using System.Linq;

4: using System.Text;

5: using System.Management; //需要引用System.Management.dll

6:

7: namespace SoftRegister

8: {

9:class SoftReg

10:{

11: ///<summary>

12: /// 获取硬盘卷标号

13: ///</summary>

14: ///<returns></returns>

15: public string GetDiskVolumeSerialNumber()

16: {

17: ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");

18: ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");

19: disk.Get();

20: return disk.GetPropertyValue("VolumeSerialNumber").ToString();

21: }

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 myCpuCollection = myCpu.GetInstances();

32: foreach (ManagementObject myObject in myCpuCollection)

33: {

34: strCpu = myObject.Properties["Processorid"].Value.ToString();

35: }

36: return strCpu;

37: }

38:

39: ///<summary>

40: /// 生成机器码

41: ///</summary>

42: ///<returns></returns>

43: public string GetMNum()

44: {

45: string strNum = GetCpu() + GetDiskVolumeSerialNumber();

46: string strMNum = strNum.Substring(0, 24); //截取前24位作为机器码

47: return strMNum;

48: }

49:

50: public int[] intCode = new int[127]; //存储密钥

51: public char[] charCode = new char[25]; //存储ASCII码

52: public int[] intNumber = new int[25]; //存储ASCII码值

53:

54: //初始化密钥

55: public void SetIntCode()

56: {

57: for (int i = 1; i < intCode.Length; i++)

58: {

59: intCode[i] = i % 9;

60: }

61: }

62:

63: ///<summary>

64: /// 生成注册码

65: ///</summary>

66: ///<returns></returns>

67: public string GetRNum()

68: {

69: SetIntCode();

70: string strMNum = GetMNum();

71: for (int i = 1; i < charCode.Length; i++) //存储机器码

72: {

73: charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));

74: }

75: for (int j = 1; j < intNumber.Length; j++) //改变ASCII码值

76: {

77: intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];

78: }

79: string strAsciiName = ""; //注册码

80: for (int k = 1; k < intNumber.Length; k++) //生成注册码

81: {

82:

83: if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]

84: <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判断如果在0-9、A-Z、a-z之间

85: {

86: strAsciiName += Convert.ToChar(intNumber[k]).ToString();

87: }

88: else if (intNumber[k] > 122) //判断如果大于z

89: {

90: strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();

91: }

92: else

93: {

94: strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString();

95: }

96: }

97: return strAsciiName;

98: }

99:}

100: }

主窗体:

1: using System;

2: using System.Collections.Generic;

3: using ponentModel;

4: using System.Data;

5: using System.Drawing;

6: using System.Linq;

7: using System.Text;

8: using System.Windows.Forms;

9: using Microsoft.Win32;

10:

11: namespace SoftRegister

12: {

13:public partial class FormMain : Form

14:{

15: public FormMain()

16: {

17: InitializeComponent();

18: }

19:

20: SoftReg softReg = new SoftReg();

21:

22: private void FormMain_Load(object sender, EventArgs e)

23: {

24: //判断软件是否注册

25: RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");

26: foreach (string strRNum in retkey.GetSubKeyNames())

27: {

28: if (strRNum == softReg.GetRNum())

29: {

30: this.labRegInfo.Text = "此软件已注册!";

31: this.btnReg.Enabled = false;

32: return;

33: }

34: }

35: this.labRegInfo.Text = "此软件尚未注册!";

36: this.btnReg.Enabled = true;

37: MessageBox.Show("您现在使用的是试用版,可以免费试用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

38:

39: Int32 tLong; //已使用次数

40: try

41: {

42: tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);

43: MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

44: }

45: catch

46: {

47: MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

48: Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord);

49: }

50:

51: //判断是否可以继续试用

52: tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);

53: if (tLong < 30)

54: {

55: int tTimes = tLong + 1;

56: Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", tTimes);

57: }

58: else

59: {

60: DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

61: if (result == DialogResult.Yes)

62: {

63: FormRegister.state = false; //设置软件状态为不可用

64: btnReg_Click(sender, e); //打开注册窗口

65: }

66: else

67: {

68: Application.Exit();

69: }

70: }

71:

72: }

73:

74: private void btnClose_Click(object sender, EventArgs e)

75: {

76: Application.Exit();

77: }

78:

79: private void btnReg_Click(object sender, EventArgs e)

80: {

81: FormRegister frmRegister = new FormRegister();

82: frmRegister.ShowDialog();

83: }

84:}

85: }

注册窗体:

1: using System;

2: using System.Collections.Generic;

3: using ponentModel;

4: using System.Data;

5: using System.Drawing;

6: using System.Linq;

7: using System.Text;

8: using System.Windows.Forms;

9: using Microsoft.Win32;

10:

11: namespace SoftRegister

12: {

13:public partial class FormRegister : Form

14:{

15: public FormRegister()

16: {

17: InitializeComponent();

18: }

19:

20: public static bool state = true; //软件是否为可用状态

21: SoftReg softReg = new SoftReg();

22:

23: private void btnReg_Click(object sender, EventArgs e)

24: {

25: try

26: {

27: if (txtLicence.Text == softReg.GetRNum())

28: {

29: MessageBox.Show("注册成功!重启软件后生效!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

30: RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txtLicence.Text);

31: retkey.SetValue("UserName", "Rsoft");

32: this.Close();

33: }

34: else

35: {

36: MessageBox.Show("注册码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

37: txtLicence.SelectAll();

38: }

39: }

40: catch (Exception ex)

41: {

42: throw new Exception(ex.Message);

43: }

44: }

45:

46: private void btnClose_Click(object sender, EventArgs e)

47: {

48: if (state == true)

49: {

50: this.Close();

51: }

52: else

53: {

54: Application.Exit();

55: }

56: }

57:

58: private void FormRegister_Load(object sender, EventArgs e)

59: {

60: this.txtHardware.Text = softReg.GetMNum();

61: }

62:}

63: }

(二)注册机的实现: SoftReg类:

1: using System;

2: using System.Collections.Generic;

3: using System.Linq;

4: using System.Text;

5: using System.Management;

6:

7: namespace SoftwarePassport

8: {

9:class SoftReg

10:{

11: public int[] intCode = new int[127]; //存储密钥

12: public char[] charCode = new char[25]; //存储ASCII码

13: public int[] intNumber = new int[25]; //存储ASCII码值

14:

15: //初始化密钥

16: public void SetIntCode()

17: {

18: for (int i = 1; i < intCode.Length; i++)

19: {

20: intCode[i] = i % 9;

21: }

22: }

23:

24: ///<summary>

25: /// 生成注册码

26: ///</summary>

27: ///<returns></returns>

28: public string GetRNum(string strMNum)

29: {

30: SetIntCode();

31:

32: for (int i = 1; i < charCode.Length; i++) //存储机器码

33: {

34: charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));

35: }

36: for (int j = 1; j < intNumber.Length; j++) //改变ASCII码值

37: {

38: intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];

39: }

40: string strAsciiName = ""; //注册码

41: for (int k = 1; k < intNumber.Length; k++) //生成注册码

42: {

43:

44: if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]

45: <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判断如果在0-9、A-Z、a-z之间

46: {

47: strAsciiName += Convert.ToChar(intNumber[k]).ToString();

48: }

49: else if (intNumber[k] > 122) //判断如果大于z

50: {

51: strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();

52: }

53: else

54: {

55: strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString();

56: }

57: }

58: return strAsciiName;

59: }

60:}

61: }

主窗体:

1: using System;

2: using System.Collections.Generic;

3: using ponentModel;

4: using System.Data;

5: using System.Drawing;

6: using System.Linq;

7: using System.Text;

8: using System.Windows.Forms;

9:

10: namespace SoftwarePassport

11: {

12:public partial class FormMain : Form

13:{

14: public FormMain()

15: {

16: InitializeComponent();

17: }

18:

19: SoftReg softReg = new SoftReg();

20:

21: private void btnCreate_Click(object sender, EventArgs e)

22: {

23: try

24: {

25: string strHardware = this.txtHardware.Text;

26: string strLicence = softReg.GetRNum(strHardware);

27: this.txtLicence.Text = strLicence;

28: }

29: catch (System.Exception ex)

30: {

31: MessageBox.Show("输入的机器码格式错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

32: }

33: }

34:

35: private void btnExit_Click(object sender, EventArgs e)

36: {

37: Application.Exit();

38: }

39:}

40: }

转自:/hanzhaoxin/archive//01/04/2844191.html

如果觉得《C#—软件注册与注册机》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。