[C#] 纯文本查看 复制代码
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RegisterAccount
{
public class MainForm : Form
{
private TextBox tbLogin;
private TextBox tbPwd;
private TextBox tbConfirm;
private TextBox tbEmail; // 新增的邮箱文本框
private TextBox tbPhone; // 新增的电话号码文本框
private Button btnRegister;
private Label lblLogin;
private Label lblPwd;
private Label lblConfirm;
private Label lblEmail;
private Label lblPhone;
private static readonly Regex PwdRegex = new Regex("^[\\x20-\\xFF]{4,16}$");
public MainForm()
{
InitializeComponent();
}
private void btnRegister_Click(object sender, EventArgs e)
{
if (tbConfirm.Text != tbPwd.Text)
{
MessageBox.Show("Password does not match confirmation!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
byte[] value = null;
try
{
value = GetAccountPasswordHash(tbConfirm.Text);
}
catch (ArgumentException ex)
{
MessageBox.Show("Password must be 4 to 16 Latin letters or numbers!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
using (SqlConnection sqlConnection = new SqlConnection("Server=127.0.0.1;Database=AionAccounts;User Id=sa;Password=123456;Connection Timeout=200"))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = "agent_CreateAccount";
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@ggid", Guid.NewGuid());
sqlCommand.Parameters.AddWithValue("@account", tbLogin.Text);
sqlCommand.Parameters.AddWithValue("@password", value);
sqlCommand.Parameters.AddWithValue("@email", tbEmail.Text); // 使用用户输入的邮箱
sqlCommand.Parameters.AddWithValue("@mobile", tbPhone.Text); // 使用用户输入的电话号码
sqlCommand.Parameters.AddWithValue("@question1", string.Empty);
sqlCommand.Parameters.AddWithValue("@question2", string.Empty);
sqlCommand.Parameters.AddWithValue("@answer1", new byte[1]);
sqlCommand.Parameters.AddWithValue("@answer2", new byte[1]);
SqlParameter sqlParameter = sqlCommand.Parameters.Add("@ReturnVal", SqlDbType.Int);
sqlParameter.Direction = ParameterDirection.ReturnValue;
sqlCommand.ExecuteNonQuery();
int num = (int)sqlParameter.Value;
if (num == 0)
{
MessageBox.Show("Account not created! Invalid login or login already exists!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show(string.Format("Account created successfully! Account ID = {0}!", num), "Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
}
}
private void InitializeComponent()
{
this.tbLogin = new System.Windows.Forms.TextBox();
this.tbPwd = new System.Windows.Forms.TextBox();
this.tbConfirm = new System.Windows.Forms.TextBox();
this.tbEmail = new System.Windows.Forms.TextBox();
this.tbPhone = new System.Windows.Forms.TextBox();
this.btnRegister = new System.Windows.Forms.Button();
this.lblLogin = new System.Windows.Forms.Label();
this.lblPwd = new System.Windows.Forms.Label();
this.lblConfirm = new System.Windows.Forms.Label();
this.lblEmail = new System.Windows.Forms.Label();
this.lblPhone = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// tbLogin
//
this.tbLogin.Location = new System.Drawing.Point(100, 20);
this.tbLogin.Name = "tbLogin";
this.tbLogin.Size = new System.Drawing.Size(150, 21);
this.tbLogin.TabIndex = 1;
//
// tbPwd
//
this.tbPwd.Location = new System.Drawing.Point(100, 50);
this.tbPwd.Name = "tbPwd";
this.tbPwd.PasswordChar = '*';
this.tbPwd.Size = new System.Drawing.Size(150, 21);
this.tbPwd.TabIndex = 3;
//
// tbConfirm
//
this.tbConfirm.Location = new System.Drawing.Point(140, 80);
this.tbConfirm.Name = "tbConfirm";
this.tbConfirm.PasswordChar = '*';
this.tbConfirm.Size = new System.Drawing.Size(150, 21);
this.tbConfirm.TabIndex = 5;
//
// tbEmail
//
this.tbEmail.Location = new System.Drawing.Point(100, 110);
this.tbEmail.Name = "tbEmail";
this.tbEmail.Size = new System.Drawing.Size(150, 21);
this.tbEmail.TabIndex = 7;
//
// tbPhone
//
this.tbPhone.Location = new System.Drawing.Point(100, 140);
this.tbPhone.Name = "tbPhone";
this.tbPhone.Size = new System.Drawing.Size(150, 21);
this.tbPhone.TabIndex = 9;
//
// btnRegister
//
this.btnRegister.Location = new System.Drawing.Point(100, 180);
this.btnRegister.Name = "btnRegister";
this.btnRegister.Size = new System.Drawing.Size(100, 30);
this.btnRegister.TabIndex = 10;
this.btnRegister.Text = "Register";
this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
//
// lblLogin
//
this.lblLogin.AutoSize = true;
this.lblLogin.Location = new System.Drawing.Point(20, 20);
this.lblLogin.Name = "lblLogin";
this.lblLogin.Size = new System.Drawing.Size(41, 12);
this.lblLogin.TabIndex = 0;
this.lblLogin.Text = "Login:";
//
// lblPwd
//
this.lblPwd.AutoSize = true;
this.lblPwd.Location = new System.Drawing.Point(20, 50);
this.lblPwd.Name = "lblPwd";
this.lblPwd.Size = new System.Drawing.Size(59, 12);
this.lblPwd.TabIndex = 2;
this.lblPwd.Text = "Password:";
//
// lblConfirm
//
this.lblConfirm.AutoSize = true;
this.lblConfirm.Location = new System.Drawing.Point(20, 80);
this.lblConfirm.Name = "lblConfirm";
this.lblConfirm.Size = new System.Drawing.Size(107, 12);
this.lblConfirm.TabIndex = 4;
this.lblConfirm.Text = "Confirm Password:";
//
// lblEmail
//
this.lblEmail.AutoSize = true;
this.lblEmail.Location = new System.Drawing.Point(20, 110);
this.lblEmail.Name = "lblEmail";
this.lblEmail.Size = new System.Drawing.Size(41, 12);
this.lblEmail.TabIndex = 6;
this.lblEmail.Text = "Email:";
//
// lblPhone
//
this.lblPhone.AutoSize = true;
this.lblPhone.Location = new System.Drawing.Point(20, 140);
this.lblPhone.Name = "lblPhone";
this.lblPhone.Size = new System.Drawing.Size(41, 12);
this.lblPhone.TabIndex = 8;
this.lblPhone.Text = "Phone:";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(350, 288);
this.Controls.Add(this.lblLogin);
this.Controls.Add(this.tbLogin);
this.Controls.Add(this.lblPwd);
this.Controls.Add(this.tbPwd);
this.Controls.Add(this.lblConfirm);
this.Controls.Add(this.tbConfirm);
this.Controls.Add(this.lblEmail);
this.Controls.Add(this.tbEmail);
this.Controls.Add(this.lblPhone);
this.Controls.Add(this.tbPhone);
this.Controls.Add(this.btnRegister);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Account Registration";
this.ResumeLayout(false);
this.PerformLayout();
}
private static byte[] GetAccountPasswordHash(string input)
{
if (!PwdRegex.IsMatch(input))
{
throw new ArgumentException("Input string does not meet the requirements (4 to 16 ASCII symbols)", "input");
}
byte[] array = new byte[17];
byte[] array2 = new byte[17];
byte[] bytes = Encoding.ASCII.GetBytes(input);
for (int i = 0; i < input.Length; i++)
{
array[i + 1] = bytes[i];
array2[i + 1] = array[i + 1];
}
long num = (long)((ulong)array[1] + (ulong)array[2] * 256UL + (ulong)array[3] * 65536UL + (ulong)array[4] * 16777216UL);
long num2 = num * 213119L + 2529077L;
num2 -= num2 / 4294967296L * 4294967296L;
// Perform similar calculations for num3, num4, and num5
array[4] = (byte)(num2 / 16777216L);
array[3] = (byte)((num2 - (long)((int)array[4] * 16777216)) / 65536L);
array[2] = (byte)((num2 - (long)((int)array[4] * 16777216) - (long)((int)array[3] * 65536)) / 256L);
array[1] = (byte)(num2 - (long)((int)array[4] * 16777216) - (long)((int)array[3] * 65536) - (long)((int)array[2] * 256));
// Perform similar assignments for array[5] to array[16]
array2[1] = (byte)(array2[1] ^ array[1]);
for (int j = 1; j < 16; j++)
{
array2[j] = (byte)(array2[j] ^ array2[j - 1] ^ array[j]);
}
for (int j = 0; j < 16; j++)
{
if (array2[j] == 0)
{
array2[j] = 102;
}
}
byte[] array3 = new byte[16];
Buffer.BlockCopy(array2, 1, array3, 0, 16);
return array3;
}
}
public static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}