public void EncryptMessage(byte[] message, out byte[] encryptedBuffer) { encryptedBuffer = null; SecPkgContext_Sizes ContextSizes = new SecPkgContext_Sizes(); if (QueryContextAttributes(ref _hContext, SECPKG_ATTR_SIZES, out ContextSizes) != SEC_E_OK) { throw new Exception("QueryContextAttribute() failed!!!"); } MultipleSecBufferHelper[] ThisSecHelper = new MultipleSecBufferHelper[2]; ThisSecHelper[0] = new MultipleSecBufferHelper(message, SecBufferType.SECBUFFER_DATA); ThisSecHelper[1] = new MultipleSecBufferHelper(new byte[ContextSizes.cbSecurityTrailer], SecBufferType.SECBUFFER_TOKEN); SecBufferDesc DescBuffer = new SecBufferDesc(ThisSecHelper); try { if (EncryptMessage(ref _hContext, 0, ref DescBuffer, 0) != SEC_E_OK) { throw new Exception("EncryptMessage() failed!!!"); } encryptedBuffer = DescBuffer.GetSecBufferByteArray(); } finally { DescBuffer.Dispose(); } }
public void SignMessage(byte[] message, out byte[] signedBuffer) { signedBuffer = null; SecPkgContext_Sizes ContextSizes = new SecPkgContext_Sizes(); if (QueryContextAttributes(ref _hContext, SECPKG_ATTR_SIZES, out ContextSizes) != SEC_E_OK) { throw new Exception("QueryContextAttribute() failed!!!"); } MultipleSecBufferHelper[] ThisSecHelper = new MultipleSecBufferHelper[2]; ThisSecHelper[0] = new MultipleSecBufferHelper(message, SecBufferType.SECBUFFER_DATA); ThisSecHelper[1] = new MultipleSecBufferHelper(new byte[ContextSizes.cbMaxSignature], SecBufferType.SECBUFFER_TOKEN); SecBufferDesc DescBuffer = new SecBufferDesc(ThisSecHelper); try { if (MakeSignature(ref _hContext, 0, ref DescBuffer, 0) != SEC_E_OK) { throw new Exception("MakeSignature() failed!!!"); } //SSPIHelper.SignAndVerify(ref _hClientContext,ref hServerContext,ref DescBuffer); uint EncryptionQuality = 0; VerifySignature(ref this._hContext, ref DescBuffer, 0, out EncryptionQuality); signedBuffer = DescBuffer.GetSecBufferByteArray(); } finally { DescBuffer.Dispose(); } }
public static extern int QueryContextAttributes(ref SECURITY_HANDLE phContext, uint ulAttribute, out SecPkgContext_Sizes pContextAttributes);