Beispiel #1
0
        /// <summary>
        /// This takes an input file and encrypts it into the output file
        /// </summary>
        /// <param name="inFile">the file to encrypt</param>
        /// <param name="outFile">the file to write the encrypted data to</param>
        /// <param name="password">the password for use as the key</param>
        /// <param name="callback">the method to call to notify of progress</param>
        public static void EncryptFile(string inFile, string outFile, string password, CryptoProgressCallBack callback)
        {
            using (FileStream fin = File.OpenRead(inFile),
                   fout = File.OpenWrite(outFile))
            {
                long   lSize = fin.Length;            // the size of the input file for storing
                int    size  = (int)lSize;            // the size of the input file for progress
                byte[] bytes = new byte[BUFFER_SIZE]; // the buffer
                int    read  = -1;                    // the amount of bytes read from the input file
                int    value = 0;                     // the amount overall read from the input file for progress

                // generate IV and Salt
                byte[] IV   = GenerateRandomBytes(16);
                byte[] salt = GenerateRandomBytes(16);

                // create the crypting object
                SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
                sma.IV = IV;

                // write the IV and salt to the beginning of the file
                fout.Write(IV, 0, IV.Length);
                fout.Write(salt, 0, salt.Length);

                // create the hashing and crypto streams
                HashAlgorithm hasher = SHA256.Create();
                using (CryptoStream cout = new CryptoStream(fout, sma.CreateEncryptor(), CryptoStreamMode.Write),
                       chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                {
                    // write the size of the file to the output file
                    BinaryWriter bw = new BinaryWriter(cout);
                    bw.Write(lSize);

                    // write the file cryptor tag to the file
                    bw.Write(FC_TAG);

                    // read and the write the bytes to the crypto stream in BUFFER_SIZEd chunks
                    while ((read = fin.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        cout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value += read;
                        callback(0, size, value);
                        Thread.Sleep(100);
                    }
                    // flush and close the hashing object
                    chash.Flush();
                    chash.Close();

                    // read the hash
                    byte[] hash = hasher.Hash;

                    // write the hash to the end of the file
                    cout.Write(hash, 0, hash.Length);

                    // flush and close the cryptostream
                    cout.Flush();
                    cout.Close();
                }
            }
        }
        void FileSend(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker w = sender as BackgroundWorker;

            try
            {
                //using (FileStream fs = File.Open(e.Argument + "", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                //using (BufferedStream bs = new BufferedStream(fs))
                //using (StreamReader sr = new StreamReader(bs))
                //{
                //    string line;
                //    while ((line = sr.ReadLine()) != null)
                //    {

                //        Thread.Sleep(100);
                //    }
                //}
                int    i     = 1;
                string tp    = ((SenderArgs)e.Argument).rest;
                string fname = tp.Split('~')[0];
                string efile = new FileInfo(fname).Name + ".fe";
                string pwd   = tp.Split('~')[1];
                CryptoHelp.EncryptFile(fname, efile, pwd, (min, max, val) =>
                {
                    //pb.Minimum = min;
                    //pb.Maximum = max;
                    //pb.Value = val;
                    //Application.DoEvents();
                    w.ReportProgress(val, min + "~" + max + "~" + val);
                });
                i++;
                w.ReportProgress(i++, "File Encrypted...");
                Thread.Sleep(1000);
                Document dd = new Document();
                dd.Length       = new FileInfo(fname).Length;
                dd.lastModified = new FileInfo(fname).LastWriteTime;
                byte[] efiledata = File.ReadAllBytes(efile);
                dd.FileData   = efiledata;
                dd.HashMethod = ((SenderArgs)e.Argument).hme;
                string touser    = tp.Split('~')[2];
                string tousertbl = touser.Split(',')[1].Trim().Replace(' ', '_') + "_" + touser.Split(',')[0].Trim();
                string fromemp   = st.GetCurrentUserTableName();
                // i = -999;
                if (efiledata != null)
                {
                    w.ReportProgress(i++, "Sending File...");
                    SqlConnection cn = new SqlConnection(st.ConStr);
                    cn.Open();
                    SqlCommand cd = new SqlCommand
                                        ("INSERT INTO " + tousertbl + " (fid, fname, fromemp,toemp, dttime, filekey, filecontents, filesize) VALUES('" +
                                        st.generateID(tousertbl, "fid", "FL") + "','" +
                                        new FileInfo(fname).Name + "','" +
                                        fromemp + "','" +
                                        tousertbl + "','" +
                                        DateTime.Now.ToString("M-d-yyyy HH:mm:ss") + "','" +
                                        pwd + "',@fdata," +
                                        new FileInfo(efile).Length + ")", cn);

                    cd.Parameters.AddWithValue("@fdata", st.ObjectToByteArray(dd));
                    //st.IMsgBox(cd.CommandText);
                    for (int j = 1 - 500; j <= 100 - 500; j++)
                    {
                        w.ReportProgress(j, "0~100~" + (j + 500));
                        if (j + 500 == 30)
                        {
                            cd.ExecuteNonQuery();
                        }
                        if (j + 500 == 70)
                        {
                            cd.CommandText = "INSERT INTO " + fromemp + " (fid, fname, fromemp,toemp, dttime, filekey, filecontents, filesize) VALUES('" +
                                             st.generateID(fromemp, "fid", "FL") + "','" +
                                             new FileInfo(fname).Name + "','" +
                                             fromemp + "','" +
                                             tousertbl + "','" +
                                             DateTime.Now.ToString("M-d-yyyy HH:mm:ss") + "','" +
                                             pwd + "',@fdata," +
                                             new FileInfo(efile).Length + ")";
                            cd.Parameters.Clear();
                            cd.Parameters.AddWithValue("@fdata", st.ObjectToByteArray(dd));
                            cd.ExecuteNonQuery();
                        }

                        Thread.Sleep(10);
                    }
                    File.Delete(efile);
                    cn.Close();
                    //  st.IMsgBox("sdf");
                }
            }
            catch (Exception ex)
            {
                st.EMsgBox("Error Occured While Reading the File : " + ex.ToString());
            }
        }
Beispiel #3
0
        /// <summary>
        /// takes an input file and decrypts it to the output file
        /// </summary>
        /// <param name="inFile">the file to decrypt</param>
        /// <param name="outFile">the to write the decrypted data to</param>
        /// <param name="password">the password used as the key</param>
        /// <param name="callback">the method to call to notify of progress</param>
        public static void DecryptFile(string inFile, string outFile, string password, CryptoProgressCallBack callback)
        {
            // NOTE:  The encrypting algo was so much easier...

            // create and open the file streams
            using (FileStream fin = File.OpenRead(inFile),
                   fout = File.OpenWrite(outFile))
            {
                int    size     = (int)fin.Length;       // the size of the file for progress notification
                byte[] bytes    = new byte[BUFFER_SIZE]; // byte buffer
                int    read     = -1;                    // the amount of bytes read from the stream
                int    value    = 0;
                int    outValue = 0;                     // the amount of bytes written out

                // read off the IV and Salt
                byte[] IV = new byte[16];
                fin.Read(IV, 0, 16);
                byte[] salt = new byte[16];
                fin.Read(salt, 0, 16);

                // create the crypting stream
                SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
                sma.IV = IV;

                value = 32;                 // the value for the progress
                long lSize = -1;            // the size stored in the input stream

                // create the hashing object, so that we can verify the file
                HashAlgorithm hasher = SHA256.Create();

                // create the cryptostreams that will process the file
                using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read),
                       chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                {
                    // read size from file
                    BinaryReader br = new BinaryReader(cin);
                    lSize = br.ReadInt64();
                    ulong tag = br.ReadUInt64();

                    if (FC_TAG != tag)
                    {
                        throw new CryptoHelpException("File Corrupted!");
                    }

                    //determine number of reads to process on the file
                    long numReads = lSize / BUFFER_SIZE;

                    // determine what is left of the file, after numReads
                    long slack = (long)lSize % BUFFER_SIZE;

                    // read the buffer_sized chunks
                    for (int i = 0; i < numReads; ++i)
                    {
                        read = cin.Read(bytes, 0, bytes.Length);
                        fout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value    += read;
                        outValue += read;
                        callback(0, size, value);
                        Thread.Sleep(100);
                    }

                    // now read the slack
                    if (slack > 0)
                    {
                        read = cin.Read(bytes, 0, (int)slack);
                        fout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value    += read;
                        outValue += read;
                        callback(0, size, value);
                    }
                    // flush and close the hashing stream
                    chash.Flush();
                    chash.Close();

                    // flush and close the output file
                    fout.Flush();
                    fout.Close();

                    // read the current hash value
                    byte[] curHash = hasher.Hash;

                    // get and compare the current and old hash values
                    byte[] oldHash = new byte[hasher.HashSize / 8];
                    read = cin.Read(oldHash, 0, oldHash.Length);
                    if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash)))
                    {
                        throw new CryptoHelpException("File Corrupted!");
                    }
                }

                // make sure the written and stored size are equal
                if (outValue != lSize)
                {
                    throw new CryptoHelpException("File Sizes don't match!");
                }
            }
        }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(st.ConStr);

            cn.Open();
            SqlDataReader rd = new SqlCommand("SELECT * FROM " + st.GetCurrentUserTableName() + " WHERE fid='" + fd + "'", cn).ExecuteReader();

            if (rd.Read())
            {
                //download the File.
                //st.IMsgBox("Download...");
                BackgroundWorker w = new BackgroundWorker()
                {
                    WorkerReportsProgress      = true,
                    WorkerSupportsCancellation = true
                };
                w.DoWork += new DoWorkEventHandler((ss, ee) =>
                {
                    try
                    {
                        int p             = -999;
                        SqlConnection cn1 = new SqlConnection(st.ConStr);
                        cn1.Open();
                        byte[] fdata = (byte[])new SqlCommand("SELECT filecontents FROM " + st.GetCurrentUserTableName() + " WHERE fid='" + ee.Argument.ToString().Split('~')[0].Trim() + "'", cn1).ExecuteScalar();
                        if (fdata != null)
                        {
                            w.ReportProgress(p++, "Downloading to Temp...");
                            Thread.Sleep(500);
                            string fnm = ee.Argument.ToString().Split('~')[1].Trim();
                            Directory.CreateDirectory("C:\\itemp");
                            string efpath = "C:\\itemp\\" + fnm.Substring(fnm.LastIndexOf('\\') + 1) + ".fe";
                            FileStream fs = new FileStream(efpath, FileMode.Create);
                            Document dd   = (Document)st.ByteArrayToObject(fdata);
                            fs.Write(dd.FileData, 0, dd.FileData.Length);
                            fs.Flush();
                            fs.Close();
                            w.ReportProgress(p++, "File Downloaded to TEMP...");
                            Thread.Sleep(500);
                            string effinish = efpath.Remove(efpath.LastIndexOf('.'));
                            CryptoHelp.DecryptFile(efpath, effinish, ee.Argument.ToString().Split('~')[2].Trim(), (min, max, val) =>
                            {
                                w.ReportProgress(val, min + "~" + max + "~" + val);
                            });
                            w.ReportProgress(p++, "File Decrypted Successfully.");
                            Thread.Sleep(1000);
                            w.ReportProgress(p++, "Verifying the Hash Value of the File.");
                            Thread.Sleep(1000);
                            string hs = st.ComputeHash(dd.Length + dd.lastModified.ToString(), "gj", dd.HashMethod);
                            //w.ReportProgress(p++, "");

                            /*SaveFileDialog saveAs = new SaveFileDialog();
                             * saveAs.AddExtension = true;
                             * saveAs.Filter = "Files |*." + new FileInfo(efpath).Extension;
                             * if (saveAs.ShowDialog() != DialogResult.Cancel)
                             * {
                             *  string nfname = saveAs.FileName;
                             *  File.Copy(effinish, nfname);
                             *  st.IMsgBox("File Downloaded Successfully.");
                             * }*/
                            ee.Result = effinish + "~" + hs;
                        }
                        cn1.Close();
                    }
                    catch (Exception ex)
                    {
                        st.EMsgBox(ex.ToString());
                    }
                });
                w.ProgressChanged += new ProgressChangedEventHandler((sss, eee) =>
                {
                    pb.Visible   = true;
                    lbls.Visible = true;
                    string us    = eee.UserState + "";
                    if (eee.ProgressPercentage < 0)
                    {
                        lbls.Text = us;
                    }
                    else
                    {
                        pb.Minimum = Convert.ToInt32(us.Split('~')[0].Trim());
                        pb.Maximum = Convert.ToInt32(us.Split('~')[1].Trim());
                        pb.Value   = Convert.ToInt32(us.Split('~')[2].Trim());
                        lbls.Text  = "Decrypting the File...";
                    }
                });
                w.RunWorkerCompleted += new RunWorkerCompletedEventHandler((ssss, eeee) =>
                {
                    pb.Visible          = false;
                    string effinish     = eeee.Result.ToString().Split('~')[0].Trim();
                    txtDocumentKey.Text = eeee.Result.ToString().Split('~')[1].Trim();
                    if (txtFkey.Text.Trim() == txtDocumentKey.Text.Trim())
                    {
                        SaveFileDialog saveAs = new SaveFileDialog();
                        saveAs.AddExtension   = true;
                        saveAs.Filter         = "Files |*" + new FileInfo(effinish).Extension;
                        saveAs.FileName       = new FileInfo(effinish).Name;
                        lbls.Text             = "Hash Values Matched.";
                        if (saveAs.ShowDialog() != DialogResult.Cancel)
                        {
                            string nfname = saveAs.FileName;
                            File.Copy(effinish, nfname);
                            st.IMsgBox("File Downloaded Successfully.\n\n[" + nfname + "]");
                            try
                            {
                                File.Delete(effinish);
                                File.Delete(effinish + ".fe");
                                Directory.Delete("C:\\itemp");
                            }
                            catch { }
                            this.Close();
                        }
                    }
                    else
                    {
                        st.EMsgBox("The Document Was Modified / Intrusion Occured.\nThe Hash Signature Doesn't Match the File Origin.");
                        lbls.Text = "Hash Values Mismatch.";
                        try
                        {
                            File.Delete(effinish);
                            File.Delete(effinish + ".fe");
                            Directory.Delete("C:\\itemp");
                        }
                        catch { }
                        //this.Close();
                    }
                });
                btnDownload.Enabled = false;
                w.RunWorkerAsync(rd["fid"] + "~" + rd["fname"] + "~" + rd["filekey"]);
            }
            else
            {
                st.EMsgBox("Invalid File Key. Download Failed.");
            }
            cn.Close();
        }