C#写了一个简单的注册机制 每次生成的注册码都一样 求解
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Random rd = new Random();
int num1 = rd.Next(100000, 999999);
int num2 = rd.Next(100000, 999999);
textBox1.Text = "" + num1 + getCpu() + num2;
string apppath = Environment.CurrentDirectory;
string path = apppath + @"\key.txt";
string str;
StreamReader sr = new StreamReader(path, false);
try
{
str = sr.ReadLine().ToString();
sr.Close();
this.textBox2.Text = str;///读取
}
catch
{
sr.Close();
}
// textBox2.Text = line;
}
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;
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = textBox1.Text.Substring(6, 16);
var md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str1)), 4, 8);
t2 = t2.Replace("-", "");
if (textBox2.Text == t2)
{
StreamWriter sw = new StreamWriter("key.txt", false);
sw.Write(textBox2.Text);
sw.Flush();
sw.Close();
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
else
{
if (textBox2.Text != "")
{
MessageBox.Show("注册码错误!");
}
}