#!/usr/bin/perl -w use strict; use Crypt::RC4; print "usage: $0 [ crypt | view ] \n" and exit unless validateInput(@ARGV); my ($option, $file, $password) = @ARGV; my ($plaintext, $crypted, $decrypted); $plaintext = openFile($file); if ($option eq 'view') { $decrypted = RC4($password, $plaintext); print "$decrypted\n"; } elsif ($option eq 'crypt') { my $outputname = "$file" . '.enc'; $crypted = RC4($password, $plaintext); open FH, ">$outputname" or print "error: $!\n" and exit; print FH "$crypted"; close FH; print "$file encrypted with the given password\nnew filename $outputname\n"; } sub openFile { my $filename = shift; my $data; open FH, "<$filename" or print "error: $!\n" and exit; while () { $data .= $_; } close FH; return $data; } sub validateInput { return undef unless @_ == 3; return undef unless ($_[0] =~ /view|crypt/); return undef unless (-e $_[1]); 1; }