# ----------------------------------------------------------------------------
# vsPGP.pm
# Copyright (c) 2000 Jason M. Hinkle. All rights reserved. This module is
# free software; you may redistribute it and/or modify it under the same
# terms as Perl itself.
# For more information see: http://www.verysimple.com/scripts/
#
# LEGAL DISCLAIMER:
# This software is provided as-is. Use it at your own risk. The
# author takes no responsibility for any damages or losses directly
# or indirectly caused by this software.
# ----------------------------------------------------------------------------
package vspgp;
require 5.000;
$VERSION = "0.10";
$ID = "vspgp.pm";
$|++;
=head1 NAME
vspgp
=head1 SYNOPSIS
use vspgp;
my ($objpgp) = new vspgp;
# tell vsPGP about the PGP executable (these are the defaults, by the way,
# so if this matches your system, you don't need to set these.
# PgpTempDir needs to be writable by the account running the script.
$objpgp->Version(2.6.2); # (not currently used, but might be later)
$objpgp->PgpExePath("/usr/local/bin/pgpe");
$objpgp->PgpKeyPath("/home/demo/public_html/.pgp");
$objpgp->PgpTempDir("/home/demo/public_html/temp");
# Example 1: Encrypt
$objpgp->PublicKey("Mark");
$objpgp->PlainText($plain_text_message);
$objpgp->Encrypt;
my ($encrypted_message) = $objpgp->EncryptedText;
# Example 2: Decrypt
$objpgp->Password("mypassword");
$objpgp->EncryptedText($encrypted_message);
$objpgp->Decrypt;
# Example 3: EncryptSign
$objpgp->PublicKey("myfriend\@herhost.com");
$objpgp->PrivateKey("me\@myhost.com");
$objpgp->Password("mypassword");
$objpgp->PlainText($plain_text_message);
$objpgp->EncryptSign;
my ($encrypted_signed_message) = $objpgp->EncryptedText;
# Example 4: Sign
$objpgp->PrivateKey("me\@myhost.com");
$objpgp->Password("mypassword");
$objpgp->PlainText($plain_text_message);
$objpgp->Sign;
my ($signed_message) = $objpgp->SignedText;
# Encrypt, Decrypt, etc. will return 1 for success, 0 for fail. You
# can check the pgp results using $objpgp->Result. If there was an error
# then you can check $objpgp->ErrDescription for details.
=head1 DESCRIPTION
Object oriented interface to pgp. Requires pgp installed on the server.
Allows Perl scripts to encrypt, decrypt and sign messages using pgp
for the encyption. Tested with pgp 2.6.2 and pgp 6.5.8 on UNIX and
Windows.
=head1 USAGE
See http://www.verysimple.com/scripts/ for more information.
=head1 AUTHOR
Jason M. Hinkle
=head1 COPYRIGHT
Copyright (c) 2000 Jason M. Hinkle. All rights reserved.
This module is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
#_____________________________________________________________________________
sub new {
my $class = shift;
my $this = {
strPgpVersion => 6.5.8,
strPgpExePath => "/usr/local/bin/pgpe",
strPgpKeyPath => "/home/allthetraders/public_html/.pgp",
strPgpTempDir => "/home/allthetraders/public_html/temp",
strPgpTimeZone => "CST6CDT",
strPublicKey => "",
strPrivateKey => "",
strPassword => "",
strPlainText => "",
strEncryptedText => "",
strSignedText => "",
strResult => "",
strErrDescription => "",
strDebug =>"",
};
bless $this;
return $this;
}
# ###########################################################################
# PUBLIC PROPERTIES
#_____________________________________________________________________________
sub Version {
return $VERSION;
}
#_____________________________________________________________________________
sub PgpVersion {
return shift->_GetSetProperty("strPgpVersion",shift);
}
#_____________________________________________________________________________
sub PgpExePath {
return shift->_GetSetProperty("strPgpExePath",shift);
}
#_____________________________________________________________________________
sub PgpKeyPath {
return shift->_GetSetProperty("strPgpKeyPath",shift);
}
#_____________________________________________________________________________
sub PgpTempDir {
return shift->_GetSetProperty("strPgpTempDir",shift);
}
#_____________________________________________________________________________
sub PgpTimeZone {
return shift->_GetSetProperty("strPgpTimeZone",shift);
}
#_____________________________________________________________________________
sub PublicKey {
return shift->_GetSetProperty("strPublicKey",shift);
}
#_____________________________________________________________________________
sub PrivateKey {
return shift->_GetSetProperty("strPrivateKey",shift);
}
#_____________________________________________________________________________
sub Password {
return shift->_GetSetProperty("strPassword",shift);
}
#_____________________________________________________________________________
sub PlainText {
return shift->_GetSetProperty("strPlainText",shift);
}
#_____________________________________________________________________________
sub EncryptedText {
return shift->_GetSetProperty("strEncryptedText",shift);
}
#_____________________________________________________________________________
sub SignedText {
return shift->_GetSetProperty("strSignedText",shift);
}
#_____________________________________________________________________________
sub Result {
return shift->{'strResult'};
}
#_____________________________________________________________________________
sub ErrDescription {
return shift->{'strErrDescription'};
}
#_____________________________________________________________________________
sub ErrDebug {
return shift->{"strDebug"};
}
# ###########################################################################
# PRIVATE PROPERTIES
#_____________________________________________________________________________
sub _GetSetProperty {
# private fuction that is used by all the properties to get/set values
# if a parameter is sent in, then the property is set and true is returned.
# if no parameter is sent, then the current value is returned
my $this = shift;
my $fieldName = shift;
my $newValue = shift;
if (defined($newValue)) {
$this->{$fieldName} = $newValue;
} else {
return $this->{$fieldName};
}
return 1;
}
# ###########################################################################
# PUBLIC METHODS
#_____________________________________________________________________________
sub Encrypt {
my ($this) = shift;
my ($return_value) = 0;
# generate the command line
my ($pgp_command) = $this->{'strPgpExePath'}
. " -r ".$this->{'strPublicKey'}
. " -fat +batchmode +force";
# . " -fat +batchmode +force"
# . " \"" . " -r ".$this->{'strPublicKey'} . "\"";
$this->{'strEncryptedText'} = $this->DoPgpCommand($pgp_command,$this->{'strPlainText'});
# if there were results then everything went as planned
if ($this->{'strEncryptedText'} ne "") {
$return_value = 1;
}
return $return_value;
}
#_____________________________________________________________________________
sub Decrypt {
my ($this) = shift;
# assume fail
my ($return_value) = 0;
# generate the command line
my ($pgp_command) = $this->{'strPgpExePath'}
. " -f +batchmode +force";
$this->{'strPlainText'} = $this->DoPgpCommand($pgp_command,$this->{'strEncryptedText'});
# if there were results then everything went as planned
if ($this->{'strPlainText'} ne "") {
$return_value = 1;
}
return $return_value;
}
#_____________________________________________________________________________
sub EncryptSign {
my ($this) = shift;
my ($return_value) = 0;
# generate the command line
my ($pgp_command) = $this->{'strPgpExePath'}
. " -feast +batchmode +force"
. " \"" . $this->{'strPublicKey'} . "\""
. " -u \"" . $this->{'strPrivateKey'} . "\"";
$this->{'strEncryptedText'} = $this->DoPgpCommand($pgp_command,$this->{'strPlainText'});
# if there were results then everything went as planned
if ($this->{'strEncryptedText'} ne "") {
$return_value = 1;
}
return $return_value;
}
#_____________________________________________________________________________
sub Sign {
my ($this) = shift;
my ($return_value) = 0;
# generate the command line
my ($pgp_command) = $this->{'strPgpExePath'}
. " -fts +batchmode +force"
. " -u \"" . $this->{'strPrivateKey'} . "\"";
$this->{'strSignedText'} = $this->DoPgpCommand($pgp_command,$this->{'strPlainText'});
# if there were results then everything went as planned
if ($this->{'strSignedText'} ne "") {
$return_value = 1;
}
return $return_value;
}
#_____________________________________________________________________________
sub ErrClear {
$strErrDescription = "";
return 1;
}
#_____________________________________________________________________________
sub Reset {
my ($this) = shift;
my ($clear_key_info) = shift || "";
$this->{'strPlainText'} = "";
$this->{'strEncryptedText'} = "";
$this->{'strSignedText'} = "";
$this->{'strResult'} = "";
$this->{'strErrDescription'} = "";
$this->{'strDebug'}="";
if ($clear_key_info) {
$this->{'strPublicKey'} = "";
$this->{'strPrivateKey'} = "";
$this->{'strPassword'} = "";
}
return 1;
}
#_____________________________________________________________________________
sub DoPgpCommand {
my ($this) = shift;
my ($pgp_command) = shift || "";
my ($pgp_args) = shift || "";
my ($return_value) = "";
# get the filepath settings and set our temp file paths
my ($encrypted_file_path) = $this->{'strPgpTempDir'} . $$ . ".pgp";
my ($stdout_path) = $this->{'strPgpTempDir'} . $$ . ".txt";
$pgp_command .= " > " . $encrypted_file_path;
# UNCOMMENT TO DEBUG
my ($rtext) = "
";
$rtext .= "Encrypted file path $encrypted_file_path
\n";
$rtext .= "Stdout path $stdout_path
\n";
$rtext .= "PGP Command $pgp_command
\n";
$rtext .= "PGP Args $pgp_args
\n";
# set the environmental variables
$ENV{"TZ"} = $this->{'strPgpTimeZone'};
$ENV{"PGPPATH"} = $this->{'strPgpKeyPath'};
$ENV{"PGPPASS"} = $this->{'strPassword'};
# do our redirection magic
open (OLDOUT, ">&STDOUT");
open (OLDERR, ">&STDERR");
open (STDOUT, ">$stdout_path");
open (STDERR, ">>&STDOUT");
# execute PGP command
open (PGPCOMMAND, "|$pgp_command");
print PGPCOMMAND $pgp_args;
close (PGPCOMMAND);
# undo our redirection magic
close (STDOUT);
close (STDERR);
open (STDOUT, ">&OLDOUT");
open (STDERR, ">&OLDERR");
# open the encrypted file
open (ENCRYPTED, "$encrypted_file_path");
$return_value = join('',);
close (ENCRYPTED);
# open the redirect file to see what pgp sent to STDOUT & STDERR
open (PGPERROR, "$stdout_path");
$this->{'strResult'} = join('',);
close (PGPERROR);
# delete the temporary files (COMMENT TO DEBUG)
unlink($encrypted_file_path);
unlink($stdout_path);
# if there is no encrypted text, then something went wrong
if ($return_value eq "") {
$this->{'strErrDescription'} = "PGP Command Failed. Check Result Property For Details.";
$this->{'strErrDescription'} .= $rtext;
}
$ENV{"PGPPASS"} = "";
$this->{'strDebug'} .= $rtext;
return $return_value;
}
1;