Advertisement
Kirsiina

bensiskirjautuminen

Oct 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Security.Cryptography;
  12.  
  13. namespace Bensis
  14. {
  15.     public partial class HallintaKysely : Form
  16.     {
  17.  
  18.         public static string kayttajatunnus;
  19.         public static string salasana;
  20.  
  21.         string tiedostoPolku = AppDomain.CurrentDomain.BaseDirectory + @"\tunnukset.txt";
  22.  
  23.         public HallintaKysely()
  24.         {
  25.  
  26.             if (!File.Exists(tiedostoPolku))
  27.             {
  28.                 alustaTunnuksetTiedosto();
  29.             }
  30.             InitializeComponent();
  31.         }
  32.  
  33.         private string kasitteleRivi(string str)
  34.         {
  35.             str = str.Replace(" ", string.Empty);
  36.             string[] tmp = str.Split('=');
  37.             return tmp[1];
  38.         }
  39.  
  40.         private string alustaTunnuksetTiedosto()
  41.         {
  42.             string salasana = "1234";
  43.  
  44.             MD5CryptoServiceProvider md5Kryptaaja = new MD5CryptoServiceProvider();
  45.             byte[] data = Encoding.ASCII.GetBytes(salasana);
  46.             data = md5Kryptaaja.ComputeHash(data);
  47.  
  48.             string md5tiiviste = "";
  49.             for (int i = 0; i < data.Length; i++)
  50.                 md5tiiviste += data[i].ToString("x2").ToLower();
  51.  
  52.             using (StreamWriter outputFile = new StreamWriter(tiedostoPolku))
  53.             {
  54.                 outputFile.WriteLine("Käyttäjätunnus = admin");
  55.                 outputFile.WriteLine("Salasana = " + md5tiiviste);
  56.             }
  57.  
  58.             return BitConverter.ToString(data);
  59.         }
  60.  
  61.         private void kirjauduNappi_Click(object sender, EventArgs e)
  62.         {
  63.             alustaTunnuksetTiedosto();
  64.             using (StreamReader sr = new StreamReader(tiedostoPolku))
  65.             {
  66.                 kayttajatunnus = sr.ReadLine();
  67.                 kayttajatunnus = kasitteleRivi(kayttajatunnus);
  68.  
  69.                 salasana = kasitteleRivi(sr.ReadLine());
  70.             }
  71.  
  72.             byte[] hs = new byte[50];
  73.             salasana = salasanaBox.Text;
  74.             MD5 md5 = MD5.Create();
  75.             byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(salasana);
  76.             byte[] hash = md5.ComputeHash(inputBytes);
  77.             StringBuilder sb = new StringBuilder();
  78.             for (int i = 0; i < hash.Length; i++)
  79.             {
  80.                 hs[i] = hash[i];
  81.                 sb.Append(hs[i].ToString("x2"));
  82.             }
  83.             var hash_pass = sb.ToString();
  84.  
  85.             if (kayttajatunnus == kayttajatunnusBox.Text && salasana == salasanaBox.Text)
  86.             {
  87.                 this.Close();
  88.                 Hallinta hallinta = new Hallinta();
  89.                 hallinta.Show();
  90.             } else
  91.             {
  92.                 MessageBox.Show("Käyttäjätunnus tai salasana on väärä!");
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement