#!/usr/bin/perl -w use strict; use warnings; use Crypt::Tea; if (@ARGV != 3) { print STDERR "Usage: $0 [-d|-e] [FILENAME] [KEY] \n"; exit 1; } if (($ARGV[0] ne '-d') && ($ARGV[0] ne '-e')) { print "Specify '-d' to decrypt or '-e' to encrypt\n"; exit 1; } my ($action, $file, $key) = @ARGV; open (my $fh, '<', $file) or die $!; my @input = readline($fh) or die "readline failed: $!\n"; if ($action eq '-e') { my @cryptedText = cryptText($key,$action,\@input); my $i = join("\n",@cryptedText); print $i; } if ($action eq '-d') { my @cryptedText = cryptText($key,$action,\@input); my $i = join('',@cryptedText); print $i; } sub cryptText { die if @_ != 3; my ($key,$action,$input) = @_; my @cryptedText; foreach my $text (@$input) { if ($action eq '-d') { $text = decrypt($text,$key); } if ($action eq '-e') { $text = encrypt($text,$key); } push (@cryptedText,$text); } return @cryptedText; }