本帖最后由 rsndm 于 2023-10-23 17:33 编辑
[C#] 纯文本查看 复制代码
csharp
using System;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
namespace RegisterAccount
{
public partial class MainWindow : Window
{
private static readonly Regex PwdRegex = new Regex("^[\\x20-\\xFF]{4,16}$");
public MainWindow()
{
InitializeComponent();
}
// 注册按钮点击事件
private void BtnRegister_Click(object sender, RoutedEventArgs e)
{
// 校验两次输入的密码是否一致
if (PbPwd.Password != PbConfirm.Password)
{
MessageBox.Show("请确保两次输入的密码一致!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 校验用户名和密码格式是否正确
if (!IsValidInput(TbUsername.Text, PbPwd.Password))
{
MessageBox.Show("用户名或密码格式不正确,必须由 4 到 16 个 ASCII 字符组成!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 对密码进行加密
byte[] hashedPwd = GetAccountPasswordHash(PbPwd.Password);
// 向数据库插入新用户
int insertedUserId;
using (SqlConnection connection = new SqlConnection("Server=.;Database=myDatabase;Integrated Security=True;"))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO Users (Username, Password) OUTPUT INSERTED.Id VALUES (@Username, @Password)";
command.Parameters.AddWithValue("@Username", TbUsername.Text);
command.Parameters.AddWithValue("@Password", hashedPwd);
insertedUserId = (int)command.ExecuteScalar();
}
}
MessageBox.Show($"用户 {TbUsername.Text} 注册成功,ID 为 {insertedUserId}!", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
// 找回密码按钮点击事件
private void BtnRetrievePwd_Click(object sender, RoutedEventArgs e)
{
// 校验用户名格式是否正确
if (!IsValidInput(TbUsername.Text))
{
MessageBox.Show("用户名格式不正确,必须由 4 到 16 个 ASCII 字符组成!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 从数据库中查找用户
byte[] hashedPwd;
using (SqlConnection connection = new SqlConnection("Server=.;Database=myDatabase;Integrated Security=True;"))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT Password FROM Users WHERE Username = @Username";
command.Parameters.AddWithValue("@Username", TbUsername.Text);
hashedPwd = (byte[])command.ExecuteScalar();
}
}
if (hashedPwd == null)
{
MessageBox.Show($"用户 {TbUsername.Text} 不存在!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
string decodedPwd = Encoding.ASCII.GetString(hashedPwd, 0, hashedPwd.Length);
MessageBox.Show($"用户 {TbUsername.Text} 的密码为 {decodedPwd}。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
// 校验输入的用户和密码格式是否正确
private bool IsValidInput(string username, string password = null)
{
return !string.IsNullOrWhiteSpace(username)
&& PwdRegex.IsMatch(password ?? "");
}
// 对密码进行加密
private byte[] GetAccountPasswordHash(string 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;
num = (long)((ulong)array[5] + (ulong)array[6] * 256UL + (ulong)array[7] * 65536UL + (ulong)array[8] * 16777216UL);
long num3 = num * 213247L + 2529089L;
num3 -= num3 / 4294967296L * 4294967296L;
num = (long)((ulong)array[9] + (ulong)array[10] * 256UL + (ulong)array[11] * 65536UL + (ulong)array[12] * 16777216UL);
long num4 = num * 213203L + 2529589L;
num4 -= num4 / 4294967296L * 4294967296L;
num = (long)((ulong)array[13] + (ulong)array[14] * 256UL + (ulong)array[15] * 65536UL + (ulong)array[16] * 16777216UL);
long num5 = num * 213821L + 2529997L;
num5 -= num5 / 4294967296L * 4294967296L;
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));
array[8] = (byte)(num3 / 16777216L);
array[7] = (byte)((num3 - (long)((ulong)array[8] * 16777216UL)) / 65536L);
array[6] = (byte)((num3 - (long)((ulong)array[8] * 16777216UL) - (long)((int)array[7] * 65536)) / 256L);
array[5] = (byte)(num3 - (long)((ulong)array[8] * 16777216UL) - (long)((int)array[7] * 65536) - (long)((int)array[6] * 256));
array[12] = (byte)(num4 / 16777216L);
array[11] = (byte)((num4 - (long)((ulong)array[12] * 16777216UL)) / 65536L);
array[10] = (byte)((num4 - (long)((ulong)array[12] * 16777216UL) - (long)((int)array[11] * 65536)) / 256L);
array[9] = (byte)(num4 - (long)((ulong)array[12] * 16777216UL) - (long)((int)array[11] * 65536) - (long)((int)array[10] * 256));
array[16] = (byte)(num5 / 16777216L);
array[15] = (byte)((num5 - (long)((ulong)array[16] * 16777216UL)) / 65536L);
array[14] = (byte)((num5 - (long)((ulong)array[16] * 16777216UL) - (long)((int)array[15] * 65536)) / 256L);
array[13] = (byte)(num5 - (long)((ulong)array[16] * 16777216UL) - (long)((int)array[15] * 65536) - (long)((int)array[14] * 256));
array2[1] = (byte)(array2[1] ^ array[1]);
int j = 1;
while (j < 16)
{
j++;
array2[j] = (byte)(array2[j] ^ array2[j - 1] ^ array[j]);
}
j = 0;
while (j < 16)
{
j++;
if (array2[j] == 0)
{
array2[j] = 102;
}
}
byte[] array3 = new byte[16];
Buffer.BlockCopy(array2, 1, array3, 0, 16);
return array3;
}
}
}
--------------------------------------------------------------------------------------------------------------------
wpf代码:
[C#] 纯文本查看 复制代码 <Window x:Class="RegisterAccount.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="账号注册" Height="250" Width="350">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox x:Name="TbUsername" Grid.Row="0" Margin="5" VerticalAlignment="Center" PlaceholderText="用户名"/>
<PasswordBox x:Name="PbPwd" Grid.Row="1" Margin="5" VerticalAlignment="Center" PlaceholderText="密码"/>
<PasswordBox x:Name="PbConfirm" Grid.Row="2" Margin="5" VerticalAlignment="Center" PlaceholderText="确认密码"/>
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
<Button x:Name="BtnRegister" Content="注册" Margin="5" Click="BtnRegister_Click"/>
<Button x:Name="BtnRetrievePwd" Content="找回密码" Margin="5" Click="BtnRetrievePwd_Click"/>
</StackPanel>
</Grid>
</Window>
|