public static int VerifySignature(ref SecurityHandle context, int QOP, SecurityBufferClass[] input, int sequenceNumber)
        {
            GCHandle[] gcHandles = SSPIWrapper.PinBuffers(input);
            int        result;

            try
            {
                SecurityBufferDescriptor securityBufferDescriptor = new SecurityBufferDescriptor(input);
                try
                {
                    int num = UnsafeNclNativeMethods.NativeNTSSPI.VerifySignature(ref context, ref securityBufferDescriptor, sequenceNumber, QOP);
                    SecurityBufferClass[] array = securityBufferDescriptor.marshall();
                    for (int i = 0; i < input.Length; i++)
                    {
                        input[i] = array[i];
                    }
                    result = num;
                }
                finally
                {
                    securityBufferDescriptor.FreeAllBuffers(0);
                }
            }
            finally
            {
                SSPIWrapper.FreeGCHandles(gcHandles);
            }
            return(result);
        }
        public static int DecryptMessage(ref SecurityHandle context, SecurityBufferClass[] input, int sequenceNumber)
        {
            int num = 0;

            GCHandle[] gcHandles = SSPIWrapper.PinBuffers(input);
            int        result;

            try
            {
                SecurityBufferDescriptor securityBufferDescriptor = new SecurityBufferDescriptor(input);
                try
                {
                    int num2 = UnsafeNclNativeMethods.NativeNTSSPI.DecryptMessage(ref context, ref securityBufferDescriptor, sequenceNumber, ref num);
                    if (num2 == 0)
                    {
                        SecurityBufferClass[] array = securityBufferDescriptor.marshall();
                        for (int i = 0; i < input.Length; i++)
                        {
                            input[i] = array[i];
                        }
                    }
                    result = num2;
                }
                finally
                {
                    securityBufferDescriptor.FreeAllBuffers(0);
                }
            }
            finally
            {
                SSPIWrapper.FreeGCHandles(gcHandles);
            }
            return(result);
        }
        private static int InitializeSecurityContext(ref SecurityHandle credential, ref SecurityHandle context, string targetName, int requirements, Endianness datarep, SecurityBufferClass[] inputBuffers, ref SecurityHandle newContext, SecurityBufferClass[] outputBuffers, ref int attributes, ref long timestamp)
        {
            GCHandle[] array  = null;
            GCHandle[] array2 = null;
            int        num    = 0;

            try
            {
                if (outputBuffers != null)
                {
                    array = SSPIWrapper.PinBuffers(outputBuffers);
                }
                SecurityBufferDescriptor securityBufferDescriptor = new SecurityBufferDescriptor(outputBuffers);
                try
                {
                    if (inputBuffers == null)
                    {
                        num = UnsafeNclNativeMethods.NativeNTSSPI.InitializeSecurityContextW(ref credential, IntPtr.Zero, targetName, requirements, 0, (int)datarep, IntPtr.Zero, 0, ref newContext, ref securityBufferDescriptor, ref attributes, ref timestamp);
                    }
                    else
                    {
                        array2 = SSPIWrapper.PinBuffers(inputBuffers);
                        SecurityBufferDescriptor securityBufferDescriptor2 = new SecurityBufferDescriptor(inputBuffers);
                        try
                        {
                            num = UnsafeNclNativeMethods.NativeNTSSPI.InitializeSecurityContextW(ref credential, ref context, targetName, requirements, 0, (int)datarep, ref securityBufferDescriptor2, 0, ref newContext, ref securityBufferDescriptor, ref attributes, ref timestamp);
                        }
                        finally
                        {
                            securityBufferDescriptor2.FreeAllBuffers(0);
                        }
                    }
                    if (num == 0 || num == 590610)
                    {
                        SecurityBufferClass[] array3 = securityBufferDescriptor.marshall();
                        for (int i = 0; i < outputBuffers.Length; i++)
                        {
                            outputBuffers[i] = array3[i];
                        }
                    }
                }
                finally
                {
                    securityBufferDescriptor.FreeAllBuffers(requirements);
                }
            }
            finally
            {
                if (array != null)
                {
                    SSPIWrapper.FreeGCHandles(array);
                }
                if (array2 != null)
                {
                    SSPIWrapper.FreeGCHandles(array2);
                }
            }
            return(num);
        }