/// <summary> /// Function that executes the given command on the database and returns the result. /// </summary> /// <param name="myquery">the query (stored procedure) to execute on the database.</param> /// <returns>A dataset which is filled with the table(s) with results from the stored procedure.</returns> public DataSet Query(string myquery) { DataSet TempDataSet = new DataSet(); try { DB2DataAdapter DataAdapter = new DB2DataAdapter(myquery, ConnString); DataAdapter.Fill(TempDataSet); } catch (SqlException e_dbconn_sql) { SocketClass.LogError(e_dbconn_sql, "LogServer.txt"); //Create an error message TempDataSet = new DataSet("Replies"); TempDataSet.Tables.Add("Reply"); TempDataSet.Tables[0].Columns.Add("INT"); DataRow newRow = TempDataSet.Tables[0].NewRow(); newRow[0] = -404; TempDataSet.Tables[0].Rows.Add(newRow); } catch (TimeoutException e_dbconn_conn) { SocketClass.LogError(e_dbconn_conn, "LogServer.txt"); //Create an error message TempDataSet = new DataSet("Replies"); TempDataSet.Tables.Add("Reply"); TempDataSet.Tables[0].Columns.Add("INT"); DataRow newRow = TempDataSet.Tables[0].NewRow(); newRow[0] = -404; TempDataSet.Tables[0].Rows.Add(newRow); } return(TempDataSet); }
private void SignUpButton(object sender, RoutedEventArgs e) { User user = new User(username.Text); string path = Path.path + username.Text + ".key"; UTF8Encoding utf8 = new UTF8Encoding(false); //本地保存公私钥 using (StreamWriter sw = new StreamWriter(path, false, utf8)) { sw.WriteLine("<" + username.Text + ">"); sw.WriteLine(user.GetUserKeys()[0].ToString()); sw.WriteLine(user.GetUserKeys()[1].ToString()); } //远程注册请求 string str = "<SignUp>" + user.GetUserKeys()[1].ToString(); string recv = SocketClass.Send(str); if (recv.CompareTo("sign up success") == 0) { Close(); } else { username.Text = recv; } }
/// <summary> /// 构造函数 /// </summary> public ModbusTCPClass() { //变量初始化 ModBusTCPFrameObject = new ModBusTCPFrameClass(); SocketObject = new SocketClass(); fReadTimeout = 1000; }
/// <summary> /// 构造函数 /// </summary> public ModbusTCPClass() { //变量初始化 ModBusTCPFrameObject = new ModBusTCPFrameClass(); SocketRegObject = new SocketClass(); SocketCoilObject = new SocketClass(); fReadTimeout = 1000; }
private void SignIn(object sender, RoutedEventArgs e) { //登陆实例化 FileInfo file = new FileInfo(Path.path + users.Text + ".key"); if (file.Exists) { using (StreamReader sr = new StreamReader(Path.path + users.Text + ".key", new UTF8Encoding(false))) { string uname = sr.ReadLine(); string privateKey = sr.ReadLine(); string publicKey = sr.ReadLine(); //远程登陆请求 string str = "<SignIn>" + publicKey; string recv = SocketClass.Send(str); if (recv.CompareTo("sign in success") == 0) { ShowInTaskbar = false; WindowState = WindowState.Minimized; Visibility = Visibility.Hidden; // 进入登陆界面 Console.WriteLine("进入登陆界面"); Cash cash = new Cash(new User(uname, BigInt.HexCode(privateKey), BigInt.HexCode(publicKey))); cash.Show(); } else if (recv.CompareTo("admin sign in success") == 0) { ShowInTaskbar = false; WindowState = WindowState.Minimized; Visibility = Visibility.Hidden; // 进入登陆界面 Console.WriteLine("进入监管者登陆界面"); Regulator regulator = new Regulator(); regulator.Show(); } else { users.Text = recv; } } } }
private void flush(object sender, RoutedEventArgs e) { string anwser = SocketClass.Send("<GetBil>" + "0xC593A92EC43E6A90"); Crypto crypto = new Crypto(new BigInt(Convert.ToUInt64("0xC593A92EC43E6A90", 16)), new BigInt(Convert.ToUInt64("0x9560FBB1826FE800", 16))); string[] bill_list = anwser.Split('|'); if (bill_list.Length == 1) { debug.Items.Add("无新交易"); } else { for (int i = 0; i < bill_list.Length / 11; i++) { debug.Items.Add("转账:" + bill_list[11 * i + 1]); string[] s = { bill_list[11 * i + 9], bill_list[11 * i + 10] }; string v = crypto.DecodeData(s); ulong money; try { money = Convert.ToUInt64(v.Split('x')[1], 16); debug.Items.Add("金额:" + money.ToString()); debug.Items.Add("交易签名为" + bill_list[11 * i]); using (StreamWriter sw = new StreamWriter(Path.path + "0xC593A92EC43E6A90" + "\\" + bill_list[11 * i] + ".cash", false, new UTF8Encoding(false))) { for (int w = 0; w < 11; w++) { sw.WriteLine(bill_list[11 * i + w]); } sw.WriteLine(v); } flush(); } catch (Exception ex) { debug.Items.Add("!!!! 该交易信息存在问题,请注意查证 !!!!"); debug.Items.Add(ex.Message); } } } flush(); }
/// <summary> /// Function that sends a query to the database and returns the results in a dataset. /// </summary> /// <param name="myquery">The query (in string-format) that we want to execute on the database</param> /// <returns>Dataset with the results of the query</returns> public DataSet Query(string myquery) { DataSet TempDataSet = new DataSet(); try { //connect to the dbms OdbcDataAdapter DataAdapter = new OdbcDataAdapter(myquery, ConnStringBuild.ConnectionString); //fills the dataset DataAdapter.Fill(TempDataSet); } catch (SqlException e_dbconn_sql) { Console.WriteLine("1st error"); SocketClass.LogError(e_dbconn_sql, "LogServer.txt"); //Create an error message TempDataSet = new DataSet("Replies"); TempDataSet.Tables.Add("Reply"); TempDataSet.Tables[0].Columns.Add("INT"); DataRow newRow = TempDataSet.Tables[0].NewRow(); newRow[0] = -404; TempDataSet.Tables[0].Rows.Add(newRow); } catch (TimeoutException e_dbconn_conn) { Console.WriteLine("2nd error"); SocketClass.LogError(e_dbconn_conn, "LogServer.txt"); //Create an error message TempDataSet = new DataSet("Replies"); TempDataSet.Tables.Add("Reply"); TempDataSet.Tables[0].Columns.Add("INT"); DataRow newRow = TempDataSet.Tables[0].NewRow(); newRow[0] = -404; TempDataSet.Tables[0].Rows.Add(newRow); } return(TempDataSet); }
private static void GetSingleton() { _SocketObject = new SocketClass(); }
private void MultiFun(object sender, RoutedEventArgs e) { if (mode.Content.ToString().CompareTo("购币") == 0) { int prices; try { prices = int.Parse(price.Text); } catch (Exception ex) { debug.Items.Add("<错误> 请输入合法的交易金额!"); debug.Items.Add(" "); return; } if (prices <= 0) { debug.Items.Add("<错误> 请输入合法的交易金额!"); debug.Items.Add(" "); return; } string anwser = SocketClass.Send("<BuyCon>" + current_user.GetUserKeys()[1] + "|" + price.Text); if (anwser.CompareTo("Buy Coin Failed") == 0) { debug.Items.Add("<错误> 购币失败!"); debug.Items.Add(" "); return; } string filename = new BigInt((ulong)Convert.ToUInt64(anwser.Split('|')[0])).ToString(); string money = anwser.Split('|')[1]; string r = anwser.Split('|')[2]; string sn = new BigInt(Crypto.Hash(anwser.Split('|')[2] + current_user.GetUserKeys()[0])).ToString(); anwser.Replace("|", "\n"); debug.Items.Add("购币成功!本次购币承诺为" + filename); using (StreamWriter sw = new StreamWriter(Path.path + current_user.GetUserKeys()[1].ToString() + "\\" + filename + ".cash", false, new UTF8Encoding(false))) { sw.WriteLine(filename); sw.WriteLine(sn); sw.WriteLine(money); sw.WriteLine(r); } flush(); } else if (mode.Content.ToString().CompareTo("付款") == 0) { BigInt CM_o; BigInt SN_o; uint v_o; ulong r_o; using (StreamReader sr = new StreamReader(Path.path + current_user.GetUserKeys()[1].ToString() + "\\" + check.Text + ".cash", new UTF8Encoding(false))) { CM_o = BigInt.HexCode(sr.ReadLine()); SN_o = BigInt.HexCode(sr.ReadLine()); v_o = Convert.ToUInt32(sr.ReadLine()); r_o = Convert.ToUInt64(sr.ReadLine()); } uint v_r; try { v_r = uint.Parse(price.Text); } catch (Exception ex) { debug.Items.Add("<错误> 请输入合法的交易金额!"); debug.Items.Add(" "); return; } if (v_r <= 0 || v_r > v_o) { debug.Items.Add("<错误> 请输入合法的交易金额!"); debug.Items.Add(" "); return; } string CM_spk = current_user.GetUserKeys()[1].ToString(); string CM_rpk = user.Text; uint v_s = v_o - v_r; if (v_s > 0) { BigInt r_s1 = BigInt.Random(); BigInt CM_s = 7 ^ new BigInt(v_s) + BigInt.HexCode(current_user.GetUserKeys()[1].ToString()) ^ r_s1; BigInt r_r1 = BigInt.Random(); BigInt CM_r = 7 ^ new BigInt(v_r) + BigInt.HexCode(current_user.GetUserKeys()[1].ToString()) ^ r_r1; if (CM_rpk.Split('x')[0] != "0") { debug.Items.Add("<错误> 请输入合法的收款地址!"); debug.Items.Add(" "); return; } string[] Ev_r = new Crypto(BigInt.HexCode(CM_rpk)).EncodeData(new BigInt(v_r).ToString()); string[] Ev_r_ = new Crypto(BigInt.HexCode("0xC593A92EC43E6A90")).EncodeData(new BigInt(v_r).ToString()); string s = "<Paymen>" + SN_o.ToString() + "|" + r_r1.ToString() + "|" + CM_spk + "|" + CM_rpk + "|" + CM_o.ToString() + "|" + CM_r.ToString() + "|" + CM_s.ToString() + "|" + Ev_r[0] + "|" + Ev_r[1] + "|" + Ev_r_[0] + "|" + Ev_r_[1]; string anwser = SocketClass.Send(s); Console.WriteLine(anwser); if (anwser.CompareTo("pay success") == 0) { using (StreamWriter sw = new StreamWriter(Path.path + current_user.GetUserKeys()[1].ToString() + "\\" + CM_r.ToString() + ".cash", false, new UTF8Encoding(false))) { sw.WriteLine(CM_s.ToString()); sw.WriteLine(new BigInt(Crypto.Hash(r_s1.ToString() + current_user.GetUserKeys()[0])).ToString()); sw.WriteLine(v_s); sw.WriteLine(Convert.ToUInt64(r_s1.ToString(), 16).ToString()); File.Delete(Path.path + current_user.GetUserKeys()[1].ToString() + "\\" + CM_o.ToString() + ".cash"); debug.Items.Add("==== 发布交易 ===="); debug.Items.Add("SN_o: " + SN_o.ToString()); debug.Items.Add("r_r1: " + r_r1.ToString()); debug.Items.Add("CM_spk: " + CM_spk); debug.Items.Add("CM_rpk: " + CM_rpk); debug.Items.Add("CM_o: " + CM_o.ToString()); debug.Items.Add("CM_r: " + CM_r.ToString()); debug.Items.Add("CM_s: " + CM_s.ToString()); debug.Items.Add("E(v_r): " + Ev_r[0] + " | " + Ev_r[1]); debug.Items.Add("E'(v_r): " + Ev_r_[0] + " | " + Ev_r_[1]); debug.Items.Add(" "); } flush(); } else { debug.Items.Add(anwser); } } else { string[] Ev_r = new Crypto(BigInt.HexCode(CM_rpk)).EncodeData(new BigInt(v_r).ToString()); string[] Ev_r_ = new Crypto(BigInt.HexCode("0xC593A92EC43E6A90")).EncodeData(new BigInt(v_r).ToString()); string s = "<Paymen>" + SN_o.ToString() + "|" + new BigInt(r_o).ToString() + "|" + CM_spk + "|" + CM_rpk + "|" + CM_o.ToString() + "|" + "0x0000000000000000" + "|" + CM_o.ToString() + "|" + Ev_r[0] + "|" + Ev_r[1] + "|" + Ev_r_[0] + "|" + Ev_r_[1]; string anwser = SocketClass.Send(s); Console.WriteLine(anwser); if (anwser.CompareTo("pay success") == 0) { File.Delete(Path.path + current_user.GetUserKeys()[1].ToString() + "\\" + CM_o.ToString() + ".cash"); debug.Items.Add("==== 发布交易 ===="); debug.Items.Add("SN_o: " + SN_o.ToString()); debug.Items.Add("r_r1: " + r_o.ToString()); debug.Items.Add("CM_spk: " + CM_spk); debug.Items.Add("CM_rpk: " + CM_rpk); debug.Items.Add("CM_o: " + CM_o.ToString()); debug.Items.Add("CM_r: " + CM_o.ToString()); debug.Items.Add("CM_s: " + "无"); debug.Items.Add("E(v_r): " + Ev_r[0] + " | " + Ev_r[1]); debug.Items.Add("E'(v_r): " + Ev_r_[0] + " | " + Ev_r_[1]); debug.Items.Add(" "); flush(); } else { debug.Items.Add(anwser); } } } else if (mode.Content.ToString().CompareTo("收款") == 0) { string anwser = SocketClass.Send("<GetBil>" + current_user.GetUserKeys()[1]); string[] bill_list = anwser.Split('|'); if (bill_list.Length == 1) { debug.Items.Add("当前无可用收款"); } else { for (int i = 0; i < bill_list.Length / 4; i++) { debug.Items.Add("转账:" + bill_list[4 * i + 1]); string[] s = { bill_list[4 * i + 2], bill_list[4 * i + 3] }; string v = current_user.Decrypt(s); ulong money; try { money = Convert.ToUInt64(v.Split('x')[1], 16); debug.Items.Add("金额:" + money.ToString()); string sn = new BigInt(Crypto.Hash(bill_list[4 * i] + current_user.GetUserKeys()[0])).ToString(); debug.Items.Add("交易成功!本次交易承诺为" + bill_list[4 * i + 1]); using (StreamWriter sw = new StreamWriter(Path.path + current_user.GetUserKeys()[1].ToString() + "\\" + bill_list[4 * i + 1] + ".cash", false, new UTF8Encoding(false))) { sw.WriteLine(bill_list[4 * i + 1]); sw.WriteLine(sn); sw.WriteLine(money.ToString()); sw.WriteLine(Convert.ToUInt64(bill_list[4 * i]).ToString()); } flush(); } catch (Exception ex) { debug.Items.Add("无效交易"); } } } } }