Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Code hanging with Crypt OpenPGP

by kennethk (Abbot)
on Nov 11, 2013 at 21:45 UTC ( [id://1062059]=note: print w/replies, xml ) Need Help??


in reply to Code hanging with Crypt OpenPGP

One issue with your posted code, though I would be surprised if this is your issue, is that you are only pulling in one line of your plaintext - to pull a full file into a scalar, you should either set binmode or slurp (my $plaintext = do{local $/;<INFILE>}). Maybe you're encoding a null string and thus your output is much shorter than you expect?

It's possible, though seems unlikely, that you've got some strange characters in your opens (your posted code shouldn't suffer from that issue). 3-argument open will handle any escapes that that need handling. Swapping to Indirect Filehandles, that would be:

#!/usr/bin/perl use strict; use warnings; use File::Basename; use Crypt::OpenPGP; my $ring = Crypt::OpenPGP::KeyRing->new( Data => qq^-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.8 (SunOS) mQGiBFDr..... -----END PGP PUBLIC KEY BLOCK-----^ ); my $datafile = "original.csv"; # Get just name of the original file to name the new encrypted file. my ( $encrypted ) = fileparse( $datafile, '\.[^.]*' ); # $name, $path, + $suffix open( my $in, "<", $datafile ) or die "Could not open csv file - $!"; my $plaintext = do{local $/;<$in>}; close $in; $ring->read; my $kb = $ring->find_keyblock_by_index(0); my $cert = $kb->encrypting_key; my $pgp = Crypt::OpenPGP->new( Compat => 'GnuPG' ); my $ct = $pgp->encrypt( Key => $cert, Data => $plaintext, Armour => 1 +) or die "ERROR: " . $pgp->errstr; open( my $out, ">", "$encrypted.pgp" ) or die "Could not open file for + encrypted data - $!"; print $out $ct; close $out;

Also note "\.[^.]*" doesn't mean what you likely thought, though it still works for your purposes for the given code.

Lastly, if you have your literal key pasted in the block for $ring, your use of qq delimiters might result in some weird interpolation, though that would likely have gotten caught by strict.

Hope some of this helps...

Update: Fixed typo, as per below.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Code hanging with Crypt OpenPGP
by Anonymous Monk on Nov 12, 2013 at 15:53 UTC
    Shouldn't this line be :
    my $plaintext = do{local $/;<$in>};
    Instead of:
    my $plaintext = do{local $/;<INFILE>};
    Still hangs on this line for me:
    my $ct = $pgp->encrypt( Key => $cert, Data => $plaintext, Armour => 1 +) or die "ERROR: " . $pgp->errstr;
      What kind of debugging environment do you have set up? Since Crypt::OpenPGP is pure-Perl, you should be able to isolate the particular operation in encrypt that's causing trouble.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        I know, but I could not find any information on how to attach the public key, as an example this one:
        my $ring = Crypt::OpenPGP::KeyRing->new( Data => qq^-----BEGIN PGP PUB +LIC KEY BLOCK----- Version: PGP 6.5.1 mQGiBES0BisRBAD11RjxN2lk+2eKsVj +fJSrjkrbhoIRS1eduMb1Q1dzbqNAG1L/j Srd+T/dfWEqWu0LqBwgSK/P6a+cdnENSFj8 +LQOWD8zbrXbok2gFoIkF/L1O/7znC 5IwFPP+2FZe1kFyY36PKooot+okYG/G16HNAuU3 +CzxBYCuqkxqEDeU1kdwCg/1jI FrbO0cUrJrit/6NIyzDBCOMEANgBXtHtdrdtmwE35Hc +FO2sdywvvIOE+L1Wouwku ewK6/bpLGa1LnYu7T7xO+CziYqjp9SIGiSPmZjQQPXfBrEt +CvLMCTC/mdylOP6Sh hrnLWgPgAV5d7wpR1VUQCugDDZ9s/ExQuQEeD3dHt2dBBco5/+l +vdr/cXE06J3x2 fETgA/9dhSfdp4h4PIKYkgDvTGIpFp6JeKTVd5PnTu80JO9JkkSWtPr +yukDT/smq ACzyrCkexY0zdGgFJ5mmrK/S5KNyrUOw46noCj07k6/l4dl5CCM0K670Jpo +YijL5 rAUnfvvcozjhqKpgwalGDsIZ+pDNTL2BjREZulU6mTEiO9iAxLQMZnR0LXRlc3Q +t a2V5iQBOBBARAgAOBQJEtAYrBAsDAQICGQEACgkQ7PgWEnFENBZ0NACgl4QXiLg5 3z +iImum1iWQDTjSFBA8AoI9A3rEbeeaqUAHWQMn7x2wtofPi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tMaIkARgQYEQIABgUCRLQGLwAKCRDs+BYScUQ0FoObAKD1vGbfNCeu/CVvwsTr 9mz6n +UmeTgCeNO9P7OkUuySN6D2YTYbKvgIAEes= =+ksY -----END PGP PUBLIC KEY BLO +CK-----^ );
        to the my $ct = $pgp->encrypt() code. Since you mentioned that you got the code to work I'd like to see where the differences are. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1062059]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found