public bool authenticate_user(string ssn1, string key) { try { crypto c = new crypto(); int ssn = Convert.ToInt32(c.Decrypt(ssn1, key, true)); DataTable dt_auth = new DataTable(); string connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; string query = "select * from tbl_voter_master where ssn = " + ssn; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt_auth); conn.Close(); da.Dispose(); if (dt_auth.Rows.Count > 0) { TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider(); TDES.GenerateIV(); TDES.GenerateKey(); SqlParameter[] sp_validation_key = new SqlParameter[2]; sp_validation_key[0] = new SqlParameter("@ssn", ssn); string validation = c.RandomString(); sp_validation_key[1] = new SqlParameter("@validation", validation); string query1 = "update tbl_voter_master set validation_key = '" + validation + "' where ssn = " + ssn; SqlConnection conn1 = new SqlConnection(connString); SqlCommand cmd1 = new SqlCommand(query1, conn1); conn1.Open(); SqlDataAdapter da1 = new SqlDataAdapter(cmd1); da1.Fill(dt_auth); conn1.Close(); da1.Dispose(); return(true); } else { return(false); } } catch (Exception ex) { throw ex; } }
public void showResult() { crypto c = new crypto(); DataTable dt_results = new DataTable(); string connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; string query = "select * from tbl_CTF_Directory"; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt_results); conn.Close(); da.Dispose(); DataTable dt_final = new DataTable(); dt_final.Columns.AddRange(new DataColumn[1] { new DataColumn("vote", typeof(string)) }); for (int i = 0; i < dt_results.Rows.Count; i++) { dt_final.Rows.Add(c.Decrypt(dt_results.Rows[i]["vote"].ToString(), dt_results.Rows[i]["ssn"].ToString(), true)); } DataTable dt_cadidate = new DataTable(); connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; query = "select * from tbl_candidate_master"; conn = new SqlConnection(connString); cmd = new SqlCommand(query, conn); conn.Open(); da = new SqlDataAdapter(cmd); da.Fill(dt_cadidate); conn.Close(); da.Dispose(); DataTable dt_display = new DataTable(); dt_display.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)), new DataColumn("vote", typeof(string)) }); int cad1 = 0; int cad2 = 0; for (int i = 0; i < dt_final.Rows.Count; i++) { if (Convert.ToInt32(dt_final.Rows[i]["vote"].ToString()) == 1) { cad1++; } else if (Convert.ToInt32(dt_final.Rows[i]["vote"].ToString()) == 2) { cad2++; } } dt_display.Rows.Add(dt_cadidate.Rows[0]["c_name"].ToString(), cad1); dt_display.Rows.Add(dt_cadidate.Rows[1]["c_name"].ToString(), cad2); for (int i = 0; i < dt_display.Rows.Count; i++) { Console.WriteLine(dt_display.Rows[i]["Name"].ToString() + " : " + dt_display.Rows[i]["vote"].ToString()); } }
static void Main(string[] args) { CLA cla = new CLA(); crypto c = new crypto(); Console.WriteLine("Enter Your SSN to validate. or Enter 0 for result. \n"); String ssn = Console.ReadLine(); if (ssn.Equals("0") == true) { CTF ctf = new CTF(); ctf.showResult(); } else { Console.WriteLine(); if (cla.authenticate_user(c.Encrypt(ssn, ssn, true), ssn) == true) { DataTable dt_votechk = new DataTable(); string connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; string query = "select * from tbl_CTF_Directory where ssn = '" + ssn + "'"; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt_votechk); conn.Close(); da.Dispose(); if (dt_votechk.Rows.Count > 0) { Console.WriteLine("already voted."); } else { DataTable dt_voter = new DataTable(); connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; query = "select * from tbl_voter_master where ssn = '" + ssn + "'"; conn = new SqlConnection(connString); cmd = new SqlCommand(query, conn); conn.Open(); da = new SqlDataAdapter(cmd); da.Fill(dt_voter); conn.Close(); da.Dispose(); string validation = c.Encrypt(dt_voter.Rows[0]["validation_key"].ToString(), ssn, true); Console.WriteLine("***********************************************"); Console.WriteLine("here are voter's list."); DataTable dt_can = new DataTable(); string connString1 = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; string query1 = "select id, c_name from tbl_candidate_master"; SqlConnection conn1 = new SqlConnection(connString1); SqlCommand cmd1 = new SqlCommand(query1, conn1); conn1.Open(); SqlDataAdapter da1 = new SqlDataAdapter(cmd1); da1.Fill(dt_can); conn1.Close(); da1.Dispose(); for (int i = 0; i < dt_can.Rows.Count; i++) { Console.WriteLine("press " + dt_can.Rows[i]["id"].ToString() + " for " + dt_can.Rows[i]["c_name"].ToString()); } Console.WriteLine("***********************************************"); Console.Write("Enter yout vote: "); string vote = Console.ReadLine(); DataTable dt = new DataTable(); connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; query = "insert into tbl_CTF_Directory (ssn, vote, validation) values(" + Convert.ToInt32(ssn) + ", '" + c.Encrypt(vote, ssn, true) + "', '" + c.Decrypt(validation, ssn, true) + "')"; conn = new SqlConnection(connString); cmd = new SqlCommand(query, conn); conn.Open(); da = new SqlDataAdapter(cmd); da.Fill(dt); conn.Close(); da.Dispose(); Console.WriteLine("vote is secured."); } } else { Console.WriteLine("invalid ssn."); } } Console.ReadLine(); }