Class Dh
- All Implemented Interfaces:
AutoCloseable
Perform Diffie-Hellman shared-secret key exchange.
Chilkat.Dh provides the core operations needed for
Diffie-Hellman shared-secret exchange. It allows two parties to agree on a
shared secret over an insecure channel by using common Diffie-Hellman
parameters, generating public exchange values, and computing the same shared
secret from the other party's public value.
Choose known parameters
Use UseKnownPrime when the application wants to use a
predefined Diffie-Hellman prime group.
Generate parameters
Use GenPG to generate Diffie-Hellman parameters when custom
prime and generator values are required.
Set custom P and G
Use SetPG when the prime and generator are already known or
have been received from another party or protocol.
Create the exchange value
Use CreateE to create the public Diffie-Hellman value that
is sent to the other party.
Compute the shared secret
Use FindK with the other party's public exchange value to
compute the shared secret.
Use the result carefully
The shared secret is typically used as input to a key derivation step before being used for encryption or authentication.
Common pattern:
Select or generate the Diffie-Hellman parameters, create your public
exchange value with CreateE, exchange public values with the
other party, then call FindK to compute the shared secret. The
primary methods are UseKnownPrime, GenPG,
SetPG, CreateE, and FindK.
For an extended overview, see Dh Class Overview.
</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 TypeMethodDescriptioncreateE(int numBits)Creates this party's public Diffie-Hellman exchange value, conventionally written asE = gx mod p.Computes the Diffie-Hellman shared secret from the other party's public exchange value.voidgenPG(int numBits, int G)Generates new finite-field Diffie-Hellman parameters and stores them in this object.intgetG()Returns the current Diffie-Hellman generator,g.getP()Returns the current Diffie-Hellman prime modulus,p, as a hex-encoded SSH1-format bignum.voidSets explicit finite-field Diffie-Hellman parameters for this object.voiduseKnownPrime(int index)Selects one of eight built-in safe-prime MODP groups and sets the object'sPandGproperties.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Dh
public Dh()Creates a newDhobject.
-
-
Method Details
-
getG
public int getG()Returns the current Diffie-Hellman generator,g. The generator is the small public integer used with the prime moduluspin the modular exponentiation operations that create the public exchange value and shared secret.For this class,
gis expected to be2or5. Its value is established byUseKnownPrime,GenPG, orSetPG.Public parameter: The generator is not secret. Both parties must use exactly the same
pandgvalues for the exchange.- Returns:
- the value of the
Gproperty (read-only)
-
getP
Returns the current Diffie-Hellman prime modulus,p, as a hex-encoded SSH1-format bignum. The value is established byUseKnownPrime,GenPG, orSetPG.The modulus is a safe prime:
p = 2q + 1, whereqis also prime. Safe-prime groups are commonly used for finite-field Diffie-Hellman because their subgroup structure is well understood.Encoding note: The returned string is the hexadecimal encoding of an SSH1 bignum, which includes the SSH1 bit-length prefix. It is not simply the conventional hexadecimal representation of the integer
p.Public parameter: The prime modulus is not secret and may be transmitted to the other party. Both parties must use the same
pandg.- Returns:
- the value of the
Pproperty (read-only)
-
createE
Creates this party's public Diffie-Hellman exchange value, conventionally written asE = gx mod p. Before calling this method, initialize the common Diffie-Hellman parameters by callingUseKnownPrime,GenPG, orSetPG.(numBitsnumBits) controls the size of the randomly generated private exponent retained internally by thisDhobject. Chilkat recommends setting it to twice the desired strength of the symmetric key that will ultimately be derived. For example, use256when the protocol will derive a 128-bit key.The returned
Evalue is a hex-encoded SSH1-format bignum. It is public and is sent to the other party. The size of the eventual shared-secret integer is determined primarily byp;numBitsdoes not make the returned shared secret a fixed-length symmetric key.Object state: Call
FindKon this sameDhobject after receiving the other party's exchange value.CreateEstores the corresponding private value internally, andFindKneeds it to compute the shared secret. Use a separateDhobject for each party and generate a fresh exchange value for each session.Authentication required: Basic Diffie-Hellman does not authenticate either party and is vulnerable to a man-in-the-middle attack. The surrounding protocol must authenticate the exchanged parameters and public values.
For language bindings where a string return cannot directly communicate failure, check
LastMethodSuccess. If it isFALSE, inspectLastErrorText.- Parameters:
numBits- thenumBitsargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
findK
Computes the Diffie-Hellman shared secret from the other party's public exchange value.is the peer'sEEvalue, encoded as the hexadecimal representation of an SSH1-format bignum.This method must be called on the same
Dhobject that previously calledCreateE. Both parties must also be using the samepandgparameters. Internally, the method computesK = Ex mod p, wherexis the private value generated byCreateE.The result is the shared-secret integer
K, returned as a hex-encoded SSH1-format bignum. When the exchange is performed correctly, both parties compute the same value.Do not use the returned text directly as an encryption key: It is an encoded bignum, not a uniformly formatted symmetric key. Feed the shared-secret value into the key-derivation procedure required by the protocol, together with any required transcript, salt, labels, or context. Do not invent an ad hoc conversion when interoperability or cryptographic security matters.
Key agreement is not authentication: Matching shared secrets do not by themselves prove the identity of the peer. Authenticate the exchange to prevent man-in-the-middle attacks.
For language bindings where a string return cannot directly communicate failure, check
LastMethodSuccess. If it isFALSE, inspectLastErrorText.- Parameters:
E- theEargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
genPG
public void genPG(int numBits, int G)Generates new finite-field Diffie-Hellman parameters and stores them in this object.specifies the size of the safe primenumBitspin bits, andspecifies the generatorGg, which should be2or5.On success, the generated values are available through the
PandGproperties. The prime generation and validation process is computationally expensive, especially for large groups.Prefer standardized groups: Most applications should use
UseKnownPrimeor parameters mandated by the surrounding protocol instead of generating a new group. Standardized groups improve interoperability and avoid the cost and risk of custom parameter generation.Modern key sizes: The legacy 768-, 1024-, and 1536-bit MODP groups are not appropriate for new designs. Use at least a 2048-bit standardized group when compatibility requires finite-field DH; a 3072-bit group is commonly selected when approximately 128-bit classical security is required.
Returns
TRUEif the parameters were generated successfully; otherwise returnsFALSE. CheckLastErrorTextfor failure details.- Parameters:
numBits- thenumBitsargumentG- theGargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setPG
Sets explicit finite-field Diffie-Hellman parameters for this object.is the prime moduluspp, supplied as a hex-encoded SSH1-format bignum, andis the generatorgg, normally2or5.The method validates that the supplied values satisfy the requirements expected by this Diffie-Hellman implementation. It returns
TRUEif the parameters are accepted; otherwise it returnsFALSE.Encoding requirement:
pis not ordinary integer hex. It must contain the hexadecimal encoding of the SSH1 bignum representation, including its bit-length prefix. ThePproperty of another ChilkatDhobject is already in the required form.Interoperability: Both parties must use exactly the same
pandg. These values are public, but untrusted or nonstandard parameters should not be accepted unless the protocol explicitly permits them and their origin is authenticated.- Parameters:
p- thepargument (notnull)g- thegargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
useKnownPrime
public void useKnownPrime(int index)Selects one of eight built-in safe-prime MODP groups and sets the object'sPandGproperties.is a Chilkat selector fromindex1through8; it is not the IKE group number.indexMODP group Prime bits Definition G1Group 1 768 RFC 2409, section 6.122Group 2 1024 RFC 2409, section 6.223Group 5 1536 RFC 3526, section 224Group 14 2048 RFC 3526, section 325Group 15 3072 RFC 3526, section 426Group 16 4096 RFC 3526, section 527Group 17 6144 RFC 3526, section 628Group 18 8192 RFC 3526, section 72Security guidance: Selectors
1,2, and3are legacy groups and should not be chosen for new systems. Selector4is the 2048-bit group 14; selector5is the 3072-bit group 15 and is commonly used when approximately 128-bit classical security is required. Always use the group required by the protocol or the communicating peer.Performance: Larger groups require more CPU time and produce larger public values. Choose a group based on the required security level and interoperability requirements, rather than automatically selecting the largest group.
- Parameters:
index- theindexargument
-