Пример #1
0
        public DataTable setuser(string ip, string port, string name, string pwd)
        {
            string res = "";
            string sql = "select * from TB_User u inner join TB_IPList IP on u.User_ID = ip.Sel_User where (ip.Sel_IP = @ip and ip.Sel_Port = @port) or u.User_Name = @name";

            SqlParameter[] para1 = { new SqlParameter("@ip", ip), new SqlParameter("@port", port), new SqlParameter("@name", name) };
            DataTable      dt    = new Models.SQLHelper().ExcuteQuery(sql, para1, CommandType.Text);

            if (dt.Rows.Count != 0)
            {
                return(new DataTable());
            }
            string[] keys = new Tools.Encrypt().CreateRSAKey();//创建秘钥
            sql = "insertuser";
            SqlParameter[] para2 =
            {
                new SqlParameter("@ip",    ip),
                new SqlParameter("@port",  port),
                new SqlParameter("@name",  name),
                new SqlParameter("pubkey", keys[0]),
                new SqlParameter("prikey", keys[1])
            };
            dt = new Models.SQLHelper().ExcuteQuery(sql, para2, CommandType.StoredProcedure);
            return(dt);
        }
Пример #2
0
        /// <summary>
        /// 发送交易请求
        /// </summary>
        /// <param name="data"></param>
        /// <param name="IP"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public string getDealChecked(string data, string IP = "http://localhost", string port = "58465")
        {
            string res;

            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            string now = currentTime.ToString();

            now  = now.Replace("/", "-");
            data = now + "|" + data;                                        //加入时间戳 格式:"2018/7/23 16:51:50|data"
            data = new Tools.Encrypt().RSAEncrypt(data, Users.User.PubKey); //data加密
            //data = System.Web.HttpUtility.UrlEncode(data);
            DataTable resdt = new DataTable();

            resdt.Columns.Add("ID", Type.GetType("System.String"));   //ID列表
            resdt.Columns.Add("User", Type.GetType("System.String")); //加密人
            resdt.Columns.Add("IP", Type.GetType("System.String"));   //IP
            resdt.Columns.Add("Port", Type.GetType("System.String")); //端口
            resdt.Columns.Add("Data", Type.GetType("System.String")); //加密后的data
            resdt.Rows.Add();
            resdt.Rows[0]["ID"]   = Users.User.ID;
            resdt.Rows[0]["User"] = Users.User.Name;
            resdt.Rows[0]["IP"]   = Users.User.IP;
            resdt.Rows[0]["Port"] = Users.User.Port;
            resdt.Rows[0]["Data"] = data;
            string poststr = new Tools.jstodt().ToJson(resdt);

            poststr = System.Web.HttpUtility.UrlEncode(poststr);
            string url = IP + ":" + port + "/api/Deal/postCheckDeal";//;?dataget="+ poststr;

            try
            {
                res = new Tools.HttpHelper().HttpPost(url, poststr);
                //res = new Tools.HttpHelper().HttpGet(url);//发送确认
            }
            catch (Exception)
            {
                throw;
            }
            res = System.Web.HttpUtility.UrlDecode(res);
            DataTable dt         = new Tools.jstodt().ToDataTable(res);
            string    ID         = dt.Rows[0]["ID"].ToString();
            string    datalocked = dt.Rows[0]["Data"].ToString();

            #region 检查确认数据
            string pubkeyres = new Tools.HttpHelper().HttpGet(IP + ":" + port + "/api/IPSelect/getpublickey?id=" + ID);
            pubkeyres = System.Web.HttpUtility.UrlDecode(pubkeyres);
            DataTable pubdt  = new Tools.jstodt().ToDataTable(pubkeyres);
            string    pubkey = pubdt.Rows[0]["User_PriKey"].ToString();
            string    data1  = new Tools.Encrypt().RSADecrypt(datalocked, pubkey);//解密
            #endregion
            return(res);
        }
