TransformFinalBlock() public method

Not implemented.
public TransformFinalBlock ( byte inputBuffer, int inputOffset, int inputCount ) : byte[]
inputBuffer byte
inputOffset int
inputCount int
return byte[]
コード例 #1
0
        /// <summary>
        /// Reads a sequence of bytes from the current CryptoStream into buffer,
        /// and advances the position within the stream by the number of bytes read.
        /// </summary>
        public override int Read(byte[] outBuffer, int offset, int count)
        {
            int nBytes = 0;

            while (nBytes < count)
            {
                // Calculate buffer quantities vs read-ahead size, and check for sufficient free space
                int byteCount = _slideBufFreePos - _slideBufStartPos;

                // Need to handle final block and Auth Code specially, but don't know total data length.
                // Maintain a read-ahead equal to the length of (crypto block + Auth Code).
                // When that runs out we can detect these final sections.
                int lengthToRead = _blockAndAuth - byteCount;
                if (_slideBuffer.Length - _slideBufFreePos < lengthToRead)
                {
                    // Shift the data to the beginning of the buffer
                    int iTo = 0;
                    for (int iFrom = _slideBufStartPos; iFrom < _slideBufFreePos; iFrom++, iTo++)
                    {
                        _slideBuffer[iTo] = _slideBuffer[iFrom];
                    }
                    _slideBufFreePos -= _slideBufStartPos;                              // Note the -=
                    _slideBufStartPos = 0;
                }
                int obtained = _stream.Read(_slideBuffer, _slideBufFreePos, lengthToRead);
                _slideBufFreePos += obtained;

                // Recalculate how much data we now have
                byteCount = _slideBufFreePos - _slideBufStartPos;
                if (byteCount >= _blockAndAuth)
                {
                    // At least a 16 byte block and an auth code remains.
                    _transform.TransformBlock(_slideBuffer,
                                              _slideBufStartPos,
                                              CRYPTO_BLOCK_SIZE,
                                              outBuffer,
                                              offset);
                    nBytes            += CRYPTO_BLOCK_SIZE;
                    offset            += CRYPTO_BLOCK_SIZE;
                    _slideBufStartPos += CRYPTO_BLOCK_SIZE;
                }
                else
                {
                    // Last round.
                    if (byteCount > AUTH_CODE_LENGTH)
                    {
                        // At least one byte of data plus auth code
                        int    finalBlock = byteCount - AUTH_CODE_LENGTH;
                        byte[] fb         = _transform.TransformFinalBlock(_slideBuffer,
                                                                           _slideBufStartPos,
                                                                           finalBlock);
                        Array.Copy(fb, 0, outBuffer, offset, fb.Length);

                        nBytes            += finalBlock;
                        _slideBufStartPos += finalBlock;
                    }
                    else if (byteCount < AUTH_CODE_LENGTH)
                    {
                        throw new Exception("Internal error missed auth code");                         // Coding bug
                    }
                    // Final block done. Check Auth code.
                    byte[] calcAuthCode = _transform.GetAuthCode();
                    for (int i = 0; i < AUTH_CODE_LENGTH; i++)
                    {
                        if (calcAuthCode[i] != _slideBuffer[_slideBufStartPos + i])
                        {
                            throw new Exception("AES Authentication Code does not match. This is a super-CRC check on the data in the file after compression and encryption. \r\n"
                                                + "The file may be damaged.");
                        }
                    }

                    break;                      // Reached the auth code
                }
            }
            return(nBytes);
        }