在学习了一些 Java 之后,我最近几天才开始学习 C#,所以作为一种简单的程序,我正在尝试制作一个程序来保存一些密码。
我将注释掉有问题的代码。
我的主要表单代码是:
{
public partial class fLogin : Form
{
public fLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
bool checkUser = md5(tbUsername.Text) == Properties.Settings.Default.Username;
bool checkPass = md5(tbPassword.Text) == Properties.Settings.Default.Password;
if (checkUser && checkPass)
{
Passwords pFrame = new Passwords();
pFrame.Activate();
this.Hide();
}
}
private String md5(String tBox)
{
MD5 md5 = new MD5CryptoServiceProvider();
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(tBox));
byte[] result = md5.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
strBuilder.Append(result[i].ToString("x2"));
}
String finalValue = strBuilder.ToString();
return finalValue;
}
private void fLogin_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.Username == "null" || Properties.Settings.Default.Password == "null")
{
//This is where I create my second Form!.
//CreatePassword newPass = new CreatePassword();
//this.Hide();
//newPass.Show();
}
else if (md5(tbUsername.Text) == Properties.Settings.Default.Username && md5(tbPassword.Text) == Properties.Settings.Default.Password)
{
Passwords pFrame = new Passwords();
this.Hide();
pFrame.Show();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnCPass_Click(object sender, EventArgs e)
{
ChangePass CPFrame = new ChangePass();
CPFrame.ShowDialog(this);
}
}
}
我的第二种形式代码是
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace PassMan
{
public partial class CreatePassword : Form
{
public CreatePassword()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
String username = md5(tbUsername.Text);
String password = md5(tbPassword.Text);
String[] pwds = { username, password };
Properties.Settings.Default.Username = username;
Properties.Settings.Default.Password = password;
Properties.Settings.Default.Save();
//This is where I Exit. It Closes this form & the other form!
//this.Close();
}
private String md5(String tBox)
{
MD5 md5 = new MD5CryptoServiceProvider();
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(tBox));
byte[] result = md5.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
strBuilder.Append(result[i].ToString("x2"));
}
String finalValue = strBuilder.ToString();
return finalValue;
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
Application.Exit();
}
}
}