Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Crypt::GPG temp files?

by ksublondie (Friar)
on Mar 28, 2013 at 19:14 UTC ( [id://1026025]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks!

I've been searching in vain to get up & going with PGP/GPG encrypting/decrypting with no real success. My current attempt is using Crypt::GPG which hasn't been giving me consistent results. My automated script needs to decrypt two files and encrypt a third. Here's my code:

my $gpg=Crypt::GPG->new(); $gpg->gpgbin($CONFIG{gpgbin}); $gpg->gpgopts('--lock-multiple') ; $gpg->secretkey($CONFIG{secretkeyuserid}); $gpg->passphrase($CONFIG{password}); $gpg->encryptsafe(0); $gpg->debug(1); print "decrypting\n"; open DECRYPT ,"<$CONFIG{decryptfile}"; my @decr=<DECRYPT>; close DECRYPT; my ($message,$sig)=$gpg->decrypt(\@decr) or die "Error decrypting\n"; print "message1=$message\n\n"; open DECRYPT ,"<$CONFIG{decryptfile}"; my @decr2=<DECRYPT>; close DECRYPT; my ($message2,$sig2)=$gpg->decrypt(\@decr2) or die "Error decrypting\n +"; print "message2=$message2\n\n"; open ENCRYPT, "<$CONFIG{encryptfile}"; my @encr=<ENCRYPT>; close ENCRYPT; my $encryption=$gpg->encrypt(join('',@encr),$CONFIG{icskeyuserid},'-si +gn') or die "error encrypting\n";
Sometimes it decrypts the first file correctly, sometimes not. But it always fails on the 2nd decryption (even setting to the same file) and hasn't yet encrypted my 3rd file successfully. Plus, sometimes (but not always) it prompts me for my passphrase even though I have it set in the code. Here's the debug output (I think I got all of it):
F:\scripts>GnuPGInterface.bat Prototype mismatch: sub Socket::IPPROTO_TCP () vs none at f:/perl/lib/ +Socket.pm line 395. Prototype mismatch: sub Socket::TCP_NODELAY () vs none at f:/perl/lib/ +Socket.pm line 395. Thu Mar 28 13:38:37 2013 constructor decrypting IPC::Run 0000 [#1(6068)]: ****** harnessing ***** IPC::Run 0000 [#1(6068)]: parsing [ 'gpg', '--no-secmem-warning', '--l +ock-multip le', '--compress-algo', '1', '--cipher-algo', 'cast5', '--force-v3-sig +s', '--com ment', 'Crypt::GPG v1.63', '--marginals-needed', '3', '--check-trustdb +' ] IPC::Run 0000 [#1(6068)]: parsing SCALAR IPC::Run 0000 [#1(6068)]: parsing SCALAR IPC::Run 0000 [#1(6068)]: parsing SCALAR IPC::Run 0000 [#1(6068)]: parsing IPC::Run::Timer IPC::Run 0000 [#1(6068)]: ** starting IPC::Run 0000 [#1(6068)]: timer #1 state set to 1 IPC::Run 0000 [#1(6068)]: timer #1 start_time set to 1364495917 IPC::Run 0000 [#1(6068)]: timer #1 started at 1364495917, with interva +l 10, end_ time 1364495928 IPC::Run 0002 [#1(6068)]: _spawn_pumper pid = 5940 IPC::Run 0002 [#1(6068)]: _spawn_pumper pid = 3768 IPC::Run 0002 [#1(6068)]: _spawn_pumper pid = 4352 IPC: :Run 0002 [#1(6068)]: spawning 'C:\Program Files\GNU\GnuPG\pub\gpg.EXE +' '--no-se cmem-warning' '--lock-multiple' '--compress-algo' '1' '--cipher-algo' +'cast5' '- -force-v3-sigs' '--comment' 'Crypt::GPG v1.63' '--marginals-needed' '3 +' '--check -trustdb' IPC::Run 0002 [#1(6068)]: cmd line: "C:\Program Files\GNU\GnuPG\pub\gp +g.EXE" --n o-secmem-warning --lock-multiple --compress-algo 1 --cipher-algo cast5 + --force-v 3-sigs --comment "Crypt::GPG v1.63" --marginals-needed 3 --check-trust +db IPC::Run 0000 [#1(6068)]: parsing SCALAR IPC::Run 0000 [#1(6068)]: parsing SCALAR IPC::Run 0000 [#1(6068)]: parsing SCALAR IPC::Run 0000 [#1(6068)]: parsing IPC::Run::Timer IPC::Run 0000 [#1(6068)]: ** starting IPC::Run 0000 [#1(6068)]: timer #1 state set to 1 IPC::Run 0000 [#1(6068)]: timer #1 start_time set to 1364495917 IPC::Run 0000 [#1(6068)]: timer #1 started at 1364495917, with interva +l 10, end_ time 1364495928 IPC::Run 0002 [#1(6068)]: _spawn_pumper pid = 5940 IPC::Run 0002 [#1(6068)]: _spawn_pumper pid = 3768 IPC::Run 0002 [#1(6068)]: _spawn_pumper pid = 4352 IPC: :Run 0002 [#1(6068)]: spawning 'C:\Program Files\GNU\GnuPG\pub\gpg.EXE +' '--no-se cmem-warning' '--lock-multiple' '--compress-algo' '1' '--cipher-algo' +'cast5' '- -force-v3-sigs' '--comment' 'Crypt::GPG v1.63' '--marginals-needed' '3 +' '--check -trustdb' IPC::Run 0002 [#1(6068)]: cmd line: "C:\Program Files\GNU\GnuPG\pub\gp +g.EXE" --n o-secmem-warning --lock-multiple --compress-algo 1 --cipher-algo cast5 + --force-v 3-sigs --comment "Crypt::GPG v1.63" --marginals-needed 3 --check-trust +db IPC::Run 0002 [#1(6068)]: spawn() = 2396 IPC::Run 0002 [#1(6068)]: ** finishing IPC::Run 0002 [#1(6068)]: read( 13 ) = 137 chars 'gpg: 3 marginal(s) n +eeded, 1 c omplete(s) needed, PGP trust model IPC::Run 0002 [#1(6068)]: gpg: depth: 0 valid: 2 signed: 0 trus +t: 0-, 0q, 0n, 0m, 0f, 2u IPC::Run 0002 [#1(6068)]: ' IPC::Run 0002 [#1(6068)]: read( 13 ) = 0 but true chars '' IPC::Run 0002 [#1(6068)]: read( 11 ) = 0 but true chars '' IPC::Run 0002 [#1(6068)]: kid 1 (2396) exited IPC::Run 0002 [#2(6068)]: ****** harnessing ***** IPC::Run 0002 [#2(6068)]: parsing [ 'gpg', '--no-secmem-warning', '--l +ock-multip le', '--compress-algo', '1', '--cipher-algo', 'cast5', '--force-v3-sig +s', '--com ment', 'Crypt::GPG v1.63', '--marginals-needed', '3', '--status-fd', ' +1', '--com mand-fd', '0', '--yes', '--no-tty', '--decrypt', '-o', 'C:\DOCUME~1\AD +MINI~1.001 \LOCALS~1\Temp\file41PcCC.dat', 'C:\DOCUME~1\ADMINI~1.001\LOCALS~1\Tem +p\filemwim bD.dat' ] IPC::Run 0002 [#2(6068)]: parsing SCALAR IPC::Run 0002 [#2(6068)]: parsing SCALAR IPC::Run 0002 [#2(6068)]: parsing SCALAR IPC::Run 0002 [#2(6068)]: parsing IPC::Run::Timer IPC::Run 0002 [#2(6068)]: ** starting IPC::Run 0002 [#2(6068)]: timer #2 state set to 1 IPC::Run 0002 [#2(6068)]: timer #2 start_time set to 1364495919 IPC::Run 0002 [#2(6068)]: timer #2 started at 1364495919, with interva +l 30, end_ time 1364495950 IPC::Run 0002 [#2(6068)]: 'gpg' found in cache: 'C:\Program Files\GNU\ +GnuPG\pub\ gpg.EXE' IPC::Run 0002 [#2(6068)]: _spawn_pumper pid = 4932 IPC::Run 0002 [#2(6068)]: _spawn_pumper pid = 4112 IPC::Run 0002 [#2(6068)]: _spawn_pumper pid = 4232 IPC::Run 0002 [#2(6068)]: spawning 'C:\Program Files\GNU\GnuPG\pub\gpg +.EXE' '--n o-secmem-warning' '--lock-multiple' '--compress-algo' '1' '--cipher-al +go' 'cast5 ' '--force-v3-sigs' '--comment' 'Crypt::GPG v1.63' '--marginals-needed +' '3' '--s tatus-fd' '1' '--command-fd' '0' '--yes' '--no-tty' '--decrypt' '-o' ' +C:\DOCUME~ 1\ADMINI~1.001\LOCALS~1\Temp\file41PcCC.dat' 'C:\DOCUME~1\ADMINI~1.001 +\LOCALS~1\ Temp\filemwimbD.dat' IPC::Run 0002 [#2(6068)]: cmd line: "C:\Program Files\GNU\GnuPG\pub\gp +g.EXE" --n o-secmem-warning --lock-multiple --compress-algo 1 --cipher-algo cast5 + --force-v 3-sigs --comment "Crypt::GPG v1.63" --marginals-needed 3 --status-fd 1 + --command -fd 0 --yes --no-tty --decrypt -o C:\DOCUME~1\ADMINI~1.001\LOCALS~1\Te +mp\file41P cCC.dat C:\DOCUME~1\ADMINI~1.001\LOCALS~1\Temp\filemwimbD.dat IPC::Run 0002 [#2(6068)]: spawn() = 5944 IPC::Run 0002 [#2(6068)]: ** pumping IPC::Run 0003 [#2(6068)]: read( 11 ) = 634 chars '[GNUPG:] ENC_TO F45A +2C6EBC18DD 3E 1 0 IPC::Run 0003 [#2(6068)]: [GNUPG:] USERID_HINT F45A2C6EBC18DD3E My Ema +il < user@email.com> IPC::Run 0003 [#2(6068)]: [GNUPG:] NEED_PASSPHRASE F45A2C6EBC18DD3E F4 +5A2C6EBC18 DD3E 1 0 IPC::Run 0003 [#2(6068)]: [GNUPG:] GOOD_PASSPHRASE IPC::Run 0003 [#2(6068)]: [GNUPG:] BEGIN_DECRYPTION IPC::Run 0003 [#2(6068)]: [GNUPG:] PLAINTEXT 62 1364337437 icsdciintac +c.txt IPC::Run 0003 [#2(6068)]: [GNUPG:] PLAINTEXT_LENGTH 15 IPC::Run 0003 [#2(6068)]: [GNUPG:] SIG_ID Pn8agaH3UGp3TS4X2q9Klloza6w +2013-03-26 1364337437 IPC::Run 0003 [#2(6068)]: [GNUPG:] GOODSIG 2E172E823737DF17 Their Emai +l <them@email.com> IPC::Run 0003 [#2(6068)]: [GNUPG:] VALIDSIG F1E833FCA06B0F9D3EB5757F2E +172E823737 DF17 2013-03-26 1364337437 0 4 0 17 2 00 F1E833FCA06B0F9D3EB5757F2E172 +E823737DF1 7 IPC::Run 0003 [#2(6068)]: ' IPC::Run 0003 [#2(6068)]: ** pumping IPC::Run 0003 [#2(6068)]: write( 9, ' IPC::Run 0003 [#2(6068)]: ' ) = 1 IPC::Run 0003 [#2(6068)]: read( 11 ) = 78 chars '[GNUPG:] TRUST_ULTIMA +TE IPC::Run 0003 [#2(6068)]: [GNUPG:] DECRYPTION_FAILED IPC::Run 0003 [#2(6068)]: [GNUPG:] END_DECRYPTION IPC::Run 0003 [#2(6068)]: ' IPC::Run 0003 [#2(6068)]: ** finishing IPC::Run 0003 [#2(6068)]: read( 11 ) = 0 but true chars '' IPC::Run 0003 [#2(6068)]: read( 13 ) = 394 chars 'gpg: encrypted with +2048-bit R SA key, ID BC18DD3E, created 2013-03-19 IPC::Run 0003 [#2(6068)]: "My email <user@email.com>" IPC::Run 0003 [#2(6068)]: gpg: Signature made 03/26/13 17:37:17 Centra +l Daylight Time using DSA key ID 3737DF17 IPC::Run 0003 [#2(6068)]: gpg: Good signature from "Their email <them@ +email.com>" IPC::Run 0003 [#2(6068)]: gpg: mdc_packet with invalid encoding IPC::Run 0003 [#2(6068)]: gpg: decryption failed: Invalid packet IPC::Run 0003 [#2(6068)]: ' IPC::Run 0003 [#2(6068)]: read( 13 ) = 0 but true chars '' IPC::Run 0003 [#2(6068)]: kid 1 (5944) exited message1= IPC::Run 0003 [#3(6068)]: ****** harnessing ***** IPC::Run 0003 [#3(6068)]: parsing [ 'gpg', '--no-secmem-warning', '--l +ock-multip le', '--compress-algo', '1', '--cipher-algo', 'cast5', '--force-v3-sig +s', '--def ault-key', 'FNB Hutchinson', '--sign', '-a', '--comment', 'Crypt::GPG +v1.63', '- -no-tty', '--status-fd', '1', '--command-fd', '0', '-o', 'C:\DOCUME~1\ +ADMINI~1.0 01\LOCALS~1\Temp\file5z5BWf.dat', '-r', 'Their Email', '--encrypt', 'C +:\DOCUME~1\AD MINI~1.001\LOCALS~1\Temp\fileOXYiTL.dat' ] IPC::Run 0003 [#3(6068)]: parsing SCALAR IPC::Run 0003 [#3(6068)]: parsing SCALAR IPC::Run 0003 [#3(6068)]: parsing SCALAR IPC::Run 0003 [#3(6068)]: parsing IPC::Run::Timer IPC::Run 0003 [#3(6068)]: ** starting IPC::Run 0003 [#3(6068)]: timer #3 state set to 1 IPC::Run 0003 [#3(6068)]: timer #3 start_time set to 1364495920 IPC::Run 0003 [#3(6068)]: timer #3 started at 1364495920, with interva +l 30, end_ time 1364495951 IPC::Run 0003 [#3(6068)]: 'gpg' found in cache: 'C:\Program Files\GNU\ +GnuPG\pub\ gpg.EXE' IPC::Run 0003 [#3(6068)]: _spawn_pumper pid = 4276 IPC::Run 0003 [#3(6068)]: _spawn_pumper pid = 4624 IPC::Run 0003 [#3(6068)]: _spawn_pumper pid = 988 IPC::Run 0003 [#3(6068)]: spawning 'C:\Program Files\GNU\GnuPG\pub\gpg +.EXE' '--n o-secmem-warning' '--lock-multiple' '--compress-algo' '1' '--cipher-al +go' 'cast5 ' '--force-v3-sigs' '--default-key' 'My Email' '--sign' '-a' '--commen +t' ' Crypt::GPG v1.63' '--no-tty' '--status-fd' '1' '--command-fd' '0' '-o' + 'C:\DOCUM E~1\ADMINI~1.001\LOCALS~1\Temp\file5z5BWf.dat' '-r' 'Their Email' '--e +ncrypt' 'C:\D OCUME~1\ADMINI~1.001\LOCALS~1\Temp\fileOXYiTL.dat' IPC::Run 0003 [#3(6068)]: cmd line: "C:\Program Files\GNU\GnuPG\pub\gp +g.EXE" --n o-secmem-warning --lock-multiple --compress-algo 1 --cipher-algo cast5 + --force-v 3-sigs --default-key "My Email" --sign -a --comment "Crypt::GPG v1.63" + --n o-tty --status-fd 1 --command-fd 0 -o C:\DOCUME~1\ADMINI~1.001\LOCALS~ +1\Temp\fil e5z5BWf.dat -r Their Email --encrypt C:\DOCUME~1\ADMINI~1.001\LOCALS~1 +\Temp\fileOXY iTL.dat IPC::Run 0003 [#3(6068)]: spawn() = 916 IPC::Run 0003 [#3(6068)]: ** pumping IPC::Run 0003 [#3(6068)]: read( 11 ) = 210 chars '[GNUPG:] USERID_HINT + F45A2C6EB C18DD3E My Email <user@email.com> IPC::Run 0003 [#3(6068)]: [GNUPG:] NEED_PASSPHRASE F45A2C6EBC18DD3E F4 +5A2C6EBC18 DD3E 1 0 IPC::Run 0003 [#3(6068)]: [GNUPG:] GOOD_PASSPHRASE IPC::Run 0003 [#3(6068)]: [GNUPG:] GET_BOOL openfile.overwrite.okay IPC::Run 0003 [#3(6068)]: ' IPC::Run 0003 [#3(6068)]: ** finishing IPC::Run 0003 [#3(6068)]: write( 9, 'Y IPC::Run 0003 [#3(6068)]: ' ) = 2 IPC::Run 0003 [#3(6068)]: read( 11 ) = 17 chars '[GNUPG:] GOT_IT IPC::Run 0003 [#3(6068)]: ' IPC::Run 0003 [#3(6068)]: read( 11 ) = 0 but true chars '' IPC::Run 0003 [#3(6068)]: read( 13 ) = 220 chars 'gpg: can't create `C +:\\DOCUME~ 1\\ADMINI~1.001\\LOCALS~1\\Temp\\file5z5BWf.dat': No such file or dire +ctory IPC::Run 0003 [#3(6068)]: gpg: C:\\DOCUME~1\\ADMINI~1.001\\LOCALS~1\\T +emp\\fileO XYiTL.dat: sign+encrypt failed: No such file or directory IPC::Run 0003 [#3(6068)]: ' IPC::Run 0003 [#3(6068)]: read( 13 ) = 0 but true chars '' IPC::Run 0003 [#3(6068)]: kid 1 (916) exited error encrypting
I can successfully do what I need to through the gpg command line arguments and again through a script using system calls using those same commands. I'm just about ready to scrap the modules and use my system calls instead. Not ideal.

Replies are listed 'Best First'.
Re: Crypt::GPG temp files?
by Anonymous Monk on Jul 19, 2013 at 03:11 UTC

    I was having a similar problem (although with signing rather than encrypt/decrypt). Crypt::GPG is a little naughty within sub sign() around line 74 I have:

    my $message = join ('', @_); $message .= "\n" unless $message =~ /\n$/s; $message =~ s/\n/\r\n/sg; print $tmpfh $message; close $tmpfh;

    This is incredibly bad behaviour that the module modifies $message prior to passing to GPG. As we don't get $message passed back undoubtedly the copy you're using is different to the one the module passes to GPG.

    Upon me commenting out those two regexes sign() now actually works for me!

    The above perhaps may not be helpful seeing you're using encrypt() and decrypt(), but I thought I'd post it anyway..

    It looks like this particular problem could have been resolved in module revision 1.57

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found