コード例 #1
0
        ////////////////////////////////////////////////////////////////////////
        public async System.Threading.Tasks.Task <object> TimTick(object[] parameters)
        {
            string password = "";

            byte[] decryptedArr          = new byte[1];
            NameValueCollection formData = new NameValueCollection();

            //while (true)
            {
                if (TimTickEnabled == true)
                {
                    Uri    uriStr;
                    string autoIncrementIndexOfData = cntAutoInc.ToString();
                    string InputString = "";
                    if (password == "" && GlobalClass.TestSecretCloud() == true)
                    {
                        password = GlobalClass.ReadSecretCloud(); //Sha256
                    }
                    //******************* INITIATE PHPSESSION ***************************
                    CookieAwareWebClient webClient = new CookieAwareWebClient();
                    webClient.Encoding = System.Text.Encoding.Default;
                    formData.Clear();
                    formData["username"] = "******";
                    formData["password"] = "";
                    if ((formData["password"] == "") && GlobalClass.TestPasswordCloud() == true)
                    {
                        formData["password"] = GlobalClass.ReadPasswordCloud(); //Sha256
                    }

                    Uri.TryCreate("http://your.url.here/lo.php", UriKind.RelativeOrAbsolute, out uriStr);
                    Task <byte[]> loTaskString = webClient.UploadValuesTaskAsync(uriStr, "POST", formData);
                    await         loTaskString;
                    byte[]        responseBytes = loTaskString.Result;
                    string        responseHTML  = Encoding.UTF8.GetString(responseBytes);

                    if (Uri.TryCreate("http://your.url.here", UriKind.RelativeOrAbsolute, out uriStr) == false)
                    {
                        System.Diagnostics.Debug.WriteLine("NO");
                    }
                    foreach (Cookie cookie in webClient.CookieContainer.GetCookies(uriStr))
                    {
                        System.Diagnostics.Debug.WriteLine(cookie.Name);
                        System.Diagnostics.Debug.WriteLine(cookie.Value);
                    }
                    //******************* INITIATE PHPSESSION ***************************

                    //******************* DOWNLOAD XML FILE LIST ***************************
                    Uri.TryCreate("http://your.url.here/filelist.php", UriKind.RelativeOrAbsolute, out uriStr);
                    Task <string> fileListTaskString = webClient.DownloadStringTaskAsync(uriStr);
                    await         fileListTaskString;
                    string        fileList  = fileListTaskString.Result;
                    DataSet       ds        = new DataSet();
                    byte[]        byteArray = Encoding.UTF8.GetBytes(fileList);
                    ds.ReadXml(new MemoryStream(byteArray));
                    if (ds.Tables.Count > 0)
                    {
                        var result = ds.Tables[0];
                    }
                    dataGridView1.DataSource = ds.Tables[0];
                    //******************* DOWNLOAD XML FILE LIST ***************************

                    //******************* CHECK MAX ID ***************************
                    DataTable dtMaxId = ds.Tables[0];
                    using (var ms = new MemoryStream())
                    {
                        int   cnt   = dtMaxId.Rows.Count;
                        Int64 newID = Convert.ToInt64(dtMaxId.Rows[cnt - 1]["id"]);
                        autoIncrementIndexOfData = newID.ToString();
                        if (lastId < newID)
                        {
                            lastId = newID;
                            Debug.WriteLine(autoIncrementIndexOfData);
                            //******************* CHECK TIME ***************************
                            DateTime tim = Convert.ToDateTime(dtMaxId.Rows[cnt - 1]["time"]);
                            {
                                Debug.WriteLine(tim);
                            }

                            double timCmop = Convert.ToDouble((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds);
                            if (!((Convert.ToDouble(((tim.ToUniversalTime().AddSeconds(1)) - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds) - timCmop) >= 0.0))
                            {  //last picture is oler than ten seconds
                                Debug.WriteLine("No newId");
                                return(0);
                            }
                            //******************* CHECK TIME ***************************
                        }
                        else //no new Id
                        {
                            Debug.WriteLine("No newId");
                            return(0);
                        }
                    }
                    //******************* CHECK MAX ID ***************************

                    //******************* CHECK TIME  ***************************
                    //DataTable dtMaxTime = ds.Tables[0];
                    //using (var ms = new MemoryStream())
                    //{
                    //    int cnt = dtMaxTime.Rows.Count;
                    //    DateTime tim = Convert.ToDateTime(dtMaxTime.Rows[cnt - 1]["time"]);
                    //    {
                    //        Debug.WriteLine(tim);
                    //    }
                    //}
                    //******************* CHECK TIME  ***************************

                    //******************* DOWNLOAD IV ***************************
                    Task <byte []> ivArrTaskByte = webClient.DownloadDataTaskAsync("http://your.url.here/listiv.php?id=" + autoIncrementIndexOfData);
                    await          ivArrTaskByte;
                    byte[]         ivArr = ivArrTaskByte.Result;
                    //******************* DOWNLOAD IV ***************************

                    //******************* DOWNLOAD LENGTH ***************************
                    Task <byte[]> lengthTaskByte = webClient.DownloadDataTaskAsync("http://your.url.here/getLen.php?id=" + autoIncrementIndexOfData);
                    await         lengthTaskByte;
                    byte[]        length = lengthTaskByte.Result;
                    //filesize from files
                    int fileSize = 0;
                    foreach (byte l in length)
                    {
                        fileSize += (byte)(l - (byte)(0x30));
                        fileSize *= 10;
                    }
                    fileSize /= 10;
                    System.Diagnostics.Debug.WriteLine("filesize: " + fileSize.ToString());
                    //******************* DOWNLOAD LENGTH ***************************

                    //******************* DOWNL DATA ***************************
                    Uri.TryCreate("http://your.url.here/dwnl_delete.php?id=" + autoIncrementIndexOfData, UriKind.RelativeOrAbsolute, out uriStr);
                    Task <string> InputTaskString = webClient.DownloadStringTaskAsync(uriStr);
                    await         InputTaskString;
                    InputString = InputTaskString.Result;
                    //******************* DOWNL DATA ***************************


                    // Create sha256 hash
                    SHA256 mySHA256 = SHA256Managed.Create();
                    byte[] key      = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));

                    try
                    {
                        string decrypted = this.DecryptString(InputString, key, ivArr, fileSize);
                        decryptedArr = Convert.FromBase64String(decrypted);

                        //******************* DECRYPT ***************************

                        //******************* CHECK SIGN ***************************
                        Encoding        encoding  = Encoding.UTF8;
                        DataTable       dt        = ds.Tables[0];
                        BinaryFormatter bf        = new BinaryFormatter();
                        string          dbSignStr = "";
                        using (var ms = new MemoryStream())
                        {
                            foreach (DataRow row in dt.Rows)
                            {
                                if (row["id"].ToString() == autoIncrementIndexOfData)
                                {
                                    Debug.WriteLine(row["sign"].ToString());
                                    bf.Serialize(ms, row["sign"]);
                                    dbSignStr = row["sign"].ToString();
                                }
                            }
                            byte[] signObj = ms.ToArray();
                        }

                        StringBuilder sBuilder = new StringBuilder();
                        using (HMACSHA256 hmac = new HMACSHA256(key))
                        {
                            var hash = hmac.ComputeHash(StringToStream(decrypted));

                            // Create a new Stringbuilder to collect the bytes and create a string.
                            // Loop through each byte of the hashed data
                            // and format each one as a hexadecimal string.
                            for (int i = 0; i < hash.Length; i++)
                            {
                                sBuilder.Append(hash[i].ToString("x2"));
                            }
                            // Return the hexadecimal string.
                            Debug.WriteLine(sBuilder.ToString());
                        }
                        //******************* CHECK SIGN ***************************
                        if (sBuilder.ToString() == dbSignStr) //check signature
                        {                                     //SIGNATURE CHECK SUCCESSFUL
                            try
                            {
                                using (System.IO.StreamWriter file =
                                           new System.IO.StreamWriter(dwnlPath, true))
                                {
                                    await file.WriteLineAsync(Encoding.UTF8.GetChars(decryptedArr));
                                }
                            }
                            catch (Exception ex)
                            {
                                //GlobalClass.Log("ERROR @ WriteFileToPath" + ex.ToString());
                            }
                            //******************* CHECK SIGN ***************************
                        }
                    }
                    catch { }
                    finally { }

                    try
                    {
                        Stream newStream = ByteArrToStream(decryptedArr);

                        // Stretches the image to fit the pictureBox.
                        if (pictureBox1.Image == null)
                        {
                            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                        }
                        if (pictureBox1.Image != null)
                        {
                            pictureBox1.Image.Dispose();
                        }
                        using (Image MyImage = Image.FromStream(newStream))
                        {
                            pictureBox1.Image = (Image)MyImage.Clone();
                            pictureBox1.Update();
                            pictureBox1.Refresh();
                        }
                    }
                    catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
                }
                else //false
                {   //stop
                    await System.Threading.Tasks.Task.Delay(1000);

                    return(0);
                }
            }
            return(0);
        }