Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl OpenPGP

Status
Not open for further replies.

Cracer

Programmer
Jul 7, 2004
2
US
I am trying to use OpenPGP to encrypt a file so I can transmit using FTP. The program below does process and it outputs data but when they decrypt it the first 80 characters are decrypted and the rest of the file is still encrypted and not readable. Does anybody have any ideas as to what I might be doing wrong?
Thanks in advance for the help.

#!c:\perl\bin\perl.exe

use strict;
use warnings;
use Crypt::OpenPGP;

my $ring = Crypt::OpenPGP::KeyRing->new(
Data => qq^-----BEGIN PGP PUBLIC KEY BLOCK-----
“””this is where I have my public key”””
-----END PGP PUBLIC KEY BLOCK-----^ );


my $datafile = "test";
open( INFILE, "< $datafile".".txt" )
or die "Could not open txt file - $!";
my $plaintext = <INFILE>;
close INFILE;

$ring->read;
my $kb = $ring->find_keyblock_by_index(0);
my $cert = $kb->encrypting_key;

my $pgp = Crypt::OpenPGP->new;

my $ct = $pgp->encrypt( Key => $cert, Data => $plaintext )
or die "ERROR: " . $pgp->errstr;

open( OUTFILE, "> $datafile".".pgp" )
or die "Could not open file for encrypted data - $!";
print OUTFILE $ct;
close OUTFILE;
 
This will only read one line of the input file:

Code:
my $plaintext = <INFILE>;

You probably want to "slurp" the entire file by redefining $/ = '' before doing that? See man perlvar for details and example code. Or read it into an array instead?

Annihilannic.
 
I am still not understanding how all this openpgp works so I would really appreciate if someone can help me out. What I have to do is to take a text file and send it to another company. This other company gave me their public key. I can do a manual encryption using PGP desktop where I drag and drop the file and then add a passphrase. Then I logon to their ftp site and drop my xxxx.pgp file and then they can decrypt it. Above you can see my perl code where I add their KEY. Do I have to also add the passphrase or is that included in the key? I think Annihilannic is correct that I was only reading one record but I still do not have that part working either.

Thanks for you help
 
Is there any particular reason why you need to do this in PERL? If encrypting a file is all that you need to do then you can just use command-line utilities such as GNUPG (gpg). I just tried it myself for the first time and It's pretty straightforward once you have generated the keys. The passphrase is only required for the decryption step. The HOWTO docs I followed are here:


It might be a good exercise anyway to get to know the concepts before attempting to implement it in PERL.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top