예제 #1
0
        public byte[] TransformFinalBlock(byte[] input, int inputOffset, int count)
        {
            ulong ivLength = this.iv == null ? 0 : (ulong)this.iv.Length;

            uint  result;
            ulong outputSize;

            // Get the required size of the plaintext/ciphertext buffer
            if (this.isEncrypting)
            {
                result = BCryptCore.BCryptEncrypt(this.hKey, input, (ulong)count, IntPtr.Zero, this.iv, ivLength, null, 0, out outputSize, BCryptConstants.BCRYPT_BLOCK_PADDING);
                if (result != 0)
                {
                    throw new SystemException("An error was encountered while retrieving the ciphertext size.");
                }
            }
            else
            {
                result = BCryptCore.BCryptDecrypt(this.hKey, input, (ulong)count, IntPtr.Zero, this.iv, ivLength, null, 0, out outputSize, BCryptConstants.BCRYPT_BLOCK_PADDING);
                if (result != 0)
                {
                    throw new SystemException("An error was encountered while retrieving the plaintext size.");
                }
            }

            byte[] output = new byte[outputSize];

            // No padding is used with authenticatied modes
            // Bypass final block transformation and defer to regular transformation
            if (this.mode == BlockCipherMode.GCM || this.mode == BlockCipherMode.CCM)
            {
                this.TransformBlock(input, inputOffset, output, 0, count);
                return(output);
            }

            ulong pcbResult;

            if (this.isEncrypting)
            {
                result = BCryptCore.BCryptEncrypt(this.hKey, input, (ulong)count, IntPtr.Zero, this.iv, ivLength, output, (ulong)output.Length, out pcbResult, BCryptConstants.BCRYPT_BLOCK_PADDING);
                if (result != 0)
                {
                    throw new SystemException("An error was encountered during encryption.");
                }
            }
            else
            {
                result = BCryptCore.BCryptDecrypt(this.hKey, input, (ulong)count, IntPtr.Zero, this.iv, ivLength, output, (ulong)output.Length, out pcbResult, BCryptConstants.BCRYPT_BLOCK_PADDING);
                if (result != 0)
                {
                    throw new SystemException("An error was encountered during decryption.");
                }
            }

            return(output);
        }
예제 #2
0
        public void TransformBlock(byte[] input, int inputOffset, byte[] output, int outputOffset, int count)
        {
            ulong pcbResult;
            ulong ivLength = this.iv == null ? 0 : (ulong)this.iv.Length;

            if (this.isEncrypting)
            {
                uint result = BCryptCore.BCryptEncrypt(this.hKey, input, (ulong)count, IntPtr.Zero, this.iv, ivLength, output, (ulong)output.Length, out pcbResult, BCryptConstants.BCRYPT_NO_PADDING);
                if (result != 0)
                {
                    throw new SystemException("An error was encountered during encryption.");
                }
            }
            else
            {
                uint result = BCryptCore.BCryptDecrypt(this.hKey, input, (ulong)count, IntPtr.Zero, this.iv, ivLength, output, (ulong)output.Length, out pcbResult, BCryptConstants.BCRYPT_NO_PADDING);
                if (result != 0)
                {
                    throw new SystemException("An error was encountered during decryption.");
                }
            }
        }