Skip to content

Instantly share code, notes, and snippets.

@goerz
Created August 1, 2010 18:01
Show Gist options
  • Save goerz/503584 to your computer and use it in GitHub Desktop.
Save goerz/503584 to your computer and use it in GitHub Desktop.
encrypt.pl, decrypt.pl: Two scripts that encrypt, and decrypt files using GPG
#!/usr/bin/perl -w
use strict;
my $recursive = 'false';
if ($ARGV[0] eq '-r') {
$recursive = shift @ARGV;
$recursive = 'true';
}
decrypt(\@ARGV);
sub decrypt{
my $filelist = shift;
foreach my $file (@{$filelist}){
if ( (-f $file) && ($file =~ /\.gpg$/) ){
print "\n\nDecrypting $file\n";
system('gpg "'."$file".'"');
my $newfile = $file;
$newfile =~ s/\.gpg$//;
if (-e $newfile){
system('rm "'."$file".'"');
} else {
warn("Decryption of $file was NOT successful\n");
}
}
if ( (-d $file) && ($recursive eq 'true') ){
my @recfilelist;
if ($file =~ /[ ]/){
@recfilelist = glob('./"'."$file".'"/*');
} else {
@recfilelist = glob("./$file/*");
}
decrypt(\@recfilelist);
}
}
}
#!/usr/bin/perl -w
use strict;
my $recursive = 'false';
if ($ARGV[0] eq '-r') {
$recursive = shift @ARGV;
$recursive = 'true';
}
encrypt(\@ARGV);
sub encrypt{
my $filelist = shift;
foreach my $file (@{$filelist}){
if ( (-f $file) && (!(-e "$file.gpg")) && (!($file =~ /\.gpg$/)) ){
print "Encrypting $file ... \t";
system('gpg -e -r "Michael Goerz Local Encryption" "'."$file".'"');
if (-e "$file.gpg"){
print "Wiping $file\n";
system('wipe "'."$file".'"');
} else {
print "\n\n";
warn("Encryption of $file was NOT successful\n");
}
}
if ( (-d $file) && ($recursive eq 'true') ){
my @recfilelist;
if ($file =~ /[ ]/){
@recfilelist = glob('./"'."$file".'"/*'); # TODO: check if ./ can be removed
} else {
@recfilelist = glob("./$file/*");
}
encrypt(\@recfilelist);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment