TransformFinalBlock() public method

public TransformFinalBlock ( byte inputBuffer, int inputOffset, int inputCount ) : byte[]
inputBuffer byte
inputOffset int
inputCount int
return byte[]
        /// <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 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);
        }
Example #3
0
		/// <summary>
		/// Encodes a Specific part of a Byte Array as Base64
		/// </summary>
		/// <param name="buffer">The Byte Array to Encode</param>
		/// <param name="offset">The offset to begin encoding</param>
		/// <param name="length">The number of bytes to encode</param>
		/// <returns></returns>
		public static String Encode(byte[] buffer, int offset, int length)
		{
			length += offset;
			ToBase64Transform x = new ToBase64Transform();
			byte[] OutBuf;
			MemoryStream ms = new MemoryStream();
			int pos=offset;
			int size = 3;
			if (length<3)
			{
				size = length;
			}
			do
			{
				OutBuf = x.TransformFinalBlock(buffer,pos,size);
				pos += size;
				if (length-pos<size)
				{
					size = length-pos;
				}
				ms.Write(OutBuf,0,OutBuf.Length);
			}while(pos<length);
			
			OutBuf = ms.ToArray();
			ms.Close();

			UTF8Encoding y = new UTF8Encoding();
			return(y.GetString(OutBuf));
		}
Example #4
0
		public void TransformFinalBlock_SmallLength () 
		{
			byte[] input = new byte [2]; // smaller than InputBlockSize
			ToBase64Transform t = new ToBase64Transform ();
			t.TransformFinalBlock (input, 0, 2);
		}
Example #5
0
		public void TransformFinalBlock_Null () 
		{
			byte[] input = new byte [3];
			ToBase64Transform t = new ToBase64Transform ();
			t.TransformFinalBlock (null, 0, 3);
		}
Example #6
0
		public void TransformFinalBlock_InputCount_Overflow () 
		{
			byte[] input = new byte [15];
			using (ICryptoTransform t = new ToBase64Transform ()) {
				t.TransformFinalBlock (input, 0, Int32.MaxValue);
			}
		}
Example #7
0
		public void TransformFinalBlock_InputCount_Negative () 
		{
			byte[] input = new byte [15];
			using (ICryptoTransform t = new ToBase64Transform ()) {
				t.TransformFinalBlock (input, 0, -1);
			}
		}
Example #8
0
		public void TransformFinalBlock_InputOffset_Overflow () 
		{
			byte[] input = new byte [15];
			using (ICryptoTransform t = new ToBase64Transform ()) {
				t.TransformFinalBlock (input, Int32.MaxValue, input.Length);
			}
		}
Example #9
0
		public void TransformFinalBlock_InputOffset_Negative () 
		{
			byte[] input = new byte [15];
			using (ICryptoTransform t = new ToBase64Transform ()) {
				t.TransformFinalBlock (input, -1, input.Length);
			}
		}
Example #10
0
		public void TransformFinalBlock_Input_Null () 
		{
			using (ICryptoTransform t = new ToBase64Transform ()) {
				t.TransformFinalBlock (null, 0, 15);
			}
		}
Example #11
0
		public void TransformFinalBlock_Dispose () 
		{
			byte[] input = new byte [3];
			ToBase64Transform t = new ToBase64Transform ();
			t.Clear ();
			t.TransformFinalBlock (input, 0, input.Length);
		}
Example #12
0
		public void TransformFinalBlock_WrongLength () 
		{
			byte[] input = new byte [6];
			ToBase64Transform t = new ToBase64Transform ();
			t.TransformFinalBlock (input, 0, 6);
		}
Example #13
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 );
	}
Example #14
0
        //ԭʼbase64����
        public static byte[] Base64Encode(byte[] source)
        {
            if ((source == null) || (source.Length == 0))
                throw new ArgumentException("source is not valid");

            ToBase64Transform tb64 = new ToBase64Transform();
            MemoryStream stm = new MemoryStream();
            int pos = 0;
            byte[] buff;

            while (pos + 3 < source.Length)
            {
                buff = tb64.TransformFinalBlock(source, pos, 3);
                stm.Write(buff, 0, buff.Length);
                pos += 3;
            }

            buff = tb64.TransformFinalBlock(source, pos, source.Length - pos);
            stm.Write(buff, 0, buff.Length);

            return stm.ToArray();
        }