"Mike Friedman" <> wrote in message
> >
> > So, I would try
> >
> > printf "Public key: %s\n", $key->pub_key;
> >
> > to print the public key.
> >
> > Sinan
>
> Sinan,
>
> I had already tried that and, indeed, it might be technically
> correct. But what I really want is an output file that is ASN.1
> encoded and in a standard PEM key format, which is what you get
> when you use the key->write method. It's just that the latter
> seems to give only a private key file (which may actually include
> the public key, but that's just speculation on my part).
>
Do you have Convert:

EM installed ? It's not a pre-requisite for
Crypt:

SA, but it might help you get at the answers you want. I don't
understand PEM format at all - if you're in the same boat then maybe there's
something in the openssl/doc/crypto/pem.pod for you.
Included below is a modified version of '04-pem.t' which is part of the
Crypt:

SA test suite. It demonstrates that the pem file does indeed hold
both public and private key information (by extracting that and other
information from
the pem file that '04-pem.t' generates).
Hth.
Cheers,
Rob
# $Id: 04-pem.t,v 1.4 2001/04/22 23:58:39 btrott Exp $
use strict;
use Test;
use Crypt:

SA;
use Crypt:

SA::Key;
my $no_pem;
BEGIN {
eval "use Convert:

EM;";
$no_pem = $@;
if ($no_pem) {
print "1..0 skipping\n";
exit;
}
plan tests => 12;
}
my $keyfile = "./dsa-key.pem";
my $dsa = Crypt:

SA->new;
my $key = $dsa->keygen( Size => 512 );
my $key2;
skip($no_pem, $key->write( Type => 'PEM', Filename => $keyfile));
$key2 = Crypt:

SA::Key->new( Type => 'PEM', Filename => $keyfile );
skip($no_pem, $key->p, $key2->p);
skip($no_pem, $key->q, $key2->q);
skip($no_pem, $key->g, $key2->g);
skip($no_pem, $key->pub_key, $key2->pub_key);
skip($no_pem, $key->priv_key, $key2->priv_key);
# There's an option (not used here)
# to password-protect dsa-key.pem.
skip($no_pem, $key->write( Type => 'PEM', Filename => $keyfile));
$key2 = Crypt:

SA::Key->new( Type => 'PEM', Filename => $keyfile);
skip($no_pem, $key->p, $key2->p);
skip($no_pem, $key->q, $key2->q);
skip($no_pem, $key->g, $key2->g);
skip($no_pem, $key->pub_key, $key2->pub_key);
skip($no_pem, $key->priv_key, $key2->priv_key);
#unlink $keyfile;
print "Public: ", $key->pub_key, "\n";
print "Private: ", $key->priv_key, "\n";
print "Total: ", $key->write, "\n";
# To show that dsa-key.pem does hold both private
# and public key information :
my $pem = Convert:

EM->new(
Name => "DSA PRIVATE KEY",
ASN => qq(
DSAPrivateKey SEQUENCE {
version INTEGER,
p INTEGER,
q INTEGER,
g INTEGER,
pub_key INTEGER,
priv_key INTEGER
}
));
my $pkey = $pem->read(
Filename => $keyfile
);
my %deref = %$pkey;
for(keys(%deref)) {print "\n$_: $deref{$_}\n"}
print "\n";
my %d = %{$deref{DSAPrivateKey}};
for (keys(%d)) {print "$_ : $d{$_}\n"}