void CompareOutput(IPadding Padding) { CSPPrng rng = new CSPPrng(); byte[] fill = new byte[16]; rng.GetBytes(fill); const int BLOCK = 16; for (int i = 0; i < BLOCK; i++) { byte[] data = new byte[BLOCK]; // fill with rand if (i > 0) { Array.Copy(fill, data, BLOCK - i); } // pad array Padding.AddPadding(data, i); // verify length int len = Padding.GetPaddingLength(data); if (len == 0 && i != 0) { throw new Exception("PaddingTest: Failed the padding value return check!"); } else if (i != 0 && len != BLOCK - i) { throw new Exception("PaddingTest: Failed the padding value return check!"); } // test offset method if (i > 0 && i < 15) { len = Padding.GetPaddingLength(data, i); if (len == 0 && i != 0) { throw new Exception("PaddingTest: Failed the padding value return check!"); } else if (i != 0 && len != BLOCK - i) { throw new Exception("PaddingTest: Failed the padding value return check!"); } } } rng.Dispose(); }
private void BlockDecrypt(ICipherMode Cipher, IPadding Padding, byte[] Input, int InOffset, ref byte[] Output, int OutOffset) { int blkSize = Cipher.BlockSize; long inpSize = (Input.Length - InOffset); long alnSize = inpSize - blkSize; long count = 0; Cipher.IsParallel = false; while (count != alnSize) { Cipher.Transform(Input, InOffset, Output, OutOffset); InOffset += blkSize; OutOffset += blkSize; count += blkSize; } // last block byte[] inpBuffer = new byte[blkSize]; Buffer.BlockCopy(Input, InOffset, inpBuffer, 0, blkSize); byte[] outBuffer = new byte[blkSize]; Cipher.Transform(inpBuffer, 0, outBuffer, 0); int fnlSize = blkSize - Padding.GetPaddingLength(outBuffer, 0); Buffer.BlockCopy(outBuffer, 0, Output, OutOffset, fnlSize); OutOffset += fnlSize; if (Output.Length != OutOffset) { Array.Resize(ref Output, OutOffset); } }
private void DecryptFile(string InputPath, string OutputPath) { using (BinaryReader inputReader = new BinaryReader(new FileStream(InputPath, FileMode.Open, FileAccess.Read, FileShare.None))) { byte[] inputBuffer = new byte[this.BlockSize]; byte[] outputBuffer = new byte[this.BlockSize]; long bytesRead = 0; long bytesTotal = 0; // move past header inputReader.BaseStream.Position = MessageHeader.GetHeaderSize; using (BinaryWriter outputWriter = new BinaryWriter(new FileStream(OutputPath, FileMode.Create, FileAccess.Write, FileShare.None))) { int maxOut = (int)inputReader.BaseStream.Length - MessageHeader.GetHeaderSize; while ((bytesRead = inputReader.Read(inputBuffer, 0, this.BlockSize)) > 0) { bytesTotal += bytesRead; if (bytesTotal < maxOut) { this.Mode.Transform(inputBuffer, outputBuffer); outputWriter.Write(outputBuffer); if (bytesTotal % this.ProgressInterval == 0) { CalculateProgress(bytesTotal); } } else { this.Mode.Transform(inputBuffer, outputBuffer); int size = this.BlockSize - Padding.GetPaddingLength(outputBuffer); outputWriter.Write(outputBuffer, 0, size); CalculateProgress(bytesTotal + size); } } } } }