Пример #3
0
        // GET: api/Hash
        /// <summary>
        /// 其他用户接收到交易数据
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public string PostUnlocked(string IP = "http://localhost", string port = "58465")
        {
            string data = HttpContext.Current.Request.Form.ToString();//获取传来的数据

            try
            {
                #region 数据验证
                string res = "";                                             //
                res = System.Web.HttpUtility.UrlDecode(data);                //数据解码
                DataTable dtget       = new Tools.jstodt().ToDataTable(res); //将获取到的Json打成datatable
                string    ID1         = dtget.Rows[0]["ID"].ToString();      //获取ID
                string    data1locked = dtget.Rows[0]["Data"].ToString();    //第一层数据
                string    key1        = new Tools.HttpHelper().HttpGet(IP + ":" + port + "/api/IPSelect/getpublickey?id=" + ID1);
                key1 = System.Web.HttpUtility.UrlDecode(key1);               //获取到秘钥必须解码
                DataTable key1dt = new Tools.jstodt().ToDataTable(key1);
                key1 = key1dt.Rows[0]["User_PriKey"].ToString();
                string data1unlocked = new Tools.Encrypt().RSADecrypt(data1locked, key1); //第一层解密
                string date1         = data1unlocked.Split('|')[0];                       //第一个日期
                string datalocked2   = data1unlocked.Replace(date1 + "|", "");            //第二层加密数据
                datalocked2 = System.Web.HttpUtility.UrlDecode(datalocked2);              //解码

                DataTable dtdata2     = new Tools.jstodt().ToDataTable(datalocked2);      //将获取到的Json打成datatable
                string    ID2         = dtdata2.Rows[0]["ID"].ToString();                 //获取ID
                string    data2locked = dtdata2.Rows[0]["Data"].ToString();               //第二层数据
                string    key2        = new Tools.HttpHelper().HttpGet(IP + ":" + port + "/api/IPSelect/getpublickey?id=" + ID2);
                key2 = System.Web.HttpUtility.UrlDecode(key2);                            //获取到秘钥解码
                DataTable key2dt = new Tools.jstodt().ToDataTable(key2);
                key2 = key2dt.Rows[0]["User_PriKey"].ToString();
                string data2unlocked = new Tools.Encrypt().RSADecrypt(data2locked, key2);//第二层解密
                #endregion

                #region 数据存入记录
                string          hash = new Tools.Hash().Hash_SHA_256(data); //计算hash
                Users.ChainNode node = new Users.ChainNode();               //新建存储节点
                node.data = data;
                node.hash = hash;
                Services.Block.chainlock.WaitOne();      //等待互斥信号量
                Services.Block.chain.AddLast(node);      //插入
                Services.Block.chainlock.ReleaseMutex(); //释放互斥信号量
                #endregion
            }
            catch (Exception e)
            {
                string exp = e.ToString();
                return("error");
            }
            return("true");
        }
Пример #4
0
        /// <summary>
        /// 接收并确认交易请求
        /// </summary>
        /// <returns></returns>
        public string postCheckDeal()
        {
            string dataget = HttpContext.Current.Request.Form.ToString();

            dataget = System.Web.HttpUtility.UrlDecode(dataget);
            DataTable dt         = new Tools.jstodt().ToDataTable(dataget);
            string    ID         = dt.Rows[0]["ID"].ToString();
            string    datalocked = dt.Rows[0]["Data"].ToString();

            #region 检查确认数据
            string url    = "http://10.14.4.167:9503/api/IPSelect/getpublickey?id=" + ID;
            string pubkey = new Tools.HttpHelper().HttpGet(url);
            string data   = new Tools.Encrypt().RSADecrypt(datalocked, pubkey);//解密
            #endregion
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            string now = currentTime.ToString();
            now        = now.Replace("/", "-");
            dataget    = System.Web.HttpUtility.UrlEncode(dataget);
            datalocked = now + "|" + dataget;                                           //加入时间戳 格式:"2018/7/23 16:51:50|data"
            datalocked = new Tools.Encrypt().RSAEncrypt(datalocked, Users.User.PubKey); //data加密
            DataTable resdt = new DataTable();
            resdt.Columns.Add("ID", Type.GetType("System.String"));                     //ID列表
            resdt.Columns.Add("User", Type.GetType("System.String"));                   //加密人
            resdt.Columns.Add("IP", Type.GetType("System.String"));                     //IP
            resdt.Columns.Add("Port", Type.GetType("System.String"));                   //端口
            resdt.Columns.Add("Data", Type.GetType("System.String"));                   //加密后的data
            resdt.Rows.Add();
            resdt.Rows[0]["ID"]   = Users.User.ID;
            resdt.Rows[0]["User"] = Users.User.Name;
            resdt.Rows[0]["IP"]   = Users.User.IP;
            resdt.Rows[0]["Port"] = Users.User.Port;
            resdt.Rows[0]["Data"] = datalocked;
            string retstr = new Tools.jstodt().ToJson(resdt);
            retstr = System.Web.HttpUtility.UrlEncode(retstr);
            //string url2 = "http://localhost:58495/api/Hash/PostHash";//;?dataget="+ poststr;
            //string res = new Tools.HttpHelper().HttpPost(url2, retstr);//完全揭秘
            string url2 = "http://localhost:58495/api/Hash/PostUnlocked";//;?dataget="+ poststr;
            string res  = new Tools.HttpHelper().HttpPost(url2, retstr);
            return(retstr);
        }