PERL script for decription not working

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/ line 203

Who is Participating?
Ok, so I could get it working but not through the DOS commands.
I had to use the windows bash environment from cygwin :
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


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)


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.bash

- and finally instead of call  perl (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 :)
<< Unlike unix, Windows offers only blocking io on pipes. >>
(See )

The lines causing problem in 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.
shrek2Author Commented:
When I comment out the line I get this error:

Wrong response from gnupg <expected SHM_INFO>:

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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

In this case, I cannot be more help, didn't find a module written for windows.
shrek2Author Commented:
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.  
shrek2Author Commented:

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.
use Fcntl;  #will define F_SETFD
shrek2Author Commented:

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?

#try typing this:
use Fcntl;
#into your original script
shrek2Author Commented:
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/ line 203"

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
shrek2Author Commented:
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.  
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 :)
shrek2Author Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.