Subject: | Can't encrypt or sign an empty message |
The encrypt and sign methods in Crypt::OpenPGP version 1.03 will not encrypt an empty string or an empty file. I have attached a program, demo.pl, to demonstrate the problem. To use it, change the line,
my $keyid = '<enter-your-16-hex-digit-keyid>';
to specify your /public/private key pair.
I will also attach a patch which I believe fixes the problem.
#!/usr/local/bin/perl -w
# Demonstrate problems encrypting an empty string or file
# using the Crypt::OpenPGP Perl module
use Crypt::OpenPGP;
use Term::ReadKey;
my $pgp = Crypt::OpenPGP->new;
my $keyid = '<enter-your-16-hex-digit-keyid>';
my ($ciphertext, $signature);
# Test encryption of a null string:
$ciphertext = $pgp->encrypt( Data => ''
, Recipients => $keyid
, Armour => 1
);
if (defined $ciphertext) {
print "\nEncryption of '':\n$ciphertext";
}
else {
warn "Encrytion of '' failed: ", $pgp->errstr;
}
# Test encryption of a null file:
$ciphertext = $pgp->encrypt( Filename => '/dev/null'
, Recipients => $keyid
, Armour => 1
);
if (defined $ciphertext) {
print "\nEncryption of /dev/null:\n$ciphertext";
}
else {
warn "Encryption of /dev/null failed: ", $pgp->errstr;
}
# Test signing of a null string:
$signature = $pgp->sign( Data => ''
, KeyID => $keyid
, Detach => 1
, Armour => 1
, PassphraseCallback => \&passphrase_cb
);
if (defined $signature ) {
print "\nSignature of '':\n$signature";
}
else {
warn "Signing of '' failed: ", $pgp->errstr;
}
# Test signing of a null file:
$signature = $pgp->sign( Filename => '/dev/null'
, KeyID => $keyid
, Detach => 1
, Armour => 1
, PassphraseCallback => \&passphrase_cb
);
if (defined $signature ) {
print "\nSignature of /dev/null:\n$signature";
}
else {
warn "Signing of /dev/null failed: ", $pgp->errstr;
}
# -----------------------------------------------------------------------
#
# The following routines are copied from bin/pgplet
#
sub passphrase_cb {
my($cert) = @_;
my $prompt;
if ($cert) {
$prompt = sprintf qq(
You need a passphrase to unlock the secret key for
user "%s".
%d-bit %s key, ID %s
Enter passphrase: ), $cert->uid,
$cert->key->size,
$cert->key->alg,
substr($cert->key_id_hex, -8, 8);
} else {
$prompt = "Enter passphrase: ";
}
prompt($prompt, '', 1);
}
sub prompt {
my($prompt, $def, $noecho) = @_;
print STDERR $prompt . ($def ? "[$def] " : "");
if ($noecho) {
ReadMode('noecho');
}
chomp(my $ans = ReadLine(0));
ReadMode('restore');
print STDERR "\n";
defined $ans && $ans ne '' ? $ans : $def;
}