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 DecryptMessage(int messageLength, byte[] encryptedBuffer, bool bUseClientContext, out byte[] decryptedBuffer) { decryptedBuffer = null; SECURITY_HANDLE DecryptionContext = _hContext; if (bUseClientContext) { DecryptionContext = _hContext; } byte[] EncryptedMessage = new byte[messageLength]; Array.Copy(encryptedBuffer, 0, EncryptedMessage, 0, messageLength); int SecurityTrailerLength = encryptedBuffer.Length - messageLength; byte[] SecurityTrailer = new byte[SecurityTrailerLength]; Array.Copy(encryptedBuffer, messageLength, SecurityTrailer, 0, SecurityTrailerLength); MultipleSecBufferHelper[] ThisSecHelper = new MultipleSecBufferHelper[2]; ThisSecHelper[0] = new MultipleSecBufferHelper(EncryptedMessage, SecBufferType.SECBUFFER_DATA); ThisSecHelper[1] = new MultipleSecBufferHelper(SecurityTrailer, SecBufferType.SECBUFFER_TOKEN); SecBufferDesc DescBuffer = new SecBufferDesc(ThisSecHelper); try { uint EncryptionQuality = 0; if (DecryptMessage(ref DecryptionContext, ref DescBuffer, 0, out EncryptionQuality) != SEC_E_OK) { throw new Exception("DecryptMessage() failed!!!"); } decryptedBuffer = new byte[messageLength]; Array.Copy(DescBuffer.GetSecBufferByteArray(), 0, decryptedBuffer, 0, messageLength); } finally { DescBuffer.Dispose(); } }
public void VerifyMessage(int messageLength, byte[] signedBuffer, out byte[] verifiedBuffer) { verifiedBuffer = null; byte[] SignedMessage = new byte[messageLength]; Array.Copy(signedBuffer, 0, SignedMessage, 0, messageLength); int SignatureLength = signedBuffer.Length - messageLength; byte[] Signature = new byte[SignatureLength]; Array.Copy(signedBuffer, messageLength, Signature, 0, SignatureLength); MultipleSecBufferHelper[] ThisSecHelper = new MultipleSecBufferHelper[2]; ThisSecHelper[0] = new MultipleSecBufferHelper(SignedMessage, SecBufferType.SECBUFFER_DATA); ThisSecHelper[1] = new MultipleSecBufferHelper(Signature, SecBufferType.SECBUFFER_TOKEN); SecBufferDesc DescBuffer = new SecBufferDesc(ThisSecHelper); try { uint EncryptionQuality = 0; int Return = VerifySignature(ref _hContext, ref DescBuffer, 0, out EncryptionQuality); if (Return != SEC_E_OK) { throw new Exception("VerifySignature() failed!!!"); } verifiedBuffer = new byte[messageLength]; Array.Copy(DescBuffer.GetSecBufferByteArray(), 0, verifiedBuffer, 0, messageLength); } 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(); } }