コード例 #1
0
        /// <summary>
        /// The transformation function used by all derived classes
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <param name="transformer"></param>
        protected void InternalTransformFile([NotNull] string inputFile, [NotNull] string outputFile, [NotNull] ICryptoTransform transformer)
        {
            FileStream outFileStream = File.Create(outputFile);

            using (var cs = new CryptoStream(outFileStream, transformer, CryptoStreamMode.Write))
                using (var inFile = new BinaryReader(File.OpenRead(inputFile)))
                {
                    try
                    {
                        inFile.BaseStream.CopyTo(cs, this.BufferSize);
                    }
                    catch (ArgumentException e)
                    {
                        InternalDebug.WriteToDiagnosticsFile(e.Message);
                        throw;
                    }
                }
        }
コード例 #2
0
        /// <summary>
        /// The transformation function used by all derived classes
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <param name="transformer"></param>
        protected void TransformFile(string inputFile, string outputFile, ICryptoTransform transformer)
        {
            // Any cryptographic exception indicates the data is invalid or an incorrect password has been inputted
            try
            {
#if DEBUG
                // Debug values
                if (!Stopwatch.IsHighResolution)
                {
                    throw new Exception("You don't have a high-res sysclock");
                }

                Console.WriteLine(_memoryConst);

                Stopwatch watch = Stopwatch.StartNew();

                var iterations               = 0L;
                var fullIterationTime        = 0.0D;
                var avgIterationMilliseconds = 0D;
#endif

                // Creates the streams necessary for reading and writing data
                FileStream outFileStream = File.Create(outputFile);
                using (var cs = new CryptoStream(outFileStream, transformer, CryptoStreamMode.Write))
                    using (var inFile = new BinaryReader(File.OpenRead(inputFile)))
                    // BinaryReader is not a stream, but it's only argument is one
                    {
                        // Continuously reads the stream until it hits an EndOfStream exception
                        while (true)
                        {
#if DEBUG
                            double offset = watch.Elapsed.TotalMilliseconds;
#endif
                            // Read as many bytes as we allow into the array from the file
                            byte[] data = inFile.ReadBytes(_memoryConst);

                            // Write it through the cryptostream so it is transformed
                            cs.Write(data, 0, data.Length);

                            // Break if
                            if (data.Length < _memoryConst)
                            {
                                break;
                            }
#if DEBUG
                            // Debug values
                            double perIterationMilliseconds = watch.Elapsed.TotalMilliseconds - offset;
                            avgIterationMilliseconds =
                                (avgIterationMilliseconds * iterations + perIterationMilliseconds) /
                                (iterations + 1);
                            fullIterationTime += perIterationMilliseconds;
                            iterations++;
#endif
                        }
#if DEBUG
                        // Finalize and write debug values
                        double totalMilliseconds     = watch.Elapsed.TotalMilliseconds;
                        double totalSeconds          = totalMilliseconds / 1000;
                        double perIterationSeconds   = avgIterationMilliseconds / 1000,
                               iterationMilliseconds = avgIterationMilliseconds;
                        string[] toWrite             =
                        {
                            $"Transformation type: " + transformer.GetType(),
                            "Time to transform (s):" + totalSeconds,
                            "Time to transform (ms):" + totalMilliseconds,
                            "Average iteration length (s):" + perIterationSeconds.ToString("0." + new string('#', 339)),
                            "Average iteration length (ms):" +
                            iterationMilliseconds.ToString("0." + new string('#',                                 339)),
                            "Time of all iterations, combined (s):" + fullIterationTime / 1000,
                            "Time of all iterations, combined (ms):" + fullIterationTime,
                            "Iterations:" + iterations
                        };

                        InternalDebug.WriteToDiagnosticsFile(toWrite);
#endif
                    }
            }
            catch (CryptographicException) // If something went wrong, we get it here
            {
                SymmetricAlgorithm.Dispose();
                throw;
            }
        }