Ashish Gulhati


Crypt::PGP5 - An Object Oriented Interface to PGP5.


  use Crypt::PGP5;
  $pgp = new Crypt::PGP5;

  $pgp->secretkey ($keyid);        # Set ID of default secret key.
  $pgp->passphrase ($passphrase);  # Set passphrase.
  $pgp->armor ($boolean);          # Switch ASCII armoring on/off.
  $pgp->detach ($boolean);         # Switch detached signatures on/off.
  $pgp->encryptsafe ($boolean);    # Switch paranoid encryption on/off.
  $pgp->version ($versionstring);  # Set version string.
  $pgp->debug ($boolean);          # Switch debugging output on/off.

  $signed = $pgp->sign (@message);
  @recipients = $pgp->msginfo (@ciphertext);
  $ciphertext = $pgp->encrypt ([@recipients], @plaintext);
  ($signature, $plaintext) = $pgp->verify (@ciphertext);
  ($signature, $plaintext) = $pgp->dverify ([@signature], [@message]);

  (Most of the methods below will be encapsulated into the
  Crypt::PGP5::Key class by next release, bewarned!)

  $pgp->addkey ($keyring, @key);
  $pgp->delkey ($keyid);
  $pgp->disablekey ($keyid);
  $pgp->enablekey ($keyid);
  @keys = $pgp->keyinfo (@ids);
  $keystring = $pgp->extractkey ($userid, $keyring);
  $pgp->keypass ($keyid, $oldpasswd, $newpasswd);
  $status = $pgp->keygen 
    ($name, $email, $keytype, $keysize, $expire, $pass);


The Crypt::PGP5 module provides near complete access to PGP 5 functionality through an object oriented interface. It provides methods for encryption, decryption, signing, signature verification, key generation, key export and import, and most other key management functions.


new ()

Creates and returns a new Crypt::PGP5 object.


secretkey ()

Sets the SECRETKEY instance variable which may be a KeyID or a username. This is the ID of the default key to use for signing.

passphrase ()

Sets the PASSPHRASE instance variable, required for signing and decryption.

armor ()

Sets the ARMOR instance variable. If set to 0, Crypt::PGP5 doesn't ASCII armor its output. Else, it does. Default is to use ascii-armoring. I haven't tested this without ASCII armoring yet.

detach ()

Sets the DETACH instance variable. If set to 1, the sign method will produce detached signature certificates, else it won't. The default is to produce detached signatures.

encryptsafe ()

Sets the ENCRYPTSAFE instance variable. If set to 1, encryption will fail if trying to encrypt to a key which is not trusted. This is the default. Switch to 0 if you want to encrypt to untrusted keys.

version ()

Sets the VERSION instance variable which can be used to change the Version: string on the PGP output to whatever you like.

debug ()

Sets the DEBUG instance variable which causes the raw output of Crypt::PGP5's interaction with the PGP binary to be dumped to STDOUT.


sign (@message)

Signs @message with the secret key specified with secretkey () and returns the result as a string.

verify (@message)

Verifies or decrypts the message in @message and returns the Crypt::PGP5::Signature object corresponding to the signature on the message (if any, or undef otherwise), and a string containing the decrypted message.

dverify ([@message], [@signature])

Verifies the detactched signature @signature on @message and returns a Crypt::PGP5::Signature object corresponding to the signature on the message, along with a string containing the plaintext message.

msginfo (@ciphertext)

Returns a list of the recipient key IDs that @ciphertext is encrypted to.

encrypt ([$keyid1, $keyid2...], @plaintext)

Encrypts @plaintext with the public keys of the recipients listed in the arrayref passed as the first argument and returns the result in a string, or undef if there was an error while processing. Returns ciphertext if the message could be encrypted to at least one of the recipients.

addkey ($key, $pretend)

Adds the keys given in $key to the user's key ring and returns a list of Crypt::PGP::Key objects corresponding to the keys that were added. If $pretend is true, it pretends to add the key and creates the key object, but doesn't actually perform the key addition.

delkey ($keyid)

Deletes the key with $keyid from the user's key ring.

disablekey ($keyid)

Disables the key with $keyid.

enablekey ($keyid)

Enables the key with $keyid.

keyinfo (@keyids)

Returns an array of Crypt::PGP5::Key objects corresponding to the keyids listed in @keyids.

parsekeys (@keylist)

Parses a raw PGP formatted key listing and returns a list of Crypt::PGP5::Key objects.

keypass ($keyid, $oldpass, $newpass)

Change the passphrase for a key. Returns true if the passphrase change succeeded, false if not.

extractkey ($userid, $keyring)

Extracts the key for $userid from $keyring and returns the result. The $keyring argument is optional and defaults to the public keyring set with pubring ().

keygen ($name, $email, $keytype, $keysize, $expire, $pass)

Creates a new keypair with the parameters specified. $keytype may be one of 'RSA' or 'DSS'. $keysize can be any of 768, 1024, 2048, 3072 or 4096 for DSS keys, and 768, 1024 or 2048 for RSA type keys. Returns undef if there was an error, otherwise returns a filehandle that reports the progress of the key generation process similar to the way PGP does. The key generation is not complete till you read an EOF from the returned filehandle.


  • Error checking needs work.

  • Some key manipulation functions are missing.

  • May not work with versions of PGP other than PGPfreeware 5.0i.

  • The method call interface is subject to change in future versions, specifically, key manipulation methods will be encapsulated into the Crypt::PGP5::Key class in a future version.

  • The current implementation will probably eat up all your RAM if you try to operate on huge messages. In future versions, this will be addressed by reading from and returning filehandles, rather than using in-core data.


Crypt::PGP5 is Copyright (c) 1999-2000 Ashish Gulhati <>. All Rights Reserved.


Thanks to Barkha for inspiration and lots of laughs; to Rex Rogers at Laissez Faire City for putting together a great environment to hack on freedom technologies; and of-course, to Phil Zimmerman, Larry Wall, Richard Stallman, and Linus Torvalds.


This code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

It would be nice if you would mail your patches to me, and I would love to hear about projects that make use of this module.


This is free software. If it breaks, you own both parts.