Class Ntlm
- All Implemented Interfaces:
AutoCloseable
Generate, parse, and verify NTLM authentication messages.
Chilkat.Ntlm implements the NTLM challenge/response message
workflow. It can generate Type 1, Type 2, and Type 3 NTLM messages, parse
those messages for inspection, compare Type 3 responses on the server side,
and control key authentication inputs such as username, password, domain,
workstation, target name, client challenge, server challenge, NTLM version,
encoding mode, and negotiate flags.
Type 1 negotiate message
Generate the client-to-server message that begins the NTLM exchange and advertises supported options.
Type 2 challenge message
Generate the server challenge response, including target name, server challenge, and optional target-info fields.
Type 3 authenticate message
Generate the final client response using the username and password without sending the password itself.
Server-side verification
Load a received Type 3 message, extract embedded fields, recompute the
response, and compare the result with CompareType3.
Challenges and encoding
Set or inspect 8-byte client and server challenges using encodings such as hex, Base64, Base32, quoted-printable, URL encoding, and others.
Version, flags, and diagnostics
Select NTLMv1 or NTLMv2, adjust negotiate flags when required, parse
messages for readable diagnostics, and check LastErrorText.
Common pattern:
The client calls GenType1 and sends the Type 1 message to the
server. The server calls GenType2 and sends the Type 2 challenge
back. The client sets UserName and Password, calls
GenType3, and sends the Type 3 message. The server calls
LoadType3, looks up the user's password, recomputes the Type 3
response, and uses CompareType3 to verify authentication.
Flag guidance:
The default Flags value is intended for normal use. Only modify
negotiate flags with SetFlag or by setting Flags
directly when the application has specific NTLM protocol requirements and
the meaning of the affected flags is understood.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancompareType3(String msg1, String msg2)Compares two encoded Type 3 authenticate messages and returnstruewhen their LM and NT challenge-response fields match.genType1()Generates the client-to-server Type 1 Negotiate message that starts an NTLM authentication exchange.Generates the server-to-client Type 2 Challenge message from the encoded Type 1 message intype1Msg.Generates the client-to-server Type 3 Authenticate message from the encoded Type 2 message intype2Msg.Specifies the 8-byte client challenge used when generating the Type 3 authenticate message.Specifies the server's DNS computer name for inclusion in theMsvAvDnsComputerNameentry of the Type 2 message'sTargetInfoblock.Specifies the server's DNS domain name for inclusion in theMsvAvDnsDomainNameentry of the Type 2 message'sTargetInfoblock.Specifies the client's domain or workgroup name.Controls the textual encoding used when Chilkat exposes binary NTLM values as strings or accepts them from strings.getFlags()Specifies the NTLM negotiate flags as a compact string in which each letter represents one bit.Specifies the server's NetBIOS computer name for inclusion in theMsvAvNbComputerNameentry of the Type 2 message'sTargetInfoblock.Specifies the server's NetBIOS domain name for inclusion in theMsvAvNbDomainNameentry of the Type 2 message'sTargetInfoblock.intSelects the challenge-response algorithm used to generate and verify the authentication response.intSpecifies the OEM code page used to encode NTLM text fields when Unicode negotiation is disabled.Specifies the password forUserName.Specifies the 8-byte server challenge carried in the Type 2 challenge message.Specifies the server-provided authentication target placed in the Type 2 challenge message.Specifies the account name to authenticate.Specifies the optional client workstation name.voidParses an encoded Type 3 authenticate message received by server-side code and loads its client-supplied fields into this object.parseType1(String type1Msg)Parses an encoded Type 1 negotiate message and returns human-readable XML describing its fields, including negotiation flags and any supplied domain or workstation information.parseType2(String type2Msg)Parses an encoded Type 2 challenge message and returns human-readable XML describing its fields, including flags, target name, server challenge, and target-information AV pairs when present.parseType3(String type3Msg)Parses an encoded Type 3 authenticate message and returns human-readable XML describing its fields, including the reported username, domain, workstation, and challenge-response data.voidsetClientChallenge(String value)Sets theClientChallengeproperty; seegetClientChallenge().voidsetDnsComputerName(String value)Sets theDnsComputerNameproperty; seegetDnsComputerName().voidsetDnsDomainName(String value)Sets theDnsDomainNameproperty; seegetDnsDomainName().voidSets theDomainproperty; seegetDomain().voidsetEncodingMode(String value)Sets theEncodingModeproperty; seegetEncodingMode().voidTurns one Chilkat flag letter on or off inFlags.voidSets theFlagsproperty; seegetFlags().voidsetNetBiosComputerName(String value)Sets theNetBiosComputerNameproperty; seegetNetBiosComputerName().voidsetNetBiosDomainName(String value)Sets theNetBiosDomainNameproperty; seegetNetBiosDomainName().voidsetNtlmVersion(int value)Sets theNtlmVersionproperty; seegetNtlmVersion().voidsetOemCodePage(int value)Sets theOemCodePageproperty; seegetOemCodePage().voidsetPassword(String value)Sets thePasswordproperty; seegetPassword().voidsetServerChallenge(String value)Sets theServerChallengeproperty; seegetServerChallenge().voidsetTargetName(String value)Sets theTargetNameproperty; seegetTargetName().voidsetUserName(String value)Sets theUserNameproperty; seegetUserName().voidsetWorkstation(String value)Sets theWorkstationproperty; seegetWorkstation().Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Ntlm
public Ntlm()Creates a newNtlmobject.
-
-
Method Details
-
getClientChallenge
Specifies the 8-byte client challenge used when generating the Type 3 authenticate message. Because this property is a string, the bytes are represented usingEncodingMode. For example, whenEncodingModeishex, set this property to exactly 16 hexadecimal characters.Setting this property is optional. If it is empty when
GenType3is called, Chilkat generates eight random bytes automatically.Security note: A client challenge is a nonce, not a password or encryption key. Use a fresh unpredictable value for each authentication exchange; do not deliberately reuse a fixed challenge in production.
- Returns:
- the value of the
ClientChallengeproperty
-
setClientChallenge
Sets theClientChallengeproperty; seegetClientChallenge().- Parameters:
value- the new value (notnull)
-
getDnsComputerName
Specifies the server's DNS computer name for inclusion in theMsvAvDnsComputerNameentry of the Type 2 message'sTargetInfoblock.This property is optional and is used by server-side code when constructing the
TargetInfoAV-pair sequence in a Type 2 challenge message.If any of the optional target-information name properties are supplied, Chilkat requires both
NetBiosComputerNameandNetBiosDomainNameto be set.NTLMv2 background: Target information is sent by the server and incorporated into the NTLMv2 response. The values should describe the actual authentication target; they are not arbitrary client-supplied labels.
- Returns:
- the value of the
DnsComputerNameproperty
-
setDnsComputerName
Sets theDnsComputerNameproperty; seegetDnsComputerName().- Parameters:
value- the new value (notnull)
-
getDnsDomainName
Specifies the server's DNS domain name for inclusion in theMsvAvDnsDomainNameentry of the Type 2 message'sTargetInfoblock.This property is optional and is used by server-side code when constructing the
TargetInfoAV-pair sequence in a Type 2 challenge message.If any of the optional target-information name properties are supplied, Chilkat requires both
NetBiosComputerNameandNetBiosDomainNameto be set.NTLMv2 background: Target information is sent by the server and incorporated into the NTLMv2 response. The values should describe the actual authentication target; they are not arbitrary client-supplied labels.
- Returns:
- the value of the
DnsDomainNameproperty
-
setDnsDomainName
Sets theDnsDomainNameproperty; seegetDnsDomainName().- Parameters:
value- the new value (notnull)
-
getDomain
Specifies the client's domain or workgroup name. When provided, it can be included in the Type 1 negotiate message and is used as the domain component of the identity in the Type 3 authenticate message.For a domain account, use the account's Windows domain name. For a local account, the expected value depends on the authenticating server and protocol.
Identity note:
Domainis separate fromTargetName.Domainidentifies the account's domain in the client identity, whereasTargetNameis supplied by the server in the Type 2 challenge.- Returns:
- the value of the
Domainproperty
-
setDomain
Sets theDomainproperty; seegetDomain().- Parameters:
value- the new value (notnull)
-
getEncodingMode
Controls the textual encoding used when Chilkat exposes binary NTLM values as strings or accepts them from strings. This includes generated and parsed NTLM message tokens and binary-valued properties such asClientChallengeandServerChallenge.The mode name is case-insensitive. Supported values include
base64,modBase64,base32,UU,QP,URL,hex,Q,B,url_oauth,url_rfc1738,url_rfc2396, andurl_rfc3986.Interoperability: NTLM tokens carried in HTTP
AuthorizationorWWW-Authenticateheaders are conventionally Base64 encoded. Use the sameEncodingModefor both the producer and consumer of a token.- Returns:
- the value of the
EncodingModeproperty
-
setEncodingMode
Sets theEncodingModeproperty; seegetEncodingMode().- Parameters:
value- the new value (notnull)
-
getFlags
Specifies the NTLM negotiate flags as a compact string in which each letter represents one bit. The flags advertise or select character encoding, target information, session security, and other protocol capabilities.The default is
ABCHMQ, which enables Unicode, OEM compatibility, target-name request, NTLM authentication, always-sign behavior, and extended session security.Letter Bit value Microsoft flag name
A0x00000001NTLMSSP_NEGOTIATE_UNICODEB0x00000002NTLM_NEGOTIATE_OEMC0x00000004NTLMSSP_REQUEST_TARGETD0x00000010NTLMSSP_NEGOTIATE_SIGNE0x00000020NTLMSSP_NEGOTIATE_SEALF0x00000040NTLMSSP_NEGOTIATE_DATAGRAMG0x00000080NTLMSSP_NEGOTIATE_LM_KEYH0x00000200NTLMSSP_NEGOTIATE_NTLMI0x00000400Reserved / unusedJ0x00000800NTLMSSP_NEGOTIATE_ANONYMOUSK0x00001000NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIEDL0x00002000NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIEDM0x00008000NTLMSSP_NEGOTIATE_ALWAYS_SIGNN0x00010000NTLMSSP_TARGET_TYPE_DOMAINO0x00020000NTLMSSP_TARGET_TYPE_SERVERP0x00040000Reserved / unusedQ0x00080000NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITYR0x00100000NTLMSSP_NEGOTIATE_IDENTIFYS0x00400000NTLMSSP_REQUEST_NON_NT_SESSION_KEYT0x00800000NTLMSSP_NEGOTIATE_TARGET_INFOU0x02000000NTLMSSP_NEGOTIATE_VERSIONV0x20000000NTLMSSP_NEGOTIATE_128W0x40000000NTLMSSP_NEGOTIATE_KEY_EXCHX0x80000000NTLMSSP_NEGOTIATE_56Compatibility warning: Leave this property at its default unless a specific protocol peer requires different flags and the consequences are understood. Some bits are mutually dependent, some are meaningful only in particular message types, and reserved bits should remain clear. Prefer
SetFlagwhen changing a single option.- Returns:
- the value of the
Flagsproperty
-
setFlags
Sets theFlagsproperty; seegetFlags().- Parameters:
value- the new value (notnull)
-
getNetBiosComputerName
Specifies the server's NetBIOS computer name for inclusion in theMsvAvNbComputerNameentry of the Type 2 message'sTargetInfoblock.This property is optional and is used by server-side code when constructing the
TargetInfoAV-pair sequence in a Type 2 challenge message.If any of the optional target-information name properties are supplied, Chilkat requires both
NetBiosComputerNameandNetBiosDomainNameto be set.NTLMv2 background: Target information is sent by the server and incorporated into the NTLMv2 response. The values should describe the actual authentication target; they are not arbitrary client-supplied labels.
- Returns:
- the value of the
NetBiosComputerNameproperty
-
setNetBiosComputerName
Sets theNetBiosComputerNameproperty; seegetNetBiosComputerName().- Parameters:
value- the new value (notnull)
-
getNetBiosDomainName
Specifies the server's NetBIOS domain name for inclusion in theMsvAvNbDomainNameentry of the Type 2 message'sTargetInfoblock.This property is optional and is used by server-side code when constructing the
TargetInfoAV-pair sequence in a Type 2 challenge message.If any of the optional target-information name properties are supplied, Chilkat requires both
NetBiosComputerNameandNetBiosDomainNameto be set.NTLMv2 background: Target information is sent by the server and incorporated into the NTLMv2 response. The values should describe the actual authentication target; they are not arbitrary client-supplied labels.
- Returns:
- the value of the
NetBiosDomainNameproperty
-
setNetBiosDomainName
Sets theNetBiosDomainNameproperty; seegetNetBiosDomainName().- Parameters:
value- the new value (notnull)
-
getNtlmVersion
public int getNtlmVersion()Selects the challenge-response algorithm used to generate and verify the authentication response.Value Behavior
1Use NTLMv1. This is the default for backward compatibility.2Use NTLMv2.The client and server must use compatible response algorithms. This setting is not negotiated by the three NTLM messages.
Security recommendation: Set this property to
2for modern deployments. NTLMv1 is obsolete and has been removed from Windows 11 version 24H2 and Windows Server 2025. Use NTLM only where compatibility requires it; Kerberos through Negotiate is preferred in Active Directory environments.- Returns:
- the value of the
NtlmVersionproperty
-
setNtlmVersion
public void setNtlmVersion(int value)Sets theNtlmVersionproperty; seegetNtlmVersion().- Parameters:
value- the new value
-
getOemCodePage
public int getOemCodePage()Specifies the OEM code page used to encode NTLM text fields when Unicode negotiation is disabled. The default is the local computer's OEM code page.This property applies only when flag
A(NTLMSSP_NEGOTIATE_UNICODE) is clear and OEM encoding is selected. With the default flags, Unicode is used and this setting has no effect.Interoperability: Both sides must interpret OEM-encoded fields using compatible code pages. Retaining Unicode negotiation avoids locale-dependent corruption of usernames, domains, and workstation names.
- Returns:
- the value of the
OemCodePageproperty
-
setOemCodePage
public void setOemCodePage(int value)Sets theOemCodePageproperty; seegetOemCodePage().- Parameters:
value- the new value
-
getPassword
Specifies the password forUserName. Client-side code must set it before callingGenType3. Server-side verification code sets it afterLoadType3, typically after looking up the account identified by the loaded username and domain.The password itself is not placed in the Type 3 message. It is used to derive the challenge-response proof.
Sensitive value: Treat this property as credential material. Do not log it, persist it in plain text, or retain it longer than necessary. NTLM challenge-response still depends on password-derived secrets and does not make weak passwords safe.
- Returns:
- the value of the
Passwordproperty
-
setPassword
Sets thePasswordproperty; seegetPassword().- Parameters:
value- the new value (notnull)
-
getServerChallenge
Specifies the 8-byte server challenge carried in the Type 2 challenge message. Because this property is a string, the bytes are represented usingEncodingMode. For example, withEncodingModeset tohex, provide exactly 16 hexadecimal characters.Setting this property is optional. If it is empty when
GenType2is called, Chilkat generates eight random bytes automatically. Server-side verification must use the same challenge that was sent to the client.Security note: Generate a fresh unpredictable server challenge for each authentication exchange. Reusing challenges increases exposure to replay and precomputation attacks.
- Returns:
- the value of the
ServerChallengeproperty
-
setServerChallenge
Sets theServerChallengeproperty; seegetServerChallenge().- Parameters:
value- the new value (notnull)
-
getTargetName
Specifies the server-provided authentication target placed in the Type 2 challenge message. Depending on the target-type flags, it commonly identifies a Windows domain or the server handling local-account authentication.This is not the client account's domain; use
Domainfor that purpose.Protocol note: The target name helps describe the authentication realm advertised by the server. It does not by itself authenticate the server or prevent NTLM relay attacks.
- Returns:
- the value of the
TargetNameproperty
-
setTargetName
Sets theTargetNameproperty; seegetTargetName().- Parameters:
value- the new value (notnull)
-
getUserName
Specifies the account name to authenticate. Client-side code must set it before callingGenType3. After server-side code callsLoadType3, this property contains the username extracted from the received Type 3 message.Use
Domainseparately when the account belongs to a Windows domain or workgroup.Server-side rule: Treat the username and domain from a received Type 3 message as untrusted input until the challenge response has been verified.
- Returns:
- the value of the
UserNameproperty
-
setUserName
Sets theUserNameproperty; seegetUserName().- Parameters:
value- the new value (notnull)
-
getWorkstation
Specifies the optional client workstation name. When supplied, Chilkat can include it in the Type 1 negotiate message and the Type 3 authenticate message. AfterLoadType3, it contains the workstation value reported by the client. Identity note: The workstation field is descriptive client-supplied metadata. It is not proof of the client's computer identity and should not be used as an authorization decision by itself.- Returns:
- the value of the
Workstationproperty
-
setWorkstation
Sets theWorkstationproperty; seegetWorkstation().- Parameters:
value- the new value (notnull)
-
compareType3
Compares two encoded Type 3 authenticate messages and returnstruewhen their LM and NT challenge-response fields match. A server commonly passes the Type 3 message received from the client as one argument and the expected Type 3 message recomputed withGenType3as the other.The messages must use the encoding selected by
EncodingMode. Other Type 3 metadata, such as username or workstation text, is not the basis of this comparison.Authentication workflow: A matching response is meaningful only when the expected message was generated from the original Type 2 challenge, the correct account password, the same NTLM version, and the relevant loaded client fields. A
falseresult means the response fields do not match; inspectLastMethodSuccessandLastErrorTextif malformed input or another processing error is possible.- Parameters:
msg1- themsg1argument (notnull)msg2- themsg2argument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
genType1
Generates the client-to-server Type 1 Negotiate message that starts an NTLM authentication exchange. The message advertises the capabilities inFlagsand can include the optionalDomainandWorkstationvalues.The returned token is encoded according to
EncodingMode. CheckLastMethodSuccessbefore using the returned string.Message flow: Type 1 contains negotiation metadata; it does not contain the user's password or challenge response.
- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
genType2
Generates the server-to-client Type 2 Challenge message from the encoded Type 1 message intype1Msg. Before calling, server-side code normally setsTargetNameand may setServerChallengeand the target-information name properties.If
ServerChallengeis empty, Chilkat generates a random 8-byte challenge. The input and returned token useEncodingMode. CheckLastMethodSuccessbefore sending the result.State requirement: Preserve the Type 2 message—or at minimum all state needed to reproduce it—for the later server-side verification of Type 3. The expected response is bound to the challenge and target information sent in this message.
- Parameters:
type1Msg- thetype1Msgargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
genType3
Generates the client-to-server Type 3 Authenticate message from the encoded Type 2 message intype2Msg. Client-side code must setUserNameandPassword; setDomainandWorkstationwhen required by the account and peer.Chilkat computes the response using
NtlmVersion, the server challenge and target information from Type 2, and the client challenge. IfClientChallengeis empty, Chilkat generates it automatically. The password itself is not transmitted.The input and returned token use
EncodingMode. CheckLastMethodSuccessbefore using the returned string.Security limitation: NTLM proves knowledge of password-derived material but does not by itself authenticate the server. Deploy it only inside a transport and protocol configuration that addresses relay, channel protection, and server identity.
- Parameters:
type2Msg- thetype2Msgargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadType3
Parses an encoded Type 3 authenticate message received by server-side code and loads its client-supplied fields into this object. On success,UserName,Domain,Workstation, andClientChallengeare populated from the message.This method parses the message; it does not authenticate the client. After loading, look up the account's password, set
Password, recompute the expected Type 3 response withGenType3using the original Type 2 message, and compare the response fields withCompareType3.Do not trust parsed identity fields yet: Values extracted from Type 3 are controlled by the remote client. Use them only to locate the candidate account until the challenge response has been verified.
- Parameters:
type3Msg- thetype3Msgargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
parseType1
Parses an encoded Type 1 negotiate message and returns human-readable XML describing its fields, including negotiation flags and any supplied domain or workstation information.The input token is decoded according to
EncodingMode. The returned text is diagnostic XML intended for inspection and troubleshooting; parsing a message does not verify its authenticity. CheckLastMethodSuccessbefore using the returned string.- Parameters:
type1Msg- thetype1Msgargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
parseType2
Parses an encoded Type 2 challenge message and returns human-readable XML describing its fields, including flags, target name, server challenge, and target-information AV pairs when present.The input token is decoded according to
EncodingMode. The returned text is diagnostic XML intended for inspection and troubleshooting; parsing a message does not verify its authenticity. CheckLastMethodSuccessbefore using the returned string.Diagnostic sensitivity: A Type 2 message contains a live server challenge and target metadata. Avoid exposing diagnostic output unnecessarily while an authentication exchange is active.
- Parameters:
type2Msg- thetype2Msgargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
parseType3
Parses an encoded Type 3 authenticate message and returns human-readable XML describing its fields, including the reported username, domain, workstation, and challenge-response data.The input token is decoded according to
EncodingMode. The returned text is diagnostic XML intended for inspection and troubleshooting; parsing a message does not verify its authenticity. CheckLastMethodSuccessbefore using the returned string.Sensitive diagnostic data: The password is not present, but Type 3 contains account identifiers and password-derived response material that may be useful to an attacker. Do not place parsed output in public or long-lived logs.
- Parameters:
type3Msg- thetype3Msgargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFlag
Turns one Chilkat flag letter on or off inFlags.flagLetteris a single letter listed in theFlagstable, andonOrOffistrueto set the bit orfalseto clear it.Returns
truewhen the flag letter is accepted and the value is updated; otherwise returnsfalse.Use with care: NTLM flags have dependencies and message-specific meanings. Modify them only for a known interoperability requirement, and avoid enabling reserved letters.
- Parameters:
flagLetter- theflagLetterargument (notnull)onOrOff- theonOrOffargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-