Ejemplo n.º 1
0
        private void DecodeFileOnSeparateThread(PFFileEncoder encoder, string encryptedInputFile, string tempFileName, StatusTimer st)
        {
            TimeSpan      ts    = new TimeSpan(0, 0, _statusReportIntervalSeconds);
            PFThread      t     = new PFThread(new ParameterizedThreadStart(DecodeFileFromBase64), "DecodeFileFromBase64");
            List <object> parms = new List <object>();

            parms.Add(encoder);
            parms.Add(encryptedInputFile);
            parms.Add(tempFileName);
            parms.Add(st);
            t.ThreadDescription       = "Decode File from Base64 Text.";
            t.ShowElapsedMilliseconds = false;
            t.StartTime = DateTime.Now;
            st.ShowElapsedTimeMilliseconds = false;
            st.NumSecondsInterval          = _statusReportIntervalSeconds;
            if (st.StatusTimerIsRunning == false)
            {
                st.Start();
            }
            t.Start(parms);
            while (t.ThreadObject.Join(ts) == false)
            {
                if (st.StatusReportDue())
                {
                    FileInfo fi = new FileInfo(tempFileName);
                    currentStatusReport("DecodeEncryptionFromBase64", "InProgress", fi.Length, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                    fi = null;
                }
            }
            t.FinishTime = DateTime.Now;
        }
Ejemplo n.º 2
0
        //methods
        /// <summary>
        /// Converts file to base64 encoding.
        /// </summary>
        /// <param name="srcFile">File containing data to be encoded.</param>
        /// <param name="targetFile">Output file that will contain the base64 encoded data.</param>
        /// <param name="st">A StatusTimer object to use for timing the encode operation.</param>
        public void EncodeFileToBase64(string srcFile, string targetFile, StatusTimer st)
        {
            byte[]       bytes          = new byte[BufferSize];
            FileStream   fs             = null;
            StreamWriter sw             = null;
            FileStream   fsOut          = null;
            int          bytesRead      = 0;
            long         totalBytesRead = 0;
            bool         errorOccurred  = false;

            // Open srcFile in read-only mode.
            try
            {
                if (st == null)
                {
                    throw new ArgumentNullException("StatusTime must be specified for this routine.");
                }
                st.NumSecondsInterval = _statusReportIntervalSeconds;

                fs = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize);
                long sourceSize = new FileInfo(srcFile).Length;

                if (sourceSize <= BufferSize)
                {
                    // Open stream writer
                    sw        = new StreamWriter(targetFile, false, Encoding.ASCII);
                    bytesRead = fs.Read(bytes, 0, BufferSize);

                    if (bytesRead > 0)
                    {
                        if (currentStatusReport != null)
                        {
                            if (st.StatusReportDue())
                            {
                                currentStatusReport("EncodeToBase64", "In Progress", totalBytesRead, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                            }
                        }
                        string base64String = Convert.ToBase64String(bytes, 0, bytesRead);
                        totalBytesRead += bytesRead;
                        sw.Write(base64String);
                        if (currentStatusReport != null)
                        {
                            if (st.StatusReportDue())
                            {
                                currentStatusReport("EncodeToBase64", "Completed", totalBytesRead, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                            }
                        }
                    }
                }
                else
                {
                    // Instantiate a ToBase64Transform object.
                    ToBase64Transform transf = new ToBase64Transform();
                    // Arrays to hold input and output bytes.
                    byte[] inputBytes  = new byte[transf.InputBlockSize];
                    byte[] outputBytes = new byte[transf.OutputBlockSize];
                    int    bytesWritten;

                    fsOut = new FileStream(targetFile, FileMode.Create, FileAccess.Write);

                    do
                    {
                        if (currentStatusReport != null)
                        {
                            if (st.StatusReportDue())
                            {
                                currentStatusReport("EncodeToBase64", "In Progress", totalBytesRead, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                            }
                        }
                        bytesRead       = fs.Read(inputBytes, 0, inputBytes.Length);
                        totalBytesRead += bytesRead;
                        bytesWritten    = transf.TransformBlock(inputBytes, 0, bytesRead, outputBytes, 0);
                        fsOut.Write(outputBytes, 0, bytesWritten);
                    } while (sourceSize - totalBytesRead > transf.InputBlockSize);

                    // Transform the final block of data.
                    bytesRead       = fs.Read(inputBytes, 0, inputBytes.Length);
                    totalBytesRead += bytesRead;
                    byte[] finalOutputBytes = transf.TransformFinalBlock(inputBytes, 0, bytesRead);
                    fsOut.Write(finalOutputBytes, 0, finalOutputBytes.Length);

                    if (currentStatusReport != null)
                    {
                        currentStatusReport("EncodeToBase64", "Completed", totalBytesRead, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                    }

                    // Clear Base64Transform object.
                    transf.Clear();
                }
            }
            catch (IOException ex)
            {
                errorOccurred = true;
                _msg.Length   = 0;
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new IOException(_msg.ToString());
            }
            catch (SecurityException ex)
            {
                errorOccurred = true;
                _msg.Length   = 0;
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new SecurityException(_msg.ToString());
            }
            catch (UnauthorizedAccessException ex)
            {
                errorOccurred = true;
                _msg.Length   = 0;
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new UnauthorizedAccessException(_msg.ToString());
            }
            finally
            {
                if (errorOccurred)
                {
                    if (currentStatusReport != null)
                    {
                        currentStatusReport("EncodeToBase64", "ErrorCancel", totalBytesRead, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                    }
                }

                if (sw != null)
                {
                    sw.Close();
                }
                if (fs != null)
                {
                    fs.Dispose();
                    fs.Close();
                }
                if (fsOut != null)
                {
                    fsOut.Dispose();
                    fsOut.Close();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///  Decrypts using a byte array. The encrypted input file is loaded into a stream and then decrypted and saved as unencrypted data to the output file.
        ///  Method uses techniques to handle cases where file being decrypted is very large.
        ///  This helps deal with very large files that can cause out of memory exceptions during decryption process.
        /// </summary>
        /// <param name="encryptedInputFile">Full path to file containing the encrypted data.</param>
        /// <param name="outputFile">Full path to file that will contain decrypted data.</param>
        /// <param name="st">A StatusTimer object to use for timing the decryption.</param>
        /// <returns>Returns path to output file.</returns>
        public string DecryptBinary(string encryptedInputFile, string outputFile, StatusTimer st)
        {
            if (st == null)
            {
                throw new ArgumentNullException("StatusTime must be specified for this routine.");
            }
            if (String.IsNullOrEmpty(encryptedInputFile) || String.IsNullOrEmpty(outputFile))
            {
                throw new ArgumentNullException("Paths to both the encrypted input file and the output file need to be specified.");
            }

            if (KeyIsValid(GetStringFromByteArray(_key)) == false)
            {
                throw new System.Exception("Invalid length for Key.");
            }
            if (IVIsValid(GetStringFromByteArray(_iv)) == false)
            {
                throw new System.Exception("Invalid length for IV.");
            }


            FileStream   fsInput      = null;
            FileStream   fsEncrypted  = null;
            CryptoStream cryptoStream = null;

            try
            {
                fsInput      = new FileStream(encryptedInputFile, FileMode.Open, FileAccess.Read);
                fsEncrypted  = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
                cryptoStream = new CryptoStream(fsEncrypted, _cryptoProvider.CreateDecryptor(_key, _iv), CryptoStreamMode.Write);

                int    bufferLength  = _bufferLength;
                byte[] buffer        = new byte[bufferLength];
                int    contentLength = 0;
                long   totalBytes    = 0;

                contentLength = fsInput.Read(buffer, 0, bufferLength);
                while (contentLength != 0)
                {
                    cryptoStream.Write(buffer, 0, contentLength);
                    cryptoStream.Flush();
                    contentLength = fsInput.Read(buffer, 0, bufferLength);
                    totalBytes   += contentLength;
                    if (currentStatusReport != null)
                    {
                        if (st.StatusReportDue())
                        {
                            currentStatusReport("Decryption", "In Progress", totalBytes, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                        }
                    }
                }
                if (currentStatusReport != null)
                {
                    currentStatusReport("Decryption", "Completed", totalBytes, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime());
                }
                cryptoStream.FlushFinalBlock();
                cryptoStream.Close();
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Attempt to decrypt file ");
                _msg.Append(encryptedInputFile);
                _msg.Append(" failed. Verify you are using same key/iv pair used to encrypt.  Error message: ");
                _msg.Append("\r\n");
                _msg.Append(AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                if (fsInput != null)
                {
                    fsInput.Close();
                }
                if (fsEncrypted != null)
                {
                    fsEncrypted.Close();
                }
            }


            return(outputFile);
        }
        //WARNING: Status timer can slow down processing significantly if processing is in a tight loop.
        public void StatusTimerTest(MainForm frm)
        {
            StatusTimer timer          = new StatusTimer();
            long        loopMax        = 100000000;
            int         statusInterval = 5;

            try
            {
                _msg.Length = 0;
                _msg.Append("StatusTimerTest started ...\r\n");
                WriteMessageToLog(_msg.ToString());

                timer.NumSecondsInterval = statusInterval;
                timer.Start();  //report status every two seconds

                _msg.Length = 0;
                _msg.Append("\r\nLooping ");
                _msg.Append(loopMax.ToString("#,##0"));
                _msg.Append(" times... \r\n");
                WriteMessageToLog(_msg.ToString());

                for (long num = 1; num <= loopMax; num++)
                {
                    if (timer.StatusReportDue())
                    {
                        _msg.Length = 0;
                        _msg.Append("Loop count = ");
                        _msg.Append(num.ToString("#,##0"));
                        _msg.Append(" Elapsed time to this point: ");
                        _msg.Append(timer.GetFormattedElapsedTime());
                        WriteMessageToLog(_msg.ToString());
                    }
                }
                _msg.Length = 0;
                _msg.Append("Loop count = ");
                _msg.Append(loopMax.ToString("#,##0"));
                _msg.Append(" Elapsed time to this point: ");
                _msg.Append(timer.GetFormattedElapsedTime());
                WriteMessageToLog(_msg.ToString());


                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append(Environment.NewLine);
                _msg.Append("Total Elapsed Time:   ");
                _msg.Append(timer.GetFormattedElapsedTime());
                _msg.Append(Environment.NewLine);
                _msg.Append("Elapsed milliseconds: ");
                _msg.Append(timer.GetElapsedTime().TotalMilliseconds.ToString("#,##0"));
                _msg.Append(Environment.NewLine);
                WriteMessageToLog(_msg.ToString());

                timer.Stop();
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                WriteMessageToLog(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... StatusTimerTest finished.");
                WriteMessageToLog(_msg.ToString());
            }
        }