Last active
June 17, 2018 07:24
-
-
Save d630/725de91a754f807020d0bcd6d30f668a to your computer and use it in GitHub Desktop.
Windows Forms: Login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel; | |
| using System.Data; | |
| using System.Drawing; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Windows.Forms; | |
| using System.IO; | |
| namespace Login | |
| { | |
| public partial class Form1 : Form | |
| { | |
| public Form1() | |
| { | |
| InitializeComponent(); | |
| } | |
| private string encrypt(string password) | |
| { | |
| char[] c = new char[password.Length]; | |
| for (int i = 0; i < c.Length; i++) | |
| c[i] = (char)(password[i] + 1); | |
| return new string(c); | |
| } | |
| private string decrypt(string password) | |
| { | |
| char[] c = new char[password.Length]; | |
| for (int i = 0; i < c.Length; i++) | |
| c[i] = (char)(password[i] - 1); | |
| return new string(c); | |
| } | |
| private bool userExists(string user) | |
| { | |
| bool ret = false; | |
| FileStream fs = null; | |
| StreamReader sr = null; | |
| try | |
| { | |
| fs = new FileStream("passwd.txt", FileMode.Open, FileAccess.Read); | |
| sr = new StreamReader(fs); | |
| while (sr.Peek() != -1) | |
| { | |
| var p = sr.ReadLine().Split('\t'); | |
| if (p[0].Equals(user)) | |
| { | |
| ret = true; | |
| break; | |
| } | |
| } | |
| } | |
| catch (FileNotFoundException fnfe) | |
| { | |
| MessageBox.Show("Error: passwd file missing"); | |
| } | |
| finally | |
| { | |
| if (sr != null) | |
| sr.Close(); | |
| if (fs != null) | |
| fs.Close(); | |
| } | |
| return ret; | |
| } | |
| private bool createEntry(string user, string password) | |
| { | |
| bool ret = false; | |
| FileStream fs = null; | |
| StreamWriter sw = null; | |
| try | |
| { | |
| fs = new FileStream("passwd.txt", FileMode.Append, FileAccess.Write); | |
| sw= new StreamWriter(fs); | |
| sw.WriteLine(user + "\t" + password); | |
| ret = true; | |
| } | |
| catch (FileNotFoundException fnfe) | |
| { | |
| MessageBox.Show("Error: passwd file missing"); | |
| } | |
| finally | |
| { | |
| if (sw != null) | |
| sw.Close(); | |
| if (fs != null) | |
| fs.Close(); | |
| } | |
| return ret; | |
| } | |
| private bool findEntry(string user, string password) | |
| { | |
| bool ret = false; | |
| int entries = 0; | |
| FileStream fs = null; | |
| StreamReader sr = null; | |
| try | |
| { | |
| fs = new FileStream("passwd.txt", FileMode.Open, FileAccess.Read); | |
| sr = new StreamReader(fs); | |
| while (sr.Peek() != -1) | |
| { | |
| entries++; | |
| if (sr.ReadLine().Equals(user + "\t" + password)) | |
| { | |
| ret = true; | |
| break; | |
| } | |
| } | |
| } | |
| catch(FileNotFoundException fnfe) | |
| { | |
| MessageBox.Show("Error: passwd file missing"); | |
| } | |
| finally | |
| { | |
| if (sr != null ) | |
| sr.Close(); | |
| if (fs != null) | |
| fs.Close(); | |
| if (entries == 0) | |
| MessageBox.Show("Error: passwd file is empty"); | |
| } | |
| return ret; | |
| } | |
| private void label2_Click(object sender, EventArgs e) | |
| { | |
| } | |
| private void label1_Click(object sender, EventArgs e) | |
| { | |
| } | |
| private void button1_Click(object sender, EventArgs e) | |
| { | |
| if (textBoxUser.Text == "" || textBoxPassword.Text == "") | |
| { | |
| MessageBox.Show("Error: user or password missing"); | |
| return; | |
| } | |
| if (!userExists(textBoxUser.Text)) | |
| { | |
| MessageBox.Show("Error: unknown user"); | |
| return; | |
| } | |
| if (findEntry(textBoxUser.Text, encrypt(textBoxPassword.Text))) | |
| { | |
| Form2 ok = new Form2(); | |
| ok.ShowDialog(); | |
| } | |
| else | |
| { | |
| MessageBox.Show("Error: unknown password"); | |
| } | |
| } | |
| private void button2_Click(object sender, EventArgs e) | |
| { | |
| if (textBoxUser.Text == "" || textBoxPassword.Text == "") | |
| { | |
| MessageBox.Show("Error: user or password missing"); | |
| return; | |
| } | |
| if (textBoxUser.Text.Contains("\t") || textBoxPassword.Text.Contains("\t")) | |
| { | |
| MessageBox.Show("Error: tab character is not allowed"); | |
| return; | |
| } | |
| if (userExists(textBoxUser.Text)) | |
| { | |
| MessageBox.Show("Error: user already registered"); | |
| return; | |
| } | |
| if (createEntry(textBoxUser.Text, encrypt(textBoxPassword.Text))) | |
| { | |
| Form2 ok = new Form2(); | |
| ok.ShowDialog(); | |
| } | |
| else | |
| { | |
| MessageBox.Show("Error: couldn't register new user"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO