Crypt::DH - Diffie-Hellman key exchange system
my $dh = Crypt::DH->new;
## Generate public and private keys.
$my_pub_key = $dh->pub_key;
## Send $my_pub_key to "other" party, and receive "other"
## public key in return.
## Now compute shared secret from "other" public key.
my $shared_secret = $dh->compute_secret( $other_pub_key );
is a Perl implementation of the Diffie-Hellman key exchange
system. Diffie-Hellman is an algorithm by which two parties can agree on a
shared secret key, known only to them. The secret is negotiated over an
insecure network without the two parties ever passing the actual shared
secret, or their private keys, between them.
The algorithm generally works as follows: Party A and Party B choose a property
and a property g
; these properties are shared by both parties.
Each party then computes a random private key integer priv_key
the length of priv_key
is at most (number of bits in p
) - 1.
Each party then computes a public key based on g
; the exact value is
g ^ priv_key mod p
The parties exchange these public keys.
The shared secret key is generated based on the exchanged public key, the
private key, and p
. If the public key of Party B is denoted
, then the shared secret is equal to
pub_key_B ^ priv_key mod p
The mathematical principles involved insure that both parties will generate the
same shared secret key.
More information can be found in PKCS #3 (Diffie-Hellman Key Agreement
implements the core routines needed to use Diffie-Hellman key
exchange. To actually use the algorithm, you'll need to start with values for
is a large prime, and g
is a base which
must be larger than 0 and less than p
internally for big-integer
calculations. All accessor methods ( p
) thus return Math::BigInt
objects, as does the
method. The accessors, however, allow setting with a
scalar decimal string, hex string (^0x), Math::BigInt object, or Math::Pari
object (for backwards compatibility).
Constructs a new Crypt::DH
object and returns the object.
may include none, some, or all of the keys
, and priv_key
Given an argument $p
, sets the p
prime) for this Crypt::DH
Returns the current value of p
. (as a Math::BigInt object)
Given an argument $g
, sets the g
for this Crypt::DH
Returns the current value of g
Generates the public and private key portions of the Crypt::DH
assuming that you've already filled p
If you've provided a priv_key, it's used, otherwise a random priv_key is created
using either Crypt::Random (if already loaded), or /dev/urandom, or Perl's
rand, in that order.
Given the public key $public_key
of Party B (the party with
which you're performing key negotiation and exchange), computes the shared
secret key, based on that public key, your own private key, and your own large
prime value ( p
The historical method name "compute_key" is aliased to this for
Returns the private key. Given an argument $priv_key
parameter for this Crypt::DH
Returns the public key.
Benjamin Trott (cpan:BTROTT) <email@example.com>
Brad Fitzpatrick (cpan:BRADFITZ) <firstname.lastname@example.org>
BinGOs - Chris Williams (cpan:BINGOS) <email@example.com>
Mithaldu - Christian Walde (cpan:MITHALDU)
Copyright (c) 2012 the Crypt::DH "AUTHOR" and "CONTRIBUTORS"
as listed above.
This library is free software and may be distributed under the same terms as