public static byte[] Transform(ICryptoTransform x, byte[] c) { if (x is RijndaelManagedTransform) { var t = (RijndaelManagedTransform)x; t.Reset(); } byte[] ic; var j = 0; var i = 0; var rst = c.Length % x.InputBlockSize; var count = Math.Floor((double)(c.Length / x.InputBlockSize)); var tcount = count + (rst == 0 ? 0 : 1); //c = Expand(c, (int)(x.InputBlockSize * tcount)); ic = new byte[(int)(tcount * x.OutputBlockSize)]; var g = x.GetType().GetInterfaces(); var jtot = (int)(count * x.OutputBlockSize); //x.TransformBlock(c, 0, c.Length, ic, 0); //var xcc = x.TransformBlock(c, 0, c.Length, ic, 0); for (int cnt = 0; j < jtot; cnt++) { x.TransformBlock(c, 0, c.Length, ic, 0); var v = x.TransformBlock(c, i, x.InputBlockSize, ic, j); j += v; i += x.InputBlockSize; //var ss = Encoding.UTF8.GetString(ic, 0, j); } if (rst != 0) { var v = x.TransformFinalBlock(c, i, rst); Array.Copy(v, 0, ic, j, v.Length); j += x.OutputBlockSize; i += rst; } return(ic); }
/// <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; } }