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

Greetings, Perl Monks! I am working on a small perl program that gives me an interface to Crypt::OpenPGP. It successfully generates encrypted text in Compat=PGP5 mode, which I can decrypt using the PGP Freeware client. However, my program cannot decrypt the very same cipher text using the Crypt::OpenPGP module. The error I get on decryption is:
encrypt: datasize not multiple of blocksize (16 bytes) at /usr/local/l +ib/perl5/site_perl/5.8.3/Crypt/OpenPGP/CFB.pm line 59.
Here is a small test case that attempts to do the decrypt and produces the error:
#!/usr/local/bin/perl -w use Crypt::OpenPGP; # slurp in file of ciphertext: open( my $fh, "/home/nobody/tools/crypt/cipher.txt") or die "flaming + death\n"; my $Data = do { local( $/ ) ; <$fh> } ; my $pgp = Crypt::OpenPGP->new ( "SecRing" => "/home/nobody/.keys/secring.skr" ); my $plaintext = $pgp->decrypt ( "Compat" => "PGP5", "Data" => $Data, "Passphrase" => "no, you cant have it" ); die "Decryption Failed: ", $pgp->errstr unless $plaintext; print "plain text:\n" . $plaintext . "\n";
The code that did the crypting looks like this:
my $pgp = Crypt::OpenPGP->new ( "PubRing" => $PubRing ); my $ciphertext = $pgp->encrypt ( "Compat" => $Compat, "Data" => $Data, "Recipients" => @recipients_list, "Armour" => $Armour, ); die "Encryption failed: ", $pgp->errstr unless $ciphertext; return $ciphertext;
Stuff I've Tried So Far: system/perl info: Any insight is greatly appreciated ^_~

Replies are listed 'Best First'.
Re: Crypt::OpenPGP blocksize problem
by zentara (Cardinal) on Mar 11, 2004 at 15:07 UTC
    #this sub makes all data blocksize of 16 bytes. sub get16 { my $data = shift; print "data=$data\n"; return "\0" x ( 16 - length($data)%16 ) . $data; }

    I'm not really a human, but I play one on earth. flash japh
      I, Frank, made the original request for help, but for some reason i wasn't actually logged in. Anyway, um, the block above does not help this problem at all. decrypt throws an error that no data packets were found. I utilized the above function by sending my scalar of all the crypt data as the paramter to get16 ( $Data = get16($Data) ). zentara, should the abover function be used in a line by line fashion? or is there a bug in it? I don't really understand what it does. Frank
        Don't know what your problem actually is. But I remember your error was "data not in block size 16". What the sub does is pad the string with null characters, from 1 to 15 depending, so that the data string is in nice even blocks of 16 bytes. You will see that after you decrypt, the data will be a little bigger because of the padding, but it won't affect the file. If you want to strip the file after decrypting:
        #To strip any padding after decryption: my $plaintext = $cipher->decrypt($encrypted); $plaintext =~ s/^\0+//;
        Why you are not getting the results you want is not known to me. Maybe try posting again with some code?

        I'm not really a human, but I play one on earth. flash japh