Solved

PERL script for decription not working

Posted on 2004-04-28
18
613 Views
Last Modified: 2011-10-03
Can i get a known working PERLscript to perform decryption / encryption using "gpg" on a windows machine.  

heres is what does not work.
use GnuPG qw(:algo);
my $gpg=new GnuPG(gnupg_path=>"C:\\gnupg");
$gpg->decrypt( ciphertext=>"/gensrvnt/ftp/test/28942478.intchg", output=>"/gensrvnt/ftp/test/perltest", passphrase=>"#######");

the error i get is

The vendor has not defined Fcntl macro F_SETFD, used at C:/Perl/lib/GnuPG.pm line 203

thanks
0
Comment
Question by:shrek2
  • 7
  • 5
  • 2
18 Comments
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
<< Unlike unix, Windows offers only blocking io on pipes. >>
(See http://lists.auriga.wearlab.de/pipermail/dillo-dev/2003-August/001051.html )

The lines causing problem in GnuPG.pm on windows are

# Keep pipe open after close
    fcntl( $fd, F_SETFD, 0 )
      or croak "error removing close on exec flag: $!\n" ;
    fcntl( $wfd, F_SETFD, 0 )
      or croak "error removing close on exec flag: $!\n" ;

Try to comment them for windows.
0
 

Author Comment

by:shrek2
Comment Utility
When I comment out the line I get this error:

Wrong response from gnupg <expected SHM_INFO>:

This error comes from gnupg.pm and my perl script seems to just be hanging there.  The DOS prompt does not return so I did a ^C to stop it.

0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
In this case, I cannot be more help, didn't find a module written for windows.
0
 

Author Comment

by:shrek2
Comment Utility
I am new to this site.  you are the only one to have offered assistance on this matter- does this mean no one else is wanting to get the 500 points? or does it mean that i have to give you "Mercnatilum" points for efforts?
I just find it odd that no one else has tried to obtain the 500 point award.  
0
 

Author Comment

by:shrek2
Comment Utility
jmcg,

I do not want to close the question i would like to get an answer.   This question is only hours old. On average how long should i wait for some kind of response?  If I do not get responses within 24 hours should i close the question and ask it again, or should i assume there is no answer.  

thank you for your advice on this matter.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
use Fcntl;  #will define F_SETFD
0
 

Author Comment

by:shrek2
Comment Utility
ozo,

I am not a whiz with perl:)   so could i ask you to be a bit more descriptive PLEASE.  
do you want me to type this   "use Fcntl;  #will define F_SETFD"   into my original script?

thanks
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
#try typing this:
use Fcntl;
#into your original script
0
 

Author Comment

by:shrek2
Comment Utility
I tried this and it still is not working. It hangs requiring a control break this is with the lines in the Gnupg module commented out.  

If i uncomment these lines and use the "use Fcntl;" then i get the errror message as before, "The vendor has not defined Fcntl macro F_SETFD, used at C:/Perl/lib/GnuPG.pm line 203"

Thanks
0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
Actually several newsgroups reflected the problem on windows, no solution were found.

There is maybe one alternative: try to run your script from the cygwin environment (unix like) on windows

http://www.cygwin.com/
0
 

Author Comment

by:shrek2
Comment Utility
Here is a quesiton:  I am using another decryption program "gpg" that is dos command line can i use perl to call a .bat file to run this gpg in a dos environment  and then capture the associated file that is decrypted and send it on its way.  
I will look into cygwin put i did not want to incorporate more apps.  
0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
Actually in Perl you can do simply

@result = `gpg --output decrypted  encrypted`;  # note this is backquote `


@result being the result of the command sent to stdout (if any :)
0
 

Author Comment

by:shrek2
Comment Utility
I am not a whiz with perl:)   so could i ask you to be a bit more descriptive PLEASE.  
i am not familiar with the @results options in perl.  Iam also not clear with your statement @result being the result of the command sent to stdout (if any :)

