private void ProcessStream(IStreamCipher Cipher, byte[] Input, int InOffset, byte[] Output, int OutOffset) { int blkSize = Cipher.BlockSize; long inpSize = (Input.Length - InOffset); long alnSize = (inpSize / blkSize) * blkSize; long count = 0; Cipher.IsParallel = false; while (count != alnSize) { Cipher.Transform(Input, InOffset, Output, OutOffset); InOffset += blkSize; OutOffset += blkSize; count += blkSize; } // partial if (alnSize != inpSize) { int cnkSize = (int)(inpSize - alnSize); byte[] inpBuffer = new byte[cnkSize]; Buffer.BlockCopy(Input, InOffset, inpBuffer, 0, cnkSize); byte[] outBuffer = new byte[cnkSize]; Cipher.Transform(inpBuffer, outBuffer); Buffer.BlockCopy(outBuffer, 0, Output, OutOffset, cnkSize); count += cnkSize; } }
private void StreamCipherArrayTest() { int remainder = _dataSize; while (remainder > 0) { _streamCipher.Transform(_inputBuffer, _outputBuffer); remainder -= _inputBuffer.Length; } }
private void ProcessStream(string InputPath, string OutputPath, MemoryStream Header = null) { using (BinaryReader inputReader = new BinaryReader(new FileStream(InputPath, FileMode.Open, FileAccess.Read, FileShare.None))) { int blockSize = (DCS_BLOCK * 4); long bytesRead = 0; long bytesTotal = 0; if (inputReader.BaseStream.Length < blockSize) { blockSize = (int)inputReader.BaseStream.Length; } using (BinaryWriter outputWriter = new BinaryWriter(new FileStream(OutputPath, FileMode.Create, FileAccess.Write, FileShare.None))) { byte[] inputBuffer = new byte[blockSize]; byte[] outputBuffer = new byte[blockSize]; if (Header != null) { outputWriter.Write(Header.ToArray()); } else { inputReader.BaseStream.Position = MessageHeader.GetHeaderSize; } // initialize the cipher StreamCipher.Init(new KeyParams(this.Key, this.IV)); // loop through file while ((bytesRead = inputReader.Read(inputBuffer, 0, blockSize)) == blockSize) { StreamCipher.Transform(inputBuffer, outputBuffer); outputWriter.Write(outputBuffer); bytesTotal += bytesRead; if (bytesTotal % this.ProgressInterval == 0) { CalculateProgress(bytesTotal); } } // last block if (bytesRead > 0) { outputBuffer = new byte[bytesRead]; StreamCipher.Transform(inputBuffer, outputBuffer); outputWriter.Write(outputBuffer); CalculateProgress(bytesTotal + bytesRead); } } } }
private void ProcessStream(byte[] Input, int InOffset, byte[] Output, int OutOffset, int Length) { Length += InOffset; while (InOffset < Length) { m_streamCipher.Transform(Input, InOffset, Output, OutOffset); InOffset += m_blockSize; OutOffset += m_blockSize; } }
public byte[] Encrypt(string value) { // when encrypting, you need a 'nonce' - 'number once'. By changing this on each encryption, you end up with different output each time // without it you apply identical encryption for each key which would be a vulnerability. It's like seeding a random number generator. byte[] nonce = new byte[12]; // shouldn't use this, not random enough, but its good enough for this demo new Random().NextBytes(nonce); // initialise the algorithm with the key (password) and nonce _streamCipher.SetState(_key, nonce); // convert our string to a byte array byte[] data = Encoding.UTF8.GetBytes(value); // encrypt it _streamCipher.Transform(data); // now return the nonce (first 12 bytes) and the encrypted data as one array return(nonce.Concat(data).ToArray()); }