public TransformBlock ( byte inputBuffer, int inputOffset, int inputCount, byte outputBuffer, int outputOffset ) : int | ||
inputBuffer | byte | |
inputOffset | int | |
inputCount | int | |
outputBuffer | byte | |
outputOffset | int | |
return | int |
public override void ExecuteResult(ControllerContext context) { var responsetOutStream = context.HttpContext.Response.OutputStream; var base64Transformer = new ToBase64Transform(); var inputSize = base64Transformer.InputBlockSize; var outputSize = base64Transformer.OutputBlockSize; var outputBuffer = new byte[outputSize]; var inputBuffer = new byte[inputSize]; int eof = _stream.ReadByte(), bytesRead; inputBuffer[0] = (byte)eof; while ((bytesRead = _stream.Read(inputBuffer, 1, inputBuffer.Length - 1)) != 0) { if ((eof = _stream.ReadByte()) == -1) break; base64Transformer.TransformBlock(inputBuffer, 0, bytesRead + 1, outputBuffer, 0); responsetOutStream.Write(outputBuffer, 0, outputBuffer.Length); inputBuffer[0] = (byte)eof; } bytesRead++; var finalBlock = base64Transformer.TransformFinalBlock(inputBuffer, 0, bytesRead); responsetOutStream.Write(finalBlock, 0, finalBlock.Length); }
/// <summary> /// Converts byte-array to Base64 string. /// </summary> /// <param name="inputBytes">Byte array to convert.</param> /// <returns>A Base64 string representing the input byte-array.</returns> static public string ToBase64String(byte[] inputBytes) { StringBuilder sb = new StringBuilder(); ToBase64Transform base64Transform = new ToBase64Transform(); byte[] outputBytes = new byte[base64Transform.OutputBlockSize]; // Initializie the offset size. int inputOffset = 0; // Iterate through inputBytes transforming by blockSize. int inputBlockSize = base64Transform.InputBlockSize; while ((inputBytes.Length - inputOffset) > inputBlockSize) { base64Transform.TransformBlock( inputBytes, inputOffset, inputBlockSize, outputBytes, 0); inputOffset += base64Transform.InputBlockSize; sb.Append(Encoding.UTF8.GetString( outputBytes, 0, base64Transform.OutputBlockSize)); } // Transform the final block of data. outputBytes = base64Transform.TransformFinalBlock( inputBytes, inputOffset, (inputBytes.Length - inputOffset)); sb.Append(Encoding.UTF8.GetString(outputBytes, 0, outputBytes.Length)); return sb.ToString(); }
public void TransformBlock_Dispose () { byte[] input = new byte [3]; byte[] output = new byte [4]; ToBase64Transform t = new ToBase64Transform (); t.Clear (); t.TransformBlock (input, 0, input.Length, output, 0); }
// reads bytes from a stream and writes the encoded // as base64 encoded characters. ( 60 chars on each row) public void EncodeStream( Stream ins , Stream outs ) { if( ( ins == null ) || ( outs == null ) ) throw new ArgumentNullException( "The input and output streams may not " + "be null."); ICryptoTransform base64 = new ToBase64Transform(); // the buffers byte[] plainText = new byte[ base64.InputBlockSize ]; byte[] cipherText = new byte[ base64.OutputBlockSize ]; int readLength = 0; int count = 0; byte[] newln = new byte[] { 13 , 10 }; //CR LF with mail // read through the stream until there // are no more bytes left while( true ) { // read some bytes readLength = ins.Read( plainText , 0 , plainText.Length ); // break when there is no more data if( readLength < 1 ) break; // transfrom and write the blocks. If the block size // is less than the InputBlockSize then write the final block if( readLength == plainText.Length ) { base64.TransformBlock( plainText , 0 , plainText.Length , cipherText , 0 ); // write the data outs.Write( cipherText , 0 , cipherText.Length ); // do this to output lines that // are 60 chars long count += cipherText.Length; if( count == 60 ) { outs.Write( newln , 0 , newln.Length ); count = 0; } } else { // convert the final blocks of bytes and write them cipherText = base64.TransformFinalBlock( plainText , 0 , readLength ); outs.Write( cipherText , 0 , cipherText.Length ); } } outs.Write( newln , 0 , newln.Length ); }
public void TransformBlock_WrongLength () { byte[] input = new byte [6]; byte[] output = new byte [8]; ToBase64Transform t = new ToBase64Transform (); t.TransformBlock (input, 0, 6, output, 0); // note only the first block has been processed Assert.AreEqual ("41-41-41-41-00-00-00-00", BitConverter.ToString (output)); }
public void TransformBlock_NullOutput () { byte[] input = new byte [3]; ToBase64Transform t = new ToBase64Transform (); t.TransformBlock (input, 0, 3, null, 0); }
public void TransformBlock_NullInput () { byte[] output = new byte [4]; ToBase64Transform t = new ToBase64Transform (); t.TransformBlock (null, 0, 0, output, 0); }
public void TransformBlock_OutputOffset_Overflow () { byte[] input = new byte [15]; byte[] output = new byte [16]; using (ICryptoTransform t = new ToBase64Transform ()) { t.TransformBlock (input, 0, input.Length, output, Int32.MaxValue); } }
public void TransformBlock_OutputOffset_Negative () { byte[] input = new byte [15]; byte[] output = new byte [16]; using (ICryptoTransform t = new ToBase64Transform ()) { t.TransformBlock (input, 0, input.Length, output, -1); } }
public void TransformBlock_InputCount_Overflow () { byte[] input = new byte [15]; byte[] output = new byte [16]; using (ICryptoTransform t = new ToBase64Transform ()) { t.TransformBlock (input, 0, Int32.MaxValue, output, 0); } }
public void TransformBlock_InputCount_Negative () { byte[] input = new byte [15]; byte[] output = new byte [16]; using (ICryptoTransform t = new ToBase64Transform ()) { t.TransformBlock (input, 0, -1, output, 0); } }