NAME
Crypt::PK::DH - Public key cryptography based on Diffie-Hellman
SYNOPSIS
### OO interface
#Encryption: Alice
my $pub = Crypt::PK::DH->new('Bob_pub_dh1.der');
my $ct = $pub->encrypt("secret message");
#
#Encryption: Bob (received ciphertext $ct)
my $priv = Crypt::PK::DH->new('Bob_priv_dh1.der');
my $pt = $priv->decrypt($ct);
#Signature: Alice
my $priv = Crypt::PK::DH->new('Alice_priv_dh1.der');
my $sig = $priv->sign($message);
#
#Signature: Bob (received $message + $sig)
my $pub = Crypt::PK::DH->new('Alice_pub_dh1.der');
$pub->verify($sig, $message) or die "ERROR";
#Shared secret
my $priv = Crypt::PK::DH->new('Alice_priv_dh1.der');
my $pub = Crypt::PK::DH->new('Bob_pub_dh1.der');
my $shared_secret = $priv->shared_secret($pub);
#Key generation
my $pk = Crypt::PK::DH->new();
$pk->generate_key(128);
my $private = $pk->export_key('private');
my $public = $pk->export_key('public');
### Functional interface
#Encryption: Alice
my $ct = dh_encrypt('Bob_pub_dh1.der', "secret message");
#Encryption: Bob (received ciphertext $ct)
my $pt = dh_decrypt('Bob_priv_dh1.der', $ct);
#Signature: Alice
my $sig = dh_sign('Alice_priv_dh1.der', $message);
#Signature: Bob (received $message + $sig)
dh_verify('Alice_pub_dh1.der', $sig, $message) or die "ERROR";
#Shared secret
my $shared_secret = dh_shared_secret('Alice_priv_dh1.der', 'Bob_pub_dh1.der');
FUNCTIONS
dh_encrypt
dh_decrypt
dh_sign
dh_verify
dh_shared_secret
METHODS
new
my $pk = Crypt::PK::DH->new();
#or
my $pk = Crypt::PK::DH->new($priv_or_pub_key_filename);
#or
my $pk = Crypt::PK::DH->new(\$buffer_containing_priv_or_pub_key);
generate_key
Uses Yarrow-based cryptographically strong random number generator seeded with random data taken from /dev/random
(UNIX) or CryptGenRandom
(Win32).
$pk->generate_key($keysize);
# $keysize:
# 96 => DH-768
# 128 => DH-1024
# 160 => DH-1280
# 192 => DH-1536
# 224 => DH-1792
# 256 => DH-2048
# 320 => DH-2560
# 384 => DH-3072
# 512 => DH-4096
import_key
$pk->import_key($filename);
#or
$pk->import_key(\$buffer_containing_key);
export_key
my $private = $pk->export_key('private');
#or
my $public = $pk->export_key('public');
encrypt
decrypt
sign
verify
shared_secret
# Alice having her priv key $pk and Bob's public key $pkb
my $pk = Crypt::PK::DH->new($priv_key_filename);
my $pkb = Crypt::PK::DH->new($pub_key_filename);
my $shared_secret = $pk->shared_secret($pkb);
# Bob having his priv key $pk and Alice's public key $pka
my $pk = Crypt::PK::DH->new($priv_key_filename);
my $pka = Crypt::PK::DH->new($pub_key_filename);
my $shared_secret = $pk->shared_secret($pka); # same value as computed by Alice