PERL script for decription not working

Posted on 2004-04-28
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/ line 203

Question by:shrek2
  • 7
  • 5
  • 2
LVL 10

Expert Comment

ID: 10940425
<< 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.

Author Comment

ID: 10942039
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.

LVL 10

Expert Comment

ID: 10942073
In this case, I cannot be more help, didn't find a module written for windows.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 10942753
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.  

Author Comment

ID: 10942985

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.
LVL 84

Expert Comment

ID: 10943719
use Fcntl;  #will define F_SETFD

Author Comment

ID: 10943796

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?

LVL 84

Expert Comment

ID: 10943810
#try typing this:
use Fcntl;
#into your original script

Author Comment

ID: 10950540
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"

LVL 10

Expert Comment

ID: 10950577
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

Author Comment

ID: 10951178
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.  
LVL 10

Expert Comment

ID: 10951225
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 :)

Author Comment

ID: 10952745
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.

LVL 10

Accepted Solution

Mercantilum earned 500 total points
ID: 10956074
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 :)

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

685 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