I am assuming your  'gpg -output decrypted encrypted'; # would be the encrypted or decrypted filename? for instances I use gpg -d -o c:\decrypt c:\encrypt
this decrypts the file named encrypt and places it at c:\decrypt.  
Here is my problem when i run this it prompts me for a passphrase which defeats the whole automation thing.  Can you help me to hand off my pass phrase to the prompt-

enter passphrase:  

within pgp i don't see anything that seems to allow this to happen.

thanks
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 500 total points
Comment Utility
Ok, so I could get it working but not through the DOS commands.
I had to use the windows bash environment from cygwin : http://www.cygwin.com/
To be installed with default options, and yes to "create an icon etc...".
Well, it demands some efforts from your side...

Once you installed cygwin...

So for gpg you need (see gpg help if necessary)
- the secret key or passphrase
- a "user" (the -r option)
- a "crypted file name"
- the "decrypted file name"


1. passphrase
Open a cygwin window (click icon). You should see a black window with a "$ " prompt. Welcome to Unix :)
Go to your gpg distrib ; e.g. it is in m:\program files\gpg ; do
   cd "m:/program files/gpg"
Note the quotes and the /'s (not \)
Put your passphrase in a file, call this file for instance secret.key ; on cygwin do
   echo "my passphrase" > secret.key
Ensure there is no endline at the end of the file. Actually it wouldn't be a problem on unix/linux but since you are on windows  :`(   you have to cope with the trailing ^M (control M).  Do
    dos2unix secret.key


2. test first manually that it works
(you're still in cygwin, gpg directory)
Maybe you need to generate a key, do
   gpg --gen-key
Keep safely the User and Passphrase (the one you put in the file in 1.)...
Crypt a file  foo  to  foo.gpg, then...
Let's try to decrypt it
   gpg --batch -o bar --passphrase-fd 0 -r youruser  --decrypt foo.gpg < secret.key
Do you get foo = bar ?

This has to work before going to step 3 :)


3. Perl script to do the same decryption - all paths (if any) have to be used with '/' and not '\'
Create this script in the same directory, call it decrypt.pl

#!perl

my $distrib = "put here the directory where we are e.g. m:/program files/gpg";
my $user = "put it your user";
my $pass = "secret.key";
my $file   = "put here name of file to decrypt";
my $out   = "put here name of decrypted file";

chdir ($distrib);

my @res  = `gpg --batch -o $out --passphrase-fd 0 -r\"$user\" --decrypt $file < $pass`;


4. Try the perl script
(you're still in cygwin, gpg directory)

  perl  decrypt.pl

What happens? Did you get your output file?
(of course you could have removed  "bar" before to run it to ensure you got a new decrypted version, or check the date time of the "bar" file)


5. Automatization

a) try first to call the perl script above "as is" - does it work?  If yes, that's it :)
    If no, look at logs ... to check what's wrong before to go for  b)

b) if it doesn't work, we have to go through cygwin:
- open a cygwin window
- go to same dir as before e.g.   cd "m:/program files/gpg"
- create a file named decrypt.bash the same way you did for secret.key (change path if necessary)
   as usual, path separator is /, not \ ; the \" is to get an actual " in the created file.

   echo "perl \"m:/program files/gpg/decrypt.pl\"" > decrypt.bash

- and finally instead of call  perl decrypt.pl (from php or whatever)  call
   m:\program files\cygwin\bin\bash  m:/program files/gpg/decrypt.bash
where m:\program files\cygwin is your cygwin distrib location - note that I used \, since I'm not sure what you have to use on your system -  php.
And m:/program files/gpg/ is the path where we get all the stuff, gpg, secret.key and now decrypt.bash.
What does this do?
It call the cygwin  bash command interpretor to be in its environment, which in turn  read the decrypt.bash file which in turn calls perl  with the right environment...


Sounds hard ? It woks here with cygwin :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now