boat73 has asked for the wisdom of the Perl Monks concerning the following question:

Could someone be so kind as to direct me to a start to finish example of how to use Passphrasecallback to decrypt data using a secret key?
  • Comment on Crypt::OpenPGP PassphraseCallback example please

Replies are listed 'Best First'.
Re: Crypt::OpenPGP PassphraseCallback example please
by pbeckingham (Parson) on Mar 16, 2004 at 21:58 UTC

    Post your code thus far. The Crypt::OpenPGP POD does have example code. What have you tried?

      Here is what I am doing thus far, I appologize if I am long winded.
      #### creating private/public key files, seems to work fine
      use Crypt::OpenPGP; $pgp = Crypt::OpenPGP->new (); %attrib = ( 'Type' => 'RSA', 'Size' => '1024', 'Identity' => 'Garett L Holmes gaholmes@hotmail.com', 'Passphrase' => 'password', 'Cipher' => 'DES3', 'Verbosity' => '1', 'Compat' => 'PGP5', ); ($pubkey, $privkey) = $pgp->keygen( %attrib ) or die $pgp->errstr; $public = $pubkey->save; open(PUBLIC,">test.public") or die "COULD NOT OPEN pub\n"; print PUBLIC $public; close(PUBLIC); $private = $privkey->save; open(PRIVATE,">test.private") or die "COULD NOT OPEN priv\n"; print PRIVATE $private; close(PRIVATE);
      #### Then I encrypt the data using the private key, seems to work
      use Crypt::OpenPGP; use Crypt::OpenPGP::KeyRing; $pgp = Crypt::OpenPGP->new(PubRing => 'test.public'); $crypt = $pgp->encrypt( Compat => 'PGP5', Data => 'squeamish ossifrage', Recipients => 'gaholmes\@hotmail.com', Armour => 1, ); defined($crypt) ? print $crypt : die $pgp->errstr;
      This is where I seem to lose it. I now want to decrypt $crypt using the secret key. I tried this this but know now that I am not understanding the passphrasecallback. I have read the documentation but can't seem to grasp it. After all the time I spent getting he modules installed and functioning on win32 I think I have brain burn. The module looks like just what I need and I would appreciate any assistance with understanding it. Thanks in advance.
      boat73
      $pgp = Crypt::OpenPGP->new ( "SecRing" => './pgp/test.private' )or die Crypt::OpenPGP->errstr; my $plaintext = $pgp->decrypt( "Data" => $crypt, "PassphraseCallback" => \&passphrase_cb ); die "decryption failed: ", $pgp->errstr unless $plaintext; print "PLAINTEXT IS $plaintext\n"; sub passphrase_cb { if (my $cert = $_[0]) { printf "Enter passphrase for secret key %s: ", $cert->key_id_hex; } else { print "Enter passphrase: "; } } &passphrase_cb(password); sub passphrase_cb { if (my $cert = $_[0]) { printf "Enter passphrase for secret key %s: ", $cert->key_id_hex; } else { print "Enter passphrase: "; } }

      Edited by Chady -- fixed code tags.

        I think the examples given are a bit misleading; the key part is the definition of the PassphraseCallback argument in the docs, in particular the last part:

            In either case, the callback routine should return the
            passphrase, a scalar string.
        

        The various examples sometimes leave off the final call to _prompt(), and since it is an undocumented internal routine it isn't obvious that this is doing the guts of the work: showing a prompt, setting noecho, getting the text from the user's input, and returning the resulting string.

        I'd suggest looking at the code for _prompt() in the module (it's in the top-level Crypt/OpenPGP.pm) to see what needs doing, and then (probably) avoid using ths undocumented interface directly by copying what you need out of there.

        Here's some similar code I use in a less critical situation, which copes with the possibility that Term::Readkey isn't installed by letting the password be entered unhidden:

        sub getpass { my $prompt = shift; local $| = 1; print "$prompt: "; eval { require Term::ReadKey }; my $haveterm = !$@; Term::ReadKey::ReadMode(2) if $haveterm; # turn on noecho my $pass = <STDIN>; chomp $pass; Term::ReadKey::ReadMode(0) if $haveterm; # restore print "\n"; return $pass; }

        Hugo

Re: Crypt::OpenPGP PassphraseCallback example please
by tachyon (Chancellor) on Mar 16, 2004 at 22:45 UTC
    RTFD as suggested. Does the _default_passphrase_cb() function it contains not work? If so what are the errors?

    cheers

    tachyon

      I guess I am just having trouble following _default_passphrase_cb(). The examples for me anyway are difficult to understand.