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

Hello,
I'm trying to encrypt the file using PGP and after encrypting the command is creating a new file with name OriginalFilename.asc as we wanted the Ascii armour. This is more of a PGP question thru perl but still wanted to try here as some experts might know it :)

Perl Version - 5.10.1 on Solaris
PGP - PGP commandline version for Solaris 6.5.8
my $file = '/opt/SampleData.txt'; $encrytion = `pgp -ea +batchmode $file publickey.pgp`;
The above encryption command produces new encrypted file with name 'SampleData.txt.asc' leaving the original file 'SampleData.txt' untouched in the same location.

Is there a way that I can modify the above PGP command so that after encryption the original file will be removed automatically leaving only the encrypted file on server rather than issuing a separate delete command?

Thanks for your help.

Replies are listed 'Best First'.
Re: OT: PGP encryption command to use in Perl
by Corion (Patriarch) on Oct 12, 2015 at 18:20 UTC

    I would use the -f command line option and use pgp as a filter:

    my $cmd = "pgp -f -ea +batchmode publickey.pgp >SampleData.txt.asc"; open my $pgp, "| $cmd" or die "Couldn't run [$cmd]: $!"; print $pgp "Hello World"; close $pgp;
      When I run using '-f' argument, the cursor is just stuck there and doesnt do any thing.

      I tried the following and it worked but the description for 'w' scares me :(

      $encrytion = `pgp -eaw +batchmode $file publickey.pgp`;

      Description from Doc "This instructs PGP to create a ciphertext file “message.pgp”, and to destroy the plaintext file “message.txt”. Note that this option will not wipe out any fragments of plaintext that your word processor might have created on the disk while you were editing the message before running PGP. Most word processors create backup files, scratch files, or both. PGP overwrites the file 26 times."

      Is it safe to use the above command or does it do something funky and I'm better off issuing a separate delete command for the original file?

      Thank you for your help

        I couldn't test this and only showed you what I read from the manpage.

        Personally, I use a pipe through GPG like this:

        my $GPG_cmd = "/usr/bin/gpg --homedir . --keyring pubring.gpg --secret +-keyring secring.gpg --batch --trust-model always -z 7 --no-secmem-wa +rning --lock-never"; open my $gpg, qq(| $GPG_cmd --output "$OUTFILE") or die "Couldn't launch $GPG_cmd: $!"; print $gpg $mydata;
        Did you open it with
        open my $fh, "| pgp -f ..." or die ...
        or did you perhaps use the -f option with backticks?

        I don't know the specific version of pgp, Corion's seems to read from STDIN when -f is used, so this would just wait for your input from the keyboard (with backticks) instead of open "| ..." reading from the file handle.

        Update: Better style: Three argument form
        open my $fh, '|-', $cmd or die "failed to execute:\n\t$cmd\n$! +";
        References: qx and Quote Like Operators for backticks, Using open() for IPC for open
Re: OT: PGP encryption command to use in Perl
by NetWallah (Canon) on Oct 12, 2015 at 18:26 UTC
    From The PGP man page:

    --input-cleanup
    This option will clean up the input file, depending on the arguments you specify: off (default), remove, or wipe.

    Example:

    % pgp -c report.txt --symmetric-passphrase cam3r0n --input-cleanup wipe --wipe-input-passes 8

    The output file is "report.txt.pgp", which is encrypted with a symmetric algorithm. The input file "report.txt" was wiped with 8 passes.

            Software efficiency halves every 18 months, thus compensating for Moore's Law.

      Unfortunately I cannot use this as we have Solaris 10, so stuck to the 6.5.8 version which is the latest I believe. Thanks.