Form1.cs
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;
namespace MySecureOTP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}
}
}
Form2.cs
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.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Security;
using System.Security.Cryptography;
namespace MySecureOTP
{
public partial class Form2 : Form
{
SqlConnection conn;
string connstring;
public Form2()
{
InitializeComponent();
//connstring = ConfigurationManager.ConnectionStrings"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MySecureOTP.mdf;Integrated Security=True;Connect Timeout=30"].ConnectionString
connstring = ConfigurationManager.ConnectionStrings["MySecureOTP.Properties.Settings.MySecureOTPConnectionString"].ConnectionString;
}
private void button1_Click(object sender, EventArgs e)
{
//Form3 f3 = new Form3();
string sb;
if (String.IsNullOrEmpty(textBox1.Text.ToString()) || String.IsNullOrEmpty(textBox2.Text.ToString()) || String.IsNullOrEmpty(textBox3.Text.ToString()))
{
MessageBox.Show("Login, parol yoki email kiritilmadi!!!\nQayta urinib kuring!!!");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
else
{
//Random son hosil qilish
Random rd = new Random();
int rand_num = rd.Next(100000, 999999);
string rnd = rand_num.ToString();
//Xeshni hisoblash
sb = textBox1.Text.ToString() + textBox4.Text.ToString() + rnd;
string slt = GetStringSha256Hash(sb);
//textBox5.Text = slt;
//bazaga yozish
string query1 = "INSERT INTO otp VALUES (@login, @parol, @email, @vaqt, @otp, @otphash)";
using (conn = new SqlConnection(connstring))
using (SqlCommand command = new SqlCommand(query1, conn))
{
conn.Open();
command.Parameters.AddWithValue("@login", textBox1.Text.ToString());
command.Parameters.AddWithValue("@parol", textBox2.Text.ToString());
command.Parameters.AddWithValue("@email", textBox3.Text.ToString());
command.Parameters.AddWithValue("@vaqt", textBox4.Text.ToString());
command.Parameters.AddWithValue("@otp", rnd.ToString());
command.Parameters.AddWithValue("@otphash", slt.ToString());
command.ExecuteNonQuery();
}
string query2 = "INSERT INTO mijoz VALUES (@otpcl)";
using (conn = new SqlConnection(connstring))
using (SqlCommand command = new SqlCommand(query2, conn))
{
conn.Open();
command.Parameters.AddWithValue("@otpcl", rnd.ToString());
command.ExecuteNonQuery();
}
open_otp();
MessageBox.Show("Ro‘yhatdan muvaffaqiyatli o‘tildi!!!\nMijozga OTP jo‘natildi!!!\nOTP ni tasdiqlashingizni so‘raymiz!!!");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox5.Clear();
}
}
int i = 0;
private void button2_Click(object sender, EventArgs e)
{
string otph, ln;
if (String.IsNullOrEmpty(textBox1.Text.ToString()) || String.IsNullOrEmpty(textBox2.Text.ToString()) || String.IsNullOrEmpty(textBox5.Text.ToString()))
{
MessageBox.Show("Login, parol yoki OTP kiritilmadi!!!\nQayta urinib kuring!!!");
textBox1.Clear();
textBox2.Clear();
textBox5.Clear();
}
else
{
ln = textBox1.Text.ToString() + textBox4.Text.ToString() + textBox5.Text.ToString();
otph = GetStringSha256Hash(ln);
string query = "SELECT * from otp WHERE login = '" + textBox1.Text.Trim() + "' and parol = '" + textBox2.Text.Trim() + "' and otp = '" + textBox5.Text.Trim() + "' and otphash = '" + otph.ToString() + "'";
using (conn = new SqlConnection(connstring))
{
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
Form4 fm = new Form4();
fm.Show();
textBox1.Clear();
textBox2.Clear();
textBox5.Clear();
}
else
{
textBox1.Clear();
textBox2.Clear();
textBox5.Clear();
MessageBox.Show("Login, parol yoki OTP noto‘g‘ri kiritilgan. Qayta urinib kuring!!!");
i++;
if (i == 3)
{
MessageBox.Show("Dastur bloklandi", "Yopilmoqda", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.Close();
}
}
}
}
}
internal static string GetStringSha256Hash(string text)
{
if (String.IsNullOrEmpty(text))
return String.Empty;
using (var sha = new System.Security.Cryptography.SHA256Managed())
{
byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hash = sha.ComputeHash(textData);
return BitConverter.ToString(hash).Replace("-", String.Empty);
}
}
//
public void open_otp()
{
using (conn = new SqlConnection(connstring))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM otp", conn))
{
DataTable usertable = new DataTable();
adapter.Fill(usertable);
dataGridView1.DataSource = usertable;
}
}
//
private void Form2_Load(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
textBox4.Text = dt.ToString();
checkBox1.Checked = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
label5.Visible = false;
textBox5.Visible = false;
button2.Visible = false;
label3.Visible = true;
textBox3.Visible = true;
button1.Visible = true;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
label5.Visible = true;
textBox5.Visible = true;
button2.Visible = true;
label3.Visible = false;
textBox3.Visible = false;
button1.Visible = false;
}
}
}
Dostları ilə paylaş: |