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

Fellow Monks: Every so often I come across a difficulty that I think is so common someone else must have a solution, but when I look I cannot find it. Here is one that I am struggling with at the moment.

I have a Perl application that stores sensitive data in an XML file. Which was perfect during development because it made it easy to see what was happening and try out various situations by editing the file. Now that I am moving toward deployment I want to provide "compress" and "encrypt" capabilities to the application user, something like this:

             -->-Compress->--       -->-Encrypt->-| F |
+-----+     /                \     /              | i |
| App |->--?                  ->--?               | l |
+-----+     \                /     \              | e |
             -->---->----->--       -->---->---->-| s |

Which I thought I could implement like this (error handling removed for clarity):

... use MyData; $f = new MyData(file => "foo.compressed",encrypt_key =>""); ... package MyData; sub new { ... my $parser = XML::SAX::ParserFactory->parser( Handler => MyData::Handler->new); my $fh = _file_open($filename,$key); $parser->parse_file($fh); ... } sub _file_open { my($file_name,$encrypt_key) = @_; $encrypt_key = "" if(!defined $encrypt_key); # This first bit works OK my $compress = ""; my $compress = 1 if($file_name =~ /\.compressed$/); if($encrypt_key eq "") { return new IO::Zlib($file_name,"rb") if($compress); return new IO::File($file_name,"r"); } # This is where I get into trouble ????? }

Now if I was running on a *IX machine I could create an encryption process and pipe my (possibly) compressed data to it, but I'm on Windows and I can't get pipe and fork to do anything useful. The files are small enough that I could read them into memory, decrypt them, inflate them and then create an IO::Scalar for SAX, but that would need me to know more about the Compress::Zlib than I currently do.

I am running the ActiveState 5.6.1 on a Windows 2000 machine

This must be a common requirement and I am sure that the only reason the answer is not obvious is my lack of encryption experience. How have others done this?

Update: Of course I also want to do something similar when writing a file