Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Code hanging with Crypt OpenPGP by kennethk
in thread Code hanging with Crypt OpenPGP by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-16 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found