Пример #1
0
        public void Wrap(byte[] message, out byte[] encryptedBuffer)
        {
            encryptedBuffer = null;

            WindowsAPI.SECURITY_HANDLE encryptionContext = _clientContextHandle;

            WindowsAPI.SecPkgContext_Sizes contextSizes = new WindowsAPI.SecPkgContext_Sizes();

            {
                var resultCode = WindowsAPI.QueryContextAttributes(ref encryptionContext, WindowsAPI.SECPKG_ATTR_SIZES, out contextSizes);
                if (resultCode != WindowsAPI.SEC_E_OK)
                {
                    throw WindowsAPI.CreateException(resultCode, "QueryContextAttribute() failed!!!");
                }
            }

            WindowsAPI.MultipleSecBufferHelper[] secHelper = new WindowsAPI.MultipleSecBufferHelper[3];
            secHelper[0] = new WindowsAPI.MultipleSecBufferHelper(contextSizes.cbSecurityTrailer, WindowsAPI.SecBufferType.SECBUFFER_TOKEN);
            secHelper[1] = new WindowsAPI.MultipleSecBufferHelper(message, WindowsAPI.SecBufferType.SECBUFFER_DATA);
            secHelper[2] = new WindowsAPI.MultipleSecBufferHelper(contextSizes.cbBlockSize, WindowsAPI.SecBufferType.SECBUFFER_PADDING);

            WindowsAPI.SecBufferDesc descBuffer = new WindowsAPI.SecBufferDesc(secHelper);

            try
            {
                var resultCode = WindowsAPI.EncryptMessage(ref encryptionContext, WindowsAPI.SECQOP_WRAP_NO_ENCRYPT, ref descBuffer, 0);
                if (resultCode != WindowsAPI.SEC_E_OK)
                {
                    throw WindowsAPI.CreateException(resultCode, "EncryptMessage() failed!!!");
                }

                encryptedBuffer = descBuffer.GetSecBufferByteArray();
            }
            finally
            {
                descBuffer.Dispose();
            }
        }
Пример #2
0
        public void Wrap(byte[] message, out byte[] encryptedBuffer)
        {
            encryptedBuffer = null;

            WindowsAPI.SECURITY_HANDLE encryptionContext = _clientContextHandle;

            WindowsAPI.SecPkgContext_Sizes contextSizes = new WindowsAPI.SecPkgContext_Sizes();

            {
                var resultCode = WindowsAPI.QueryContextAttributes(ref encryptionContext, WindowsAPI.SECPKG_ATTR_SIZES, out contextSizes);
                if (resultCode != WindowsAPI.SEC_E_OK)
                    throw WindowsAPI.CreateException(resultCode, "QueryContextAttribute() failed!!!");
            }

            WindowsAPI.MultipleSecBufferHelper[] secHelper = new WindowsAPI.MultipleSecBufferHelper[3];
            secHelper[0] = new WindowsAPI.MultipleSecBufferHelper(contextSizes.cbSecurityTrailer, WindowsAPI.SecBufferType.SECBUFFER_TOKEN);
            secHelper[1] = new WindowsAPI.MultipleSecBufferHelper(message, WindowsAPI.SecBufferType.SECBUFFER_DATA);
            secHelper[2] = new WindowsAPI.MultipleSecBufferHelper(contextSizes.cbBlockSize, WindowsAPI.SecBufferType.SECBUFFER_PADDING);

            WindowsAPI.SecBufferDesc descBuffer = new WindowsAPI.SecBufferDesc(secHelper);

            try
            {
                var resultCode = WindowsAPI.EncryptMessage(ref encryptionContext, WindowsAPI.SECQOP_WRAP_NO_ENCRYPT, ref descBuffer, 0);
                if (resultCode != WindowsAPI.SEC_E_OK)
                    throw WindowsAPI.CreateException(resultCode, "EncryptMessage() failed!!!");

                encryptedBuffer = descBuffer.GetSecBufferByteArray();
            }
            finally
            {
                descBuffer.Dispose();
            }
        }