Crypt_DiffieHellman [ class tree: Crypt_DiffieHellman ] [ index: Crypt_DiffieHellman ] [ all elements ]

# Class: Crypt_DiffieHellman

Source Location: /Crypt_DiffieHellman-0.2.6/Crypt/DiffieHellman.php

### Class Overview

Crypt_DiffieHellman class

#### Version:

• @package_version@

### Class Details

[line 120]
Crypt_DiffieHellman class

Example usage: Bob and Alice have started to communicate and wish to establish a shared common secret key with which to sign messages. Both establish two common pieces of information:

• a large prime number
• a generator number
Both also generate a private key (different for each) and a public key. They then transmit their public keys to each other, and agree on the prime and generator also. Both then perform identical sets of Diffie Hellman calculations and calculate a key which only each could calculate.

This is secure for a very simple reason - no other party can reverse engineer the public keys to get hold of the private keys which are essential pieces of calculating the Diffie-Hellman shared secret. The algorithm ensures this by using Modular Exponentiation which expresses a one-way-function behaviour (it's computationally infeasible to reverse it).

Using the data below, both will agree a shared secret key of 117.

Alice: prime = 563 generator = 5 private key = 9 Bob: prime = 563 generator = 5 private key = 14

\$alice = new Crypt_DiffieHellman(563, 5, 9); \$alice_pubKey = alice->generateKeys()->getPublicKey(); \$bob = new Crypt_DiffieHellman(563, 5, 14); \$bob_pubKey = \$bob->generateKeys()->getPublicKey();

// the public keys are then exchanged (with agreed prime and generator)

\$alice_computeKey = \$alice->computeSecretKey(\$bob_pubKey)->getSharedSecretKey(); \$bob_computeKey = \$bob->computeSecretKey(\$alice_pubKey)->getSharedSecretKey();

assert(\$alice_computeKey == \$bob_computeKey);

Alice and Bob have now established the same shared secret key of 117. They may now sign exchanged messages which the other party may then authenticate upon receipt.

In order to facilitate the practice of transmitting large integers in their binary form, input and output methods may accept an additional parameter of Crypt_DiffieHellman::BINARY to tell this method when the input/output should be converted from, or to, binary form. An alternate parameter of Crypt_DiffieHellman::BTWOC is used only for output methods and returns the binary big-endian twos complement of the binary form to maintain consistent binary conversion across platforms.

Although the example above uses a simple prime number, it is important to always use a sufficiently large prime, preferably one of the primes deemed to have positive cryptographic qualities. The generator is always a number less than the prime number.

[ Top ]

### __construct (Constructor)   [line 189]

 ```Crypt_DiffieHellman __construct( string|integer \$prime, string|integer \$generator, [string|integer \$privateKey = null], [string \$privateKeyType = null], [string \$mathExtension = null])```

Constructor; if set construct the object using the parameter array to set values for Prime, Generator and Private.

If a Private Key is not set, one will be generated at random.

• Access: public

#### Parameters:

 string|integer \$prime — string|integer \$generator — string|integer \$privateKey — string \$privateKeyType — string \$mathExtension —

[ Top ]

### computeSecretKey   [line 247]

 ```void computeSecretKey( string \$publicKey, [string \$type = self::NUMBER])```

Compute the shared secret key based on the public key received from the the second party to this transaction. This should agree to the secret key the second party computes on our own public key.

Once in agreement, the key is known to only to both parties. By default, the function expects the public key to be in binary form which is the typical format when being transmitted.

• Access: public

#### Parameters:

 string \$publicKey — string \$type —

[ Top ]

### generateKeys   [line 208]

 ```Crypt_DiffieHellman generateKeys( )```

Generate own public key. If a private number has not already been set, one will be generated at this stage.
• Access: public

[ Top ]

### getGenerator   [line 340]

 ```string getGenerator( [string \$type = self::NUMBER])```

Getter for the value of the generator number
• Access: public

#### Parameters:

 string \$type —

[ Top ]

### getPrime   [line 302]

 ```string getPrime( [string \$type = self::NUMBER])```

Getter for the value of the prime number
• Access: public

#### Parameters:

 string \$type —

[ Top ]

### getPrivateKey   [line 380]

 ```string getPrivateKey( [string \$type = self::NUMBER])```

Getter for the value of the private number
• Access: public

#### Parameters:

 string \$type —

[ Top ]

### getPublicKey   [line 221]

 ```string getPublicKey( [string \$type = self::NUMBER])```

Returns own public key for communication to the second party to this transaction.
• Access: public

#### Parameters:

 string \$type —

[ Top ]

### getSharedSecretKey   [line 266]

 ```string getSharedSecretKey( [string \$type = self::NUMBER])```

Return the computed shared secret key from the DiffieHellman transaction
• Access: public

#### Parameters:

 string \$type —

[ Top ]

### setBigIntegerMath   [line 406]

 ```void setBigIntegerMath( [string \$extension = null])```

Setter to pass an extension parameter which is used to create a specific BigInteger instance for a specific extension type.

Allows manual setting of the class in case of an extension problem or bug.

Due to the temporary nature of BigInteger wrapper, this decision is deferred to Crypt_DiffieHellman_Math which extends (in a slightly reversed way) Crypt_DiffieHellman_Math_BigInteger.

• Access: public

#### Parameters:

 string \$extension —

[ Top ]

### setGenerator   [line 324]

 ```Crypt_DiffieHellman setGenerator( string \$number)```

Setter for the value of the generator number
• Access: public

#### Parameters:

 string \$number —

[ Top ]

### setPrime   [line 286]

 ```Crypt_DiffieHellman setPrime( string \$number)```

Setter for the value of the prime number
• Access: public

#### Parameters:

 string \$number —

[ Top ]

### setPrivateKey   [line 361]

 ```Crypt_DiffieHellman setPrivateKey( string|integer \$number, [string \$type = self::NUMBER])```

Setter for the value of the private number
• Access: public

#### Parameters:

 string|integer \$number — string \$type —

[ Top ]

### _generatePrivateKey   [line 417]

 ```string _generatePrivateKey( )```

In the event a private number/key has not been set by the user, generate one at random.
• Access: protected

[ Top ]

Documentation generated on Sun, 01 Apr 2012 21:30:03 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.