大网数据广告活动
账户与安全

EncryptMessage (Digest) function (Sspi.h)

· 52CE 我爱测

Encrypts a message to provide privacy by using Digest.

The EncryptMessage (Digest) function encrypts a message to provide privacy. EncryptMessage (Digest) allows the application to choose among cryptographic algorithms supported by the chosen mechanism. The EncryptMessage (Digest) function uses the security context referenced by the context handle. Some packages do not have messages to be encrypted or decrypted but rather provide an integrity hash that can be checked.

This function is available as a SASL mechanism only.

Note:
EncryptMessage (Digest) and DecryptMessage (Digest) can be called at the same time from two different threads in a single security support provider interface (SSPI) context if one thread is encrypting and the other is decrypting. If more than one thread is encrypting, or more than one thread is decrypting, each thread should obtain a unique context.

Syntax

SECURITY_STATUS SEC_ENTRY EncryptMessage(
  PCtxtHandle    phContext,
  unsigned long  fQOP,
  PSecBufferDesc pMessage,
  unsigned long  MessageSeqNo
);

Parameters

phContext [in]

A handle to the security context to be used to encrypt the message.

fQOP [in]

Package-specific flags that indicate the quality of protection. A security package can use this parameter to enable the selection of cryptographic algorithms.

When using the Digest SSP, this parameter must be set to zero.

pMessage [in, out]

A pointer to a SecBufferDesc structure. On input, the structure references one or more SecBuffer structures that can be of type SECBUFFER_DATA. That buffer contains the message to be encrypted. The message is encrypted in place, overwriting the original contents of the structure.

The function does not process buffers with the SECBUFFER_READONLY attribute.

The length of the SecBuffer structure that contains the message must be no greater than cbMaximumMessage, which is obtained from the QueryContextAttributes (Digest) (SECPKG_ATTR_STREAM_SIZES) function.

When using the Digest SSP, there must be a second buffer of type SECBUFFER_PADDING or SEC_BUFFER_DATA to hold signature information. To get the size of the output buffer, call the QueryContextAttributes (Digest) function and specify SECPKG_ATTR_SIZES. The function will return a SecPkgContext_Sizes structure. The size of the output buffer is the sum of the values in the cbMaxSignature and cbBlockSize members.

Applications that do not use SSL must supply a SecBuffer of type SECBUFFER_PADDING.

MessageSeqNo [in]

The sequence number that the transport application assigned to the message. If the transport application does not maintain sequence numbers, this parameter must be zero.

When using the Digest SSP, this parameter must be set to zero. The Digest SSP manages sequence numbering internally.

Return value

If the function succeeds, the function returns SEC_E_OK.

If the function fails, it returns one of the following error codes.

Return code Description
SEC_E_BUFFER_TOO_SMALL The output buffer is too small. For more information, see Remarks.
SEC_E_CONTEXT_EXPIRED The application is referencing a context that has already been closed. A properly written application should not receive this error.
SEC_E_CRYPTO_SYSTEM_INVALID The cipher chosen for the security context is not supported.
SEC_E_INSUFFICIENT_MEMORY There is not enough memory available to complete the requested action.
SEC_E_INVALID_HANDLE A context handle that is not valid was specified in the phContext parameter.
SEC_E_INVALID_TOKEN No SECBUFFER_DATA type buffer was found.
SEC_E_QOP_NOT_SUPPORTED Neither confidentiality nor integrity are supported by the security context.

Remarks

The EncryptMessage (Digest) function encrypts a message based on the message and the session key from a security context.

If the transport application created the security context to support sequence detection and the caller provides a sequence number, the function includes this information with the encrypted message. Including this information protects against replay, insertion, and suppression of messages. The security package incorporates the sequence number passed down from the transport application.

When you use the Digest SSP, get the size of the output buffer by calling the QueryContextAttributes (Digest) function and specifying SECPKG_ATTR_SIZES. The function will return a SecPkgContext_Sizes structure. The size of the output buffer is the sum of the values in the cbMaxSignature and cbBlockSize members.

Note:
These buffers must be supplied in the order shown.

Buffer type Description
SECBUFFER_STREAM_HEADER Used internally. No initialization required.
SECBUFFER_DATA Contains the plaintext message to be encrypted.
SECBUFFER_STREAM_TRAILER Used internally. No initialization required.
SECBUFFER_EMPTY Used internally. No initialization required. Size can be zero.

For optimal performance, the pMessage structures should be allocated from contiguous memory.

Windows XP: This function was also known as SealMessage. Applications should now use EncryptMessage (Digest) only.

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Header Sspi.h (include Security.h)
Library Secur32.lib
DLL Secur32.dll

See also

SSPI Functions

AcceptSecurityContext (Digest)

DecryptMessage (Digest)

InitializeSecurityContext (Digest)

QueryContextAttributes (Digest)

SecBuffer

SecBufferDesc

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 07/25/2019
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。