コード例 #1
0
        public void Unwrap(int messageLength, byte[] encryptedBuffer,
                           out byte[] decryptedBuffer)
        {
            decryptedBuffer = null;

            WindowsAPI.SECURITY_HANDLE decryptionContext = _clientContextHandle;

            byte[] encryptedMessage = new byte[messageLength];
            Array.Copy(encryptedBuffer, 0, encryptedMessage, 0, messageLength);

            WindowsAPI.MultipleSecBufferHelper[] secHelper = new WindowsAPI.MultipleSecBufferHelper[2];
            secHelper[0] = new WindowsAPI.MultipleSecBufferHelper(encryptedMessage, WindowsAPI.SecBufferType.SECBUFFER_STREAM);
            secHelper[1] = new WindowsAPI.MultipleSecBufferHelper(null, WindowsAPI.SecBufferType.SECBUFFER_DATA);
            WindowsAPI.SecBufferDesc descBuffer = new WindowsAPI.SecBufferDesc(secHelper);
            try
            {
                uint encryptionQuality = 0;

                var ss = WindowsAPI.DecryptMessage(ref decryptionContext, ref descBuffer, 0, out encryptionQuality);
                if (ss != WindowsAPI.SEC_E_OK)
                {
                    throw WindowsAPI.CreateException(ss, "DecryptMessage() failed!!!");
                }

                decryptedBuffer = new byte[messageLength];
                Array.Copy(descBuffer.GetSecBufferByteArray(), 0, decryptedBuffer, 0, messageLength);
            }
            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();
            }
        }
コード例 #3
0
ファイル: SSPI.cs プロジェクト: rasmus-s/csharp-driver
        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();
            }
        }
コード例 #4
0
ファイル: SSPI.cs プロジェクト: rasmus-s/csharp-driver
        public void Unwrap(int messageLength, byte[] encryptedBuffer,
                                    out byte[] decryptedBuffer)
        {
            decryptedBuffer = null;

            WindowsAPI.SECURITY_HANDLE decryptionContext = _clientContextHandle;

            byte[] encryptedMessage = new byte[messageLength];
            Array.Copy(encryptedBuffer, 0, encryptedMessage, 0, messageLength);

            WindowsAPI.MultipleSecBufferHelper[] secHelper = new WindowsAPI.MultipleSecBufferHelper[2];
            secHelper[0] = new WindowsAPI.MultipleSecBufferHelper(encryptedMessage, WindowsAPI.SecBufferType.SECBUFFER_STREAM);
            secHelper[1] = new WindowsAPI.MultipleSecBufferHelper(null, WindowsAPI.SecBufferType.SECBUFFER_DATA);
            WindowsAPI.SecBufferDesc descBuffer = new WindowsAPI.SecBufferDesc(secHelper);
            try
            {
                uint encryptionQuality = 0;

                var ss = WindowsAPI.DecryptMessage(ref decryptionContext, ref descBuffer, 0, out encryptionQuality);
                if (ss != WindowsAPI.SEC_E_OK)
                    throw WindowsAPI.CreateException(ss, "DecryptMessage() failed!!!");

                decryptedBuffer = new byte[messageLength];
                Array.Copy(descBuffer.GetSecBufferByteArray(), 0, decryptedBuffer, 0, messageLength);
            }
            finally
            {
                descBuffer.Dispose();
            }
        }