예제 #1
0
        static byte[] AddDataToFile(uint id, byte[] data)
        {
            uint timestemp = uint.Parse(DateTime.Now.ToString("yyyyMMdd"));

            byte[] result = BlindNetUtil.MergeArray(BitConverter.GetBytes(timestemp), data);
            result = BlindNetUtil.MergeArray(BitConverter.GetBytes(id), result);
            return(result);
        }
예제 #2
0
        public async Task <bool> DownloadDir(ListViewItem item)
        {
            form.label_fName.Text = "(압축중)" + form.selected.FullPath + "\\" + item.Text;
            form.label_fName.Update();

            uint id = (uint)item.Tag;

            socket.CryptoSend(BitConverter.GetBytes(id), PacketType.DocDirDownload);

            BlindPacket packet = await Task.Run(() => { return(socket.CryptoReceive()); });

            if (packet.header == PacketType.Fail)
            {
                return(false);
            }

            form.label_fName.Text = form.selected.FullPath + "\\" + item.Text;
            form.label_fName.Update();

            string fileName = dPath + item.Text + ".zip";

            byte[] data     = null;
            bool   recvMode = false;

            do
            {
                packet = socket.CryptoReceive(recvMode);
                if (packet.header == PacketType.Disconnect)
                {
                    return(false);
                }
                data = BlindNetUtil.MergeArray <byte>(data, packet.data);
                form.progressBar.PerformStep();
                if (form.progressBar.Value < form.progressBar.Maximum)
                {
                    form.progressBar.Value += 1;
                }
                form.progressBar.Value -= 1;
                form.label_percent.Text = (form.progressBar.Value * 100 / form.progressBar.Maximum) + "%";
                form.label_percent.Update();
                recvMode = true;
            } while (packet.header == PacketType.Sending);
            data = BlindNetUtil.ByteTrimEndNull(data);

            FileInfo file = new FileInfo(fileName);
            int      tmp  = 1;

            while (file.Exists)
            {
                file = new FileInfo(file.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(fileName) + "(" + tmp++ + ")" + file.Extension);
            }
            FileStream fs = file.OpenWrite();

            fs.Write(data, 0, data.Length);
            fs.Close();
            return(true);
        }
예제 #3
0
        private byte[] EncryptFile(byte[] data)
        {
            string           command = "SELECT _key, iv FROM crypto_key WHERE apply_date <= NOW() AND expire_date > NOW();";
            MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection);
            DataSet          dataset = new DataSet();

            adapter.Fill(dataset);
            if (dataset.Tables[0].Rows.Count != 1)
            {
                return(null);
            }

            Cryptography.AES256 aes256 = new Cryptography.AES256((byte[])(dataset.Tables[0].Rows[0]["_key"]), (byte[])(dataset.Tables[0].Rows[0]["iv"]));
            try
            {
                uint timestemp = uint.Parse(DateTime.Now.ToString("yyyyMMdd"));
                return(BlindNetUtil.MergeArray(BitConverter.GetBytes(timestemp), aes256.Encryption(data)));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
예제 #4
0
        private void FileUpload(Directory_Info dir)
        {
            File_Info file = BlindNetUtil.ByteToStruct <File_Info>(socket.CryptoReceiveMsg());

            Debug.WriteLine("Start FileUpload \"{0}\"", file.name);
            MySqlCommand commander = null;

            try
            {
                string           command = "SELECT path FROM files_info WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';";
                MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection);
                DataSet          dataset = new DataSet();
                adapter.Fill(dataset);
                string path = null;

                if (dataset.Tables[0].Rows.Count != 0)
                {
                    command   = "UPDATE files_info SET modified_date = NOW() WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';";
                    commander = new MySqlCommand(command, connection);
                    if (commander.ExecuteNonQuery() != 1)
                    {
                        throw new Exception();
                    }

                    path = (string)dataset.Tables[0].Rows[0]["path"];
                    File.Delete(path);
                }
                else
                {
                    command = "INSERT INTO files_info VALUES (" + 0 + ", " + dir.id + ", '" + file.name + "', DEFAULT, UPPER('" + file.type + "'), " +
                              file.size + ", NULL);";
                    commander = new MySqlCommand(command, connection);
                    if (commander.ExecuteNonQuery() != 1)
                    {
                        throw new Exception();
                    }
                }

                Debug.WriteLine("[FileUpload] Start leceiving");
                byte[] data = socket.CryptoReceiveMsg();
                Debug.WriteLine("[FileUpload] End leceiving {0} bytes", data.Length);

                command   = "SELECT MAX(id) FROM files_info;";
                commander = new MySqlCommand(command, connection);
                MySqlDataReader reader = commander.ExecuteReader();
                reader.Read();
                file.id = (uint)reader["MAX(id)"];
                reader.Close();

                if (path == null)
                {
                    command = "SELECT path FROM directorys_info WHERE id = " + dir.id + ";";
                    adapter = new MySqlDataAdapter(command, connection);
                    adapter.Fill(dataset);
                    if (dataset.Tables[0].Rows.Count != 1)
                    {
                        throw new Exception();
                    }

                    path      = (string)dataset.Tables[0].Rows[0]["path"] + file.id + ".blind";
                    command   = "UPDATE files_info SET path = '" + RemakePath(path, false) + "' WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';";
                    commander = new MySqlCommand(command, connection);
                    if (commander.ExecuteNonQuery() != 1)
                    {
                        throw new Exception();
                    }
                }

                data = EncryptFile(data);
                if (data == null)
                {
                    throw new Exception();
                }
                data = BlindNetUtil.MergeArray(BitConverter.GetBytes(file.id), data);

                FileInfo   fi = new FileInfo(path);
                FileStream fs = fi.OpenWrite();
                fs.Write(data, 0, data.Length);
                fs.Close();

                UpdateModDate(dir.id);
                socket.CryptoSend(null, PacketType.OK);
                logger.Log(LogRank.INFO, "Uploaded file(" + file.id + ")");
            }
            catch (Exception ex)
            {
                socket.CryptoSend(null, PacketType.Fail);
            }
